日期:2014-05-16  浏览次数:20597 次

linux下ping多台机器,扫描多个特定端口
周末被问倒的一个问题,其实就是考基础的shell

1.linux下ping多台机器:
建立/tmp/servers
[root@localhost ~]# cat /tmp/servers 
baidu.com
google.com
sina.com

建立相关的sh文件
[root@localhost ~]# cat /tmp/mping.sh 
#!/bin/bash
while read line   #读每行
do
ping $line -c 3  #ping特定的host,cout为3
done < /tmp/servers

测试结果:
[root@localhost ~]# /tmp/mping.sh 
PING baidu.com (123.125.114.144) 56(84) bytes of data.
64 bytes from 123.125.114.144: icmp_seq=1 ttl=128 time=60.6 ms
64 bytes from 123.125.114.144: icmp_seq=2 ttl=128 time=63.1 ms

--- baidu.com ping statistics ---
3 packets transmitted, 2 received, 33% packet loss, time 1999ms
rtt min/avg/max/mdev = 60.649/61.923/63.197/1.274 ms
PING google.com (74.125.71.104) 56(84) bytes of data.
64 bytes from hx-in-f104.1e100.net (74.125.71.104): icmp_seq=1 ttl=128 time=158 ms
64 bytes from hx-in-f104.1e100.net (74.125.71.104): icmp_seq=2 ttl=128 time=151 ms
64 bytes from hx-in-f104.1e100.net (74.125.71.104): icmp_seq=3 ttl=128 time=154 ms

--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 151.705/154.733/158.267/2.740 ms
PING sina.com (12.130.132.30) 56(84) bytes of data.
64 bytes from 12.130.132.30: icmp_seq=1 ttl=128 time=225 ms
64 bytes from 12.130.132.30: icmp_seq=2 ttl=128 time=233 ms
64 bytes from 12.130.132.30: icmp_seq=3 ttl=128 time=224 ms

--- sina.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2013ms
rtt min/avg/max/mdev = 224.907/227.901/233.778/4.173 ms


2.linux下扫描特定机器端口
建立/tmp/servers_port
[db2inst1@localhost tmp]$ cat /tmp/servers_port 
google.com 80
baidu.com 8080 #不通的
127.0.0.1 22

建立脚本mnc.sh
[db2inst1@localhost tmp]$ cat /tmp/mnc.sh 
#!/bin/bash
while read line
do
nc -zw1 $line #超时一秒
done < /tmp/servers_port

测试结果
[root@localhost tmp]# ./mnc.sh 
Connection to google.com 80 port [tcp/http] succeeded!
Connection to 127.0.0.1 22 port [tcp/ssh] succeeded!