5544992 发表于 2018-8-22 06:42:03

小黑的日常折腾-网段在线地址扫描shell脚本

#!/bin/bash  
#Program:
  
#       This program show the online host.
  
#History:
  
#2016/4/3    xiaohei   v1.0
  
#blog:http://zww577593841.blog.51cto.com/6145493/1750689
  
#环境变量初始化
  
PATH=./:$PATH
  
export PATH
  
LANG=zh_CN.UTF8
  
#定义变量和数组
  
#在线主机数
  
declare -iuphost=0
  
#不在线主机数
  
declare -idownhost=0
  
#已经ping的主机个数
  
declare -ihostping=0
  
#ip地址的第一个段
  
declare-A ipclass
  
#信号捕获
  
trap'mytrap' INT
  
#捕捉到终止信号时进行的操作
  
mytrap(){
  
      clean_file
  
      echo "exit"
  
      exit 1
  
}
  
#查找在线主机并将查找到的在线主机保存至当前目录下的uphost.txt文件
  
hostping(){
  
      ping -W 2 -c 1 $1 &> /dev/null
  
      if [ $? -eq 0];then
  
                echo "$1 is up" >> ./uphost.txt
  
      fi
  
}
  
#清理上次运行该脚本生成的uphost.txt文件
  
clean_file(){
  
      if [ -f ./uphost.txt ];then
  
                rm -rf./uphost.txt
  
      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!"
  
      fi
  
}
  
analyse_ip(){
  
if [[ $#-ne 1 ]];then
  
      echo "need two ip"
  
      return 1
  
fi
  
#判断IP地址合法性
  
echo "$1" | grep -E "\" &> /dev/null
  
if [ $?-ne 0] ; then
  
      echo "The parameter error"
  
      return 1
  
fi
  
if [ "${ipclass}" -ne 254-o"${ipclass}" -ne 1    ];then
  
      echo "The parameter error"
  
      return 1
  
fi
  
}
  
#调用函数
  
clean_file
  
#获取网络地址第一个字段
  
ipclass=$(echo "$1" | cut -d. -f1)
  
ipclass=$(echo "$2" | cut -d. -f1)
  
[[ ${ipclass} != ${ipclass} ]] && echo "The parameter erro" && exit 1
  
#获取地址的其他地址段
  
for((i=1;i
页: [1]
查看完整版本: 小黑的日常折腾-网段在线地址扫描shell脚本