I am using a jsch library to connect to a server using ssh connection. Then I open an “exec” channel to execute a ping command to ping other devices connected to that server.
e.g.
ping 192.168.0.128
Then it starts printing out in the console the response which is a string and looks as follows:
– successful ping:
PING 192.168.0.128 (192.168.0.128) 56(84) bytes of data.
64 bytes from 192.168.0.128: icmp_req=1 ttl=64 time=0.253 ms
64 bytes from 192.168.0.128: icmp_req=2 ttl=64 time=0.231 ms
-unsuccessful ping:
PING 192.168.0.126 (192.168.0.126) 56(84) bytes of data.
From 192.168.0.7 icmp_seq=1 Destination Host Unreachable
From 192.168.0.7 icmp_seq=2 Destination Host Unreachable
How can I handle the output to determine programmatically whether the connection has been successful or not? A regexp pattern?
On your channel object call
getExitStatus(), if it is equal to 0 then the connection was successful, if it was equal to 1 then the connection was not successful.See http://linux.die.net/man/8/ping for the exit codes.