[SOLVED] How to check if a server is up / down if pinging to that machine is not allowed
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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
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.
Last edited by MensaWater; 09-03-2012 at 12:08 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.