试想一下,若要检查一个局域网的联通性,最长用的就是ping IP。如果局域网有很多台主机,也要一台台的ping?此脚本就可以简单的解决这样的问题,通过制定netid hostid的区间,就可以对指定的网段进行扫描检测。屏幕上会有输出检测的信息,所有检测到的已经联通的主机的MAC和主机名会被重定向到"运行时间.log"的文件中。
#netscan.sh内容如下
- #!/bin/bash
- #Filename: netscan.sh
- #Functions: 局域网主机联通性的扫描
- #Author: yanghao
- network=$1
- time=$(date +%H%M%S)
- for i in $(seq $2 $3)
- do
- ping -c 1 -w 2 $network.$i > /dev/null
- if [ $? -eq 0 ]; then
- arp $network.$i | grep ":" | awk '{print $1,$3}' >> $time.log
- echo "host $network.$i is up"
- else
- echo "host $network.$i is down"
- fi
- done
运行输出如下:
- stack@stack-G31M-ES2C:~/Desktop$ ./netscan.sh 192.168.1 100 120
- host 192.168.1.100 is down
- host 192.168.1.101 is down
- host 192.168.1.102 is down
- host 192.168.1.103 is down
- host 192.168.1.104 is down
- host 192.168.1.105 is down
- host 192.168.1.106 is down
- host 192.168.1.107 is down
- host 192.168.1.108 is down
- host 192.168.1.109 is down
- host 192.168.1.110 is up
- host 192.168.1.111 is up
- host 192.168.1.112 is down
- host 192.168.1.113 is down
- host 192.168.1.114 is up
- host 192.168.1.115 is up
- host 192.168.1.116 is down
- host 192.168.1.117 is down
- host 192.168.1.118 is up
- host 192.168.1.119 is down
- host 192.168.1.120 is down
重定向文件里面将会写有已联通主机的主机名和MAC。
阅读(413) | 评论(0) | 转发(0) |