Learning Linux has been one months, unknowingly shell script basic usage has been studied, in order to test their learning situation, in the Qingming Festival took a little time to toss out the script. The work is only practiced hand, if there is a bug, please leave a message to indicate, I will try to perfect.
The script requires the user to give two parameters, the first parameter is the start IP address of the network segment, and the second parameter is the end IP address of the network segment. IP address of the starting address the last paragraph must be 1, such as 192.168.0.1, and the last digit of the end IP address must be 254, such as 192.168.10.254. Also, the first segment of the start and end IP addresses must be the same.
Attention:
1. The larger the address range, the longer the running program takes.
2, to ping the other host firewall must be closed to detect the.
3, the program ping timeout is 1 seconds, the number of packets sent is 1, if the line between the host is not stable or lost packets, may judge the other side is not the line, you can modify the ping parameters in the script to find exactly.
This script is suitable for the small and medium-sized machine room without monitoring platform patrol, the host running the script is not an online business Server. This script is also suitable for me just like the beginning of the shell novice practiced hand, using a relatively basic knowledge.
The script reads as follows:
#!/bin/bash#program:# this program show the Online host. #History: #2016/4/3 xiaohei v1.0#blog:http:// zww577593841.blog.51cto.com/6145493/1750689# environment variable Initialization path=./: $PATHexport pathlang=zh_cn. utf8# define variables and Arrays # Number of online hosts declare -i uphost=0# not online host number declare -i downhost=0# Number of hosts that have been ping declare -i hostping=0#ip the first segment of the address declare -a ipclass# signal capture trap ' Mytrap ' int# the action to snap to the stop Signal mytrap () { clean_file echo "Exit" exit &NBSP;1} #查找在线主机并将查找到的在线主机保存至当前目录下的uphost. txt file hostping () { ping -W 2 -c 1 $1 &> /dev/null if [ $? -eq 0 ];then echo "$ is up " >> ./uphost.txt fi}# Cleans up the Uphost.txt file that was last run the script generated by Clean_file () { if [ -f ./ uphost.txt ];then &NBSP;&NBSP;RM&NBSP;-RF&NBSP;&NBSP;./UPHOST.TXT&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FI} #显示在线主机show_ Uphost () { if [ -f ./uphost.txt ];then cat ./ uphost.txt | sort -u echo "uphost: $ (grep -e " [^[:space:]] " ./uphost.txt | sort -u | wc -l ) " sync else echo "no host is up!" &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FI}ANALYSE_IP () {if [[ $# -ne 1 ]];then echo "Need two ip" return 1fi# determine IP address legality echo "$" | grep -E "\< ([ 1-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3]) \. ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \.) {2} ([0-9]| [1-9] [0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]) \> " &> /dev/nullif [ $? -ne 0 ] ; then echo "The parameter error" return 1fiif [ "${ipclass[ipend3"} " -ne 254 -o " ${ IPCLASS[IPSTART3]} " -ne 1 ];then echo "The parameter error" return 1fi } #调用函数clean_file # Get network Address first field ipclass[iphead1]=$ (echo "$" | cut -d. -f1) ipclass[ iphead2]=$ (echo "$" | cut -d. -f1) [[ ${ipclass[iphead1]} != ${ipclass [Iphead2]} ]] && echo "The parameter erro" && exit 1# Gets the address of the other address segment for ((i=1;i<=3;i++));d o ipclass[ipstart$i]=$ (Echo "$" | cut -d. -f$[$i +1]) ipclass[ ipend$i]=$ (echo "$" | cut -d. -f$[$i +1]) done# Call function analyse_ip $1 && analyse_ip $2 | | exit# Scan the online host for ((i=${ipclass[ipstart1]};i<=${ipclass[ipend1]};i++)) of each segment;d o for ((j=${ipclass[ipstart2]};j<=${ipclass[ipend2]};j++));d o for ((k=${ipclass[ipstart3]};k<=${ ipclass[ipend3]};k++));d o #将程序送至后台执行 hostping ${ipclass[ iphead]}.${i}.${j}.${k} & let hostping++ done #防止一次性执行过多的ping导致主机崩溃, can be adjusted according to their own host if [ $hostping &NBSP;-GT 1000 ];then hostping=0 sleep 1 fi donedone# Call Function Show_uphost
The program is run and tested as follows:
1) Execute Permissions for script
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/73/wKioL1cA1VGi5BBrAABU2As9vn0198.png "style=" float: none; "title=" 1.png "alt=" Wkiol1ca1vgi5bbraabu2as9vn0198.png "/>
2) Change the environment variable path, run the script, the script parameter format requirements described above.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/73/wKioL1cA1VGB4DU1AAAm3T18WnM867.png "style=" float: none; "title=" 2.png "alt=" Wkiol1ca1vgb4du1aaam3t18wnm867.png "/>
3) hint of parameter given error
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/77/wKiom1cA2J2y4Q0fAAAsDYZC888076.png "title=" 3.png " alt= "Wkiom1ca2j2y4q0faaasdyzc888076.png"/>
4) test the efficiency of the script, the larger the network segment to be tested, the longer it takes, the more I have tested the range of more than 2,500 hosts.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/73/wKioL1cA23fiJVDnAABAOT-33Yo281.png "title=" 4.png " alt= "Wkiol1ca23fijvdnaabaot-33yo281.png"/>
If you are testing this script to find what bug, please leave a message that I will try to improve, thank you!
This article is from the "Little Black" blog, please be sure to keep this source http://zww577593841.blog.51cto.com/6145493/1759899
Small black Daily Toss-network segment online address scan shell script