Home > OS >  How to check remote servers can ping each other using a script?
How to check remote servers can ping each other using a script?

Time:07-16

I need to write a script which checks whether 3 servers can ping each other. I run the script at my local linux host.

Here is what I plan to do:

ssh [email protected] "ping -c 1 10.20.77.1"
echo $?
0

In the above example, 10.238.155.155 is one server, the command login to this server and ping 10.20.77.1 which is an interface at another server.

Then I check the command return value, $?, if it is 0, then it means ping is good.

ssh [email protected] "ping -c 1 10.20.77.9"
echo $?
1

In this example, 10.20.77.9 does not exist, we can see $? is 1.

My script basically repeats running SSH login to each server, ping other servers and checks $?.

Do you think this is a reliable solution?

CodePudding user response:

With echo $? you are checking the return of ssh, which is not what you want.

Try capturing the output of the compound ssh command in a variable, then parsing that variable as needed

myvar=$(ssh [email protected] "ping -c 5 server1.place.com")
  • Related