LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to check if a server is up / down if pinging to that machine is not allowed (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-check-if-a-server-is-up-down-if-pinging-to-that-machine-is-not-allowed-4175424222/)

Vi_rod 08-27-2012 10:02 AM

How to check if a server is up / down if pinging to that machine is not allowed
 
Hi,
How to check if a server is up / down if pinging to that machine is not allowed ?
should we do ssh and grep the required part in script?? please help

MensaWater 08-27-2012 12:54 PM

Various methods exist.

If you run "nmap -Pn -sn <hostname>" it avoids ping but attempts to see if the host is up. Output should be something like:

Starting Nmap 5.21 ( http://nmap.org ) at 2012-08-27 13:19 EDT
Nmap scan report for host (x.x.x.x)
Host is up.
Nmap done: 1 IP address (1 host up) scanned in 0.00 seconds

If you run Nagios for monitoring the plugins include a binary called check_tcp and you can specify port 22 for that.
./check_tcp -H host -p 22
TCP OK - 0.001 second response time on port 22|time=0.000778s;;;0.000000;10.000000

Even if you don't run Nagios you can download and build that plugin for use.

I think were it me I'd go with something that just checked to see if the port was responding like the above rather than doing a full ssh login and command run.

Vi_rod 08-31-2012 02:09 AM

Thanks mate!
I totally forgot about NMAP :doh:

MensaWater 08-31-2012 08:56 AM

My apologies. I gave some incorrect advice above. The -PN is something I've used to avoid ping when doing full scans with nmap but what it really does is "assume the host is up" so it will always report host is up even if it fails on scanning.

Instead what seems to work after testing is:

Code:

nmap -PS22 -p22 <hostname or IP>
The above tells it to use port 22 for host discovery instead of ICMP (ping) then tells it to only scan for port 22.

Interestingly although the output of nmap will correctly report a host is down using that syntax it still gives a return code 0 as if it succeeded. This means to use it in a script you'd have to test for the text it outputs rather than the return code.

chrism01 09-02-2012 06:30 PM

That's because the rtn code is that of the nmap program (code) itself, not the returned data ie nmap worked fine ... it doesn't know/care whether you consider up or down to be a fail or success.

MensaWater 09-03-2012 12:06 PM

Quote:

Originally Posted by chrism01 (Post 4770944)
That's because the rtn code is that of the nmap program (code) itself, not the returned data ie nmap worked fine ...

Clearly that is the case but IMHO it is a bogus return code Telling me the code of binary ran without giving me some indication as to whether it was successful in doing what I asked it to do is not correct. Clearly it knows it failed given that it does output the text so I don't believe it should give a non-zero exit code. For example if I run "ls test" and the file named "test" doesn't exist I get both an error message and a return code of 2. Does this mean the binary didn't execute? No - it simply means it couldn't find the file I'd specified.

Quote:

Originally Posted by chrism01 (Post 4770944)
it doesn't know/care whether you consider up or down to be a fail or success.

It should certainly know/care based on what I specified on the command line just as my ls command would. If I had run simply "ls" in an empty directory it would output a line of "total 0" then show a return code of 0 because in THAT case it didn't "know/care" whether I thought an empty directory was bad but in the earlier "ls test" it did "know/care" that I was specifically looking for the file named "test" and gave the appropriate response.


All times are GMT -5. The time now is 02:51 PM.