16

I have 10 IP numbers which I have to ping daily for checking , How I can do that by using BASH script. So that I can automate that task by using cron. I want BASH script only.

Thank you.

Raja G
  • 100,643
  • 105
  • 254
  • 328
  • Below answer I have mentioned IP's of Google,yahoo,msn etc. I have tried that myself. Inserting {} and , are not working here between IP's to separate them. hope it may help somebody in future. Thank you for reading. – Raja G Jan 31 '14 at 05:36

7 Answers7

22

As your ip range has no symmetry and there are only 10 nodes, I would suggest to list them in a text file. I am considering the file containing the list is list.txt which contains list of ip one at each line as shown below,

10.12.13.14
172.15.48.3
192.168.45.54
...
48.114.78.227

You can use this script,

#!/bin/bash
# Program name: pingall.sh
date
cat /path/to/list.txt |  while read output
do
    ping -c 1 "$output" > /dev/null
    if [ $? -eq 0 ]; then
    echo "node $output is up" 
    else
    echo "node $output is down"
    fi
done

To update the running status of your nodes at an interval of 30 mins use at crontab,

*/30 * * * * /path/to/pingall.sh > /path/to/log.txt

Output of log.txt

$ cat /path/to/log.txt
Fri Jan 31 15:06:01 IST 2014
node 10.12.13.14 is up
node 172.15.48.3 is up
node 192.168.45.54 is up
...
node 48.114.78.227 is down
sourav c.
  • 44,037
  • 20
  • 101
  • 128
  • is it not what you were expecting? – sourav c. Feb 01 '14 at 13:30
  • this seems exactly what OP should be looking for.. and since websites like google.com, yahoo.com, etc. use multiple servers to handle requests it would be better to ping them with their domain names (so that you possibly won't have to change the IP in your ping-list the coming week).. – rusty Feb 03 '14 at 04:59
  • Works great, I posted a slightly modified answer which I use inside a Travis CI environment. – MitchellK Jul 18 '17 at 14:17
2

Assume that you have 5 IP's( to reduce the answer only) then you can ping them with

#!/usr/bin/bash    
for i in xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx xxx.xxx.xxx.xxxx 
do
ping -c 5 $i
done

Note: Not curl brackets , No Commas(,) between IP's.

Hope that helps.

Ex:

[raja @ scripts]$ cat ping.sh
for i in 74.125.236.70  98.139.183.24  65.55.206.228  91.189.94.156 198.252.206.24
do
ping -c 5 $i 
done 
[raja @ scripts]$ ./ping.sh
PING 74.125.236.70 (74.125.236.70) 56(84) bytes of data.
64 bytes from 74.125.236.70: icmp_seq=1 ttl=128 time=11.5 ms
64 bytes from 74.125.236.70: icmp_seq=2 ttl=128 time=11.0 ms
64 bytes from 74.125.236.70: icmp_seq=3 ttl=128 time=10.9 ms
64 bytes from 74.125.236.70: icmp_seq=4 ttl=128 time=16.5 ms
64 bytes from 74.125.236.70: icmp_seq=5 ttl=128 time=18.2 ms

--- 74.125.236.70 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4025ms
rtt min/avg/max/mdev = 10.966/13.682/18.291/3.120 ms
PING 98.139.183.24 (98.139.183.24) 56(84) bytes of data.
64 bytes from 98.139.183.24: icmp_seq=1 ttl=128 time=244 ms
64 bytes from 98.139.183.24: icmp_seq=2 ttl=128 time=253 ms
64 bytes from 98.139.183.24: icmp_seq=3 ttl=128 time=255 ms
64 bytes from 98.139.183.24: icmp_seq=4 ttl=128 time=251 ms
64 bytes from 98.139.183.24: icmp_seq=5 ttl=128 time=243 ms

--- 98.139.183.24 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4251ms
rtt min/avg/max/mdev = 243.511/249.623/255.275/4.674 ms
PING 65.55.206.228 (65.55.206.228) 56(84) bytes of data.
From 10.22.96.94 icmp_seq=5 Packet filtered

--- 65.55.206.228 ping statistics ---
5 packets transmitted, 0 received, +1 errors, 100% packet loss, time 14002ms

PING 91.189.94.156 (91.189.94.156) 56(84) bytes of data.
64 bytes from 91.189.94.156: icmp_seq=1 ttl=128 time=240 ms
64 bytes from 91.189.94.156: icmp_seq=2 ttl=128 time=240 ms
64 bytes from 91.189.94.156: icmp_seq=3 ttl=128 time=240 ms
64 bytes from 91.189.94.156: icmp_seq=4 ttl=128 time=240 ms
64 bytes from 91.189.94.156: icmp_seq=5 ttl=128 time=240 ms

--- 91.189.94.156 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4242ms
rtt min/avg/max/mdev = 240.060/240.222/240.309/0.626 ms
PING 198.252.206.24 (198.252.206.24) 56(84) bytes of data.
64 bytes from 198.252.206.24: icmp_seq=1 ttl=128 time=237 ms
64 bytes from 198.252.206.24: icmp_seq=2 ttl=128 time=237 ms
64 bytes from 198.252.206.24: icmp_seq=3 ttl=128 time=237 ms
64 bytes from 198.252.206.24: icmp_seq=4 ttl=128 time=237 ms
64 bytes from 198.252.206.24: icmp_seq=5 ttl=128 time=242 ms

--- 198.252.206.24 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4251ms
rtt min/avg/max/mdev = 237.600/238.575/242.291/1.933 ms
Raja G
  • 100,643
  • 105
  • 254
  • 328
  • you did not show the implementation with cron as mentioned in your Q.. also how would that script notify the result of the ping requests.. – rusty Jan 31 '14 at 05:35
  • I did mention that I want only script. Cron is not part of the question. Cron did mention for why I need this script & to mention the purpose of this script as it as a cron job. – Raja G Jan 31 '14 at 05:37
  • @hash Try it before asking my dear friend.Thank you for looking. – Raja G Jan 31 '14 at 05:38
  • I never said the script won't work.. but as your Q mentions `cron` job, a means to notify the user of the result of the job would be necessary, or do you suggest something else? – rusty Jan 31 '14 at 05:41
  • my purpose is cron job , I mean why I need this BASH script.but my requirement is bash @hash – Raja G Jan 31 '14 at 05:49
  • Well, it's not a bash script since there's no shebang line specifying that it is a bash script. `#!/usr/bin/env bash`. You should also [avoid using extensions on commands](http://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful). Lastly, for good practice, quote parameter expansions. `"$i"`, not `$i` to avoid the shell attempting wordsplitting and pathname expansion. – geirha Jan 31 '14 at 08:32
2

Check this script.

   #!/bin/bash
    for i in `seq ${2} ${3}`
    do
        ping -c 1 ${1}.${i} > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "${1}.${i} responded."
        else
            echo "${1}.${i} did not respond."
        fi
    done

To run ./script 192.168.1 0 10 for example this will ckeck the ips 192.168.1.0 to 192.168.1.10 and echo responded if ping is ok and didn't respond if not.

NB: You can replace $1 $2 $3 by static variables if the range and the IP's are always the same.

Maythux
  • 82,867
  • 54
  • 239
  • 271
2
#!/bin/bash
while read hostname
do
ping -c 1 -t 1 "$hostname" > /dev/null 2>&1 && 
echo "Ping Status of $hostname : Success" || 
echo "Ping Status of $hostname : Failed" 
done < host.txt

$ cat host.txt

host1.example.com
192.168.0.123
8.8.8.8
...
...
google.com

See:
http://www.thelinuxtips.com/2012/06/shell-script-to-ping-multiple-hosts/

2

Well

Simple as that: Use parallel --gnu command and then your command.

Get the example IP's :

$ dig +trace google.com |ipx

127.0.0.1
127.0.0.1
199.7.91.13
199.7.91.13
192.48.79.30
192.48.79.30
173.194.33.161
173.194.33.165
173.194.33.163
173.194.33.164
173.194.33.174
173.194.33.160
173.194.33.167
173.194.33.166
173.194.33.162
173.194.33.169
173.194.33.168
216.239.32.10
216.239.32.10

$ parallel --gnu ping -c1 ::: `dig +trace google.com |ipx`
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.018 ms

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

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.017/0.017/0.017/0.000 ms
PING 173.194.33.132 (173.194.33.132) 56(84) bytes of data.
64 bytes from 173.194.33.132: icmp_req=1 ttl=54 time=20.5 ms

--- 173.194.33.132 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 20.526/20.526/20.526/0.000 ms
PING 173.194.33.131 (173.194.33.131) 56(84) bytes of data.
64 bytes from 173.194.33.131: icmp_req=1 ttl=54 time=20.7 ms
d a i s y
  • 5,411
  • 9
  • 41
  • 59
Sirvesh
  • 21
  • 1
1
echo 192.168.1.1 192.168.1.2 192.168.1.3 | xargs -n1 ping -w 1

or with grep, see only non ping nodes

echo 192.168.1.1 192.168.1.2 192.168.1.3 | xargs -n1 ping -w 1 | grep -b1 100
gniourf_gniourf
  • 5,693
  • 2
  • 25
  • 28
qwe
  • 11
  • 1
0

Here is a script I wrote after reading this post.

https://bitbucket.org/kurtjensen/nettest/src/master/

It can use multiple text files as possible configs and the config files give you a chance to name the ip address more descriptively. The example config files are

  • home.txt - Which is the default
  • momdad.txt - This is for my parents network
  • etc.

So I can run the script at home and just hit enter at the prompt or enter something like "momdad" to switch to a different config fo a different network.