Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to check connectivity between 2 servers (i.e. if ssh will succeed).

The main idea is to check the shortest way between server-a and server-b using a list of middle servers (for example if I'm on dev server and I want to connect to prod server - usually a direct ssh will fail).

Because this can take a while, I prefer not to use SSH - rather I prefer to check first if I can connect and if so then try to connect through SSH.

Some possible routes to get the idea:

server-a -> server-b
server-a -> middle-server-1 -> server-b
server-a -> middle-server-6 -> server-b
server-a -> middle-server-3 -> middle-server-2 -> server-b

Hope you understand what I'm looking for?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

For checking server connectivity you have 4 tools at your disposal.

  1. ping

    This will check to see if any of the servers you're attempting to connect through, but won't be able to see if middle-server-1 can reach server-b, for example.

    You can gate how long ping will attempt to ping another server through the use of the count switch (-c). Limiting it to 1 should suffice.

    $ ping -c 1 skinner
    PING skinner (192.168.1.3) 56(84) bytes of data.
    64 bytes from skinner (192.168.1.3): icmp_req=1 ttl=64 time=5.94 ms
    
    --- skinner ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 5.946/5.946/5.946/0.000 ms
    

    You can check the status of this command through the use of this variable, $?. If it has the value 0 then it was successful, anything else and a problem occurred.

    $ echo $?
    0
    
  2. traceroute

    Another command you can use to check connectivity is traceroute.

    $ traceroute skinner
    traceroute to skinner (192.168.1.3), 30 hops max, 60 byte packets
    1  skinner (192.168.1.3)  0.867 ms  0.859 ms  0.929 ms
    

    Again this tool will not show connectivity through one server to another (same issue as ping), but it will show you the path through the network that your taking to get to another server.

  3. ssh

    ssh can be used in BatchMode to test connectivity. With BatchMode=yes you'll attempt to connect to another server, bypassing the use of username/passwords and only public/private keys. This typically speeds things up quite a bit.

    $ ssh -o "BatchMode=yes" skinner
    

    You can construct a rough one liner that will check for connectivity to a server:

    $ ssh -q -o "BatchMode=yes" skinner "echo 2>&1" && echo $host SSH_OK || echo $host SSH_NOK
    
    SSH_OK
    

    If it works you'll get a SSH_OK message, if it fails you'll get a SSH_NOK message.

    An alternative to this method is to also include the ConnectTimeout option. This will guard the ssh client from taking a long time. Something like this typically is acceptable, ConnectTimeout=5. For example:

    $ ssh -o BatchMode=yes -o ConnectTimeout=5 skinner echo ok 2>&1
    ok
    

    If it fails it will look something like this:

    $ ssh -o BatchMode=yes -o ConnectTimeout=5 mungr echo ok 2>&1
    ssh: connect to host 192.168.1.2 port 22: No route to host
    

    It will also set the return status:

    $ echo $?
    255
    
  4. telnet

    You can use this test to see if an ssh server is accessible on another server using just a basic telnet:

    $ echo quit | telnet skinner 22 2>/dev/null | grep Connected
    Connected to skinner.
    
share|improve this answer
    
I use your third suggestion(without the bacth-mode) and sometimes I need to stay connected(if it succeeded of course) and still get the SSH_OK/SSH_NOK message. What do I need to correct in the command? –  nir Sep 15 '13 at 20:45

Alternatively, you could try cutting down the SSH time by setting some config options, like ConnectTimeout=1. UseDNS=no also helps with systems that don't have reverse DNS configured correctly.

share|improve this answer

nc can "ping" port as well

nc -z hostname 22

this way you can ping port 22 on machine even if the machine is configured not to respond to regular ping (ICMP)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.