2

I have 4 servers that I want to ping each other.

Expected execution of ping:

ping from 1 to 2, 3, and 4 
ping from 2 to 1, 3, and 4 
ping from 3 to 1, 2, and 4 
ping from 4 to 1, 2, and 3 

How to achieve this via a bash script?

User login is already configured with password-less to all 4 servers.

7
  • @RuiFRibeiro none of those offer a bash solution for all to all ping.
    – terdon
    Jan 11 '18 at 12:20
  • A ping is bidirectional. Safe from changing firewall rules, especially if they all in the same infra-structure, it might be enough pinging all the servers from a central monitoring one for gauging the quality of the connection, or if they are up. Jan 11 '18 at 12:26
  • So you can already type ssh host1 ping -c 1 host2 and it works without prompting for a password? In which case, surely your bash file is just one line after another asking host_x to ping host_y. If you want something more elegant, paramaterisable, efficient or extendable, then @RuiFRibeiro comment gives you the tools to achieve that. Jan 11 '18 at 12:29
  • In fact I would not reinvent the wheel. For 3-10 machines, I would install Smokeping Jan 11 '18 at 13:32
2

For monitoring such a small number of machines, I would use SmokePing instead of reinventing the wheel with a script.

It will provide a more visual feedback, average historic data over time and even a professional look and feel to show yet another service and good job done to others. People and PHBs love visual feedback for monitoring things.

In fact, I used it to monitor critical points of my infra-structuture in the past when I had a not-so-reliable satellite connection to the Internet.

The configuration is done in a small text file, and it is easy to add new IP addresses; it also has got a nice web interface. You just need to install a lightweight web server for it.

You can monitor pretty much anything than can be monitored via ICMP/pings; you can also monitor other critical equipment besides those 4 servers.

To have a feel how the interface works, see the smokeping interface for the outside world of an university

See also the example images:

img1 img2

After installing the web server and Smokeping, the actual configuration file can be as simple as this:

*** Targets ***
 probe = FPing

 menu = Top
 title = Network Latency Grapher
 remark = Welcome to this SmokePing website.

 + mysite1
 menu = Site 1
 title = Hosts in Site 1

 ++ myhost1
 host = myhost1.mysite1.example
 ++ myhost2
 host = myhost2.mysite1.example

 + mysite2
 menu = Site 2
 title = Hosts in Site 2

 ++ myhost3
 host = myhost3.mysite2.example
 ++ myhost4
 host = myhost4.mysite2.example
1

Just store the names in an array and iterate over it, using ssh to connect to each host and ping:

ips=("server1" "server2" "server3" "server4");
for((i=0;i<${#ips[@]};i++)); do 
    for((k=0;k<${#ips[@]};k++)); do
        [[ $i -ne $k ]] && ssh "${ips[$i]}" ping -c1 "${ips[$k]}"; 
    done
done
2
  • thanks a lot terdon, your suggestion working but not expected, login to server1 and ping other 3 servers and do the same on others. login to server1 and ping server2, server3, server4 again exit and login to server2 and ping server1, server3, server4 do the same... the total output is 16-1 need
    – prakash
    Jan 12 '18 at 2:26
  • @prakash then please explain what you need.
    – terdon
    Jan 14 '18 at 10:11
1

Do you have fping installed? It would probably make this easiest:

$ hosts="www smtp"
$ for host in $hosts ; do 
    echo "$host:"; 
    ssh iv@$host "fping $hosts";
  done
www:
www is alive
smtp is alive
smtp:
www is alive
smtp is alive

(I know, that's a lazy solution.)

2
  • thanks for your attempt, but my expectation is different....
    – prakash
    Jan 12 '18 at 2:27
  • @prakash then explain how it is different! Please edit your question and explain more clearly so we don't waste your time or ours trying to guess what you need.
    – terdon
    Jan 14 '18 at 10:10
0

It works perfectly for Your requirement

It will login server1 and ping only for server2 and server3 and viceversa

#!/bin/bash
while read line
do
ssh root@$line "sed "/$line/d" /root/p_final.txt |sed '/^$/d' | sed -r "s/\s+//g"|awk '{print $1}' | sed 's/^/ping -c2 /g'| sh" </dev/null
done </root/p_final.txt

where file p_final.txt contains the list of ip's one by one.Let me know for any doubts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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