因集群里操作系统都为Radhat 7.X的操作系统,我们需要通过ping去检测主机在线情况,而服务器数量比较大的就比较繁琐,进而写了sh脚本就服务器在线情况进行批量检测。本脚本根据同事脚本优化而来,在次感谢同事@苏老板的脚本。
1、脚本内容
本脚本运行在Linux操作系统上,通过本脚本可以批量检测ipv4、ipv6的地址,前提是配置好v4与v6协议栈。
#!/bin/bash iplist=$1 exe_time=$(date +"%Y%m%d-%H%M") num=0 while [[ num -le 20 ]]; do while read host || [ -n "$host" ]; do if [[ $host == *.* ]];then ping=`ping -c 3 -w 3 -i 0.01 ${host} |grep ttl |wc -l` if [ $ping -eq 0 ];then echo ping $host fail | tee -a ${exe_time}_Ping_IPV4V6.log else echo ping $host ok | tee -a ${exe_time}_Ping_IPV4V6.log fi elif [[ $host == *:* ]];then ping6=`ping6 -c 3 -w 3 -i 0.01 ${host} |grep ttl |wc -l` if [ $ping6 -eq 0 ];then echo ping $host fail | tee -a ${exe_time}_Ping_IPV4V6.log else echo ping $host ok | tee -a ${exe_time}_Ping_IPV4V6.log fi else echo "ip address fail" fi done < ${iplist} let num+=1 echo " " | tee -a ${exe_time}_Ping_IPV4V6.log echo "============== ${num} ===============" | tee -a ${exe_time}_Ping_IPV4V6.log echo " " | tee -a ${exe_time}_Ping_IPV4V6.log done
2、使用教程
vim ping-ipv4_ipv6.sh #将上面脚本内容复制进去; chmod +x ping-ipv4_ipv6.sh #给脚本赋予执行权限; vim ip_list.sh #ip地址列表文件,可以直接从excel复制进来即可; ./ping-ipv4_ipv6.sh ip_list.sh #开始执行批量ping; ./ping-ipv4_ipv6.sh ip_list.sh |grep -E "fail|=" #筛选检测失败的地址;
3、操作截图
IP地址列表文件内容,为保证脚本正常使用,请使用下面文件内容格式: