使用bash ping多个IP?


17

我有10个IP号码,我每天必须ping一下以进行检查,如何使用BASH脚本做到这一点。这样我就可以使用cron自动化该任务。我只想要BASH脚本。

谢谢。


下面的答案我提到了Google,yahoo,msn等的IP。我自己尝试过。插入{}和,在IP之间无法将它们分开。希望它对将来的人有所帮助。感谢您的阅读。
rɑːdʒɑ

Answers:


18

由于您的ip范围没有对称性,并且只有10个节点,因此建议您在文本文件中列出它们。我正在考虑包含该列表的文件list.txt,其中每行包含一个IP列表,如下所示,

10.12.13.14
172.15.48.3
192.168.45.54
...
48.114.78.227

您可以使用此脚本,

#!/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

要每隔30分钟更新一次节点的运行状态,请在crontab中使用,

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

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

这不是您所期望的吗?
souravc

这似乎正是OP应该寻找的。.并且由于google.com,yahoo.com等网站使用多个服务器来处理请求,因此最好使用其域名ping它们(这样一来,您可能就不会必须在下周更改您的ping列表中的IP。)
2014年

效果很好,我发布了一个稍作修改的答案,该答案在Travis CI环境中使用。
MitchellK


1

检查此脚本。

   #!/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

例如,要运行./script 192.168.1 0 10,它将把ips 192.168.1.0转换为192.168.1.10,如果ping正常,则echo响应,否则,则不响应。

注意:如果范围和IP始终相同,则可以用静态变量替换$ 1 $ 2 $ 3。


IP不在同一范围内。多数民众赞成在后面的标准。感谢任何方式回答+ 1。
rɑːdʒɑ

好的,请在您的问题中指出。感谢+
Maythux 2014年

整洁而简单的脚本可能非常有用(即:在嵌入式设备上),即使它不能100%解决问题的条件。谢谢。+1值得。
Sopalajo de Arrierez,

1

假设您有5个IP(仅用于减少答案),则可以使用

#!/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

注意:IP不能夹括号,IP之间不要逗号。

希望能有所帮助。

例如:

[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

你没有表现出通过计划的实施在您的问与答中提到..还怎么会是脚本通知ping请求的结果..
精确的

我确实提到我只想要脚本。Cron不是问题的一部分。Cron确实提到了为什么我需要此脚本,并提及了此脚本的用途,因为它是cron作业。
rɑːdʒɑ

@hash在问我亲爱的朋友之前尝试一下。谢谢您的关注。
rɑːdʒɑ

我从未说过该脚本将无法正常工作..但是正如您的Q提到的cron工作,将通知用户该工作结果的方法是必要的,或者您是否提出其他建议?
精确

我的目的是cron作业,我的意思是,为什么我需要这个BASH script.but我的要求是bash的@hash
rɑːdʒɑ

1
echo 192.168.1.1 192.168.1.2 192.168.1.3 | xargs -n1 ping -w 1

或使用grep,仅查看非ping节点

echo 192.168.1.1 192.168.1.2 192.168.1.3 | xargs -n1 ping -w 1 | grep -b1 100

1

就这么简单:使用 parallel --gnu命令,再您的命令。

获取示例IP:

$ 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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.