标签:
#!/bin/bash # 在centos系统上面执行 # 执行方法: sh centospingbjbk.sh iplist1.txt # 输入: iplist.txt(ip列表)。警告,只能是ip,不能是host # 输出: faillist.txt(ping不通的ip列表)、oklist.txt(ping通的ip列表) # yuanlong.zhou # 2015.09.25 iplist=$1 cat /dev/null > faillist.txt && cat /dev/null > oklist.txt for i in `cat $iplist` do # centos是取第6个,mac是取第7个 ping=`ping -c 1 $i | grep loss | awk ‘{print $6}‘ | awk -F "%" ‘{print $1}‘` if [ $ping == 100 ];then echo ping $i fail >> faillist.txt else echo ping $i ok >> oklist.txt fi done 测试文件iplist1.txt中数据: 10.0.1.1 10.10.1.1 103.249.130.42 10.211.55.11 10.0.1.46 127.0.0.1 #!/bin/bash # 在mac系统上面执行 # 执行方法: sh macpingbjbk.sh iplist2.txt # 输入: iplist.txt(ip列表)。警告,只能是ip,不能是host # 输出: faillist.txt(ping不通的ip列表)、oklist.txt(ping通的ip列表) # yuanlong.zhou # 2015.09.25 iplist=$1 cat /dev/null > faillist.txt && cat /dev/null > oklist.txt for i in `cat $iplist` do # centos是取第6个,mac是取第7个 ping=`ping -c 1 $i | grep loss | awk ‘{print $7}‘ | awk -F "%" ‘{print $1}‘` if [ $ping == 100 ];then echo ping $i fail >> faillist.txt else echo ping $i ok >> oklist.txt fi done 测试文件iplist.txt2中数据: 10.0.1.1 10.10.1.1 10.211.55.11 10.0.1.46 改进后(脚本字段区分centos和mac) #!/bin/bash # 在centos或mac系统上面执行(脚本自动区分当前系统是centos还是mac) # 执行方法: sh pingbjbk.sh iplist3.txt # 输入: iplist3.txt(ip列表)。警告,只能是ip,不能是host # 输出: faillist.txt(ping不通的ip列表)、oklist.txt(ping通的ip列表) # yuanlong.zhou # 2015.09.25 iplist=$1 os=`uname` cat /dev/null > faillist.txt && cat /dev/null > oklist.txt flag=‘$6‘ flag2="100" if [ $os == Linux ];then flag=‘$6‘ flag2="100" else flag=‘$7‘ flag2="100.0" fi for i in `cat $iplist` do # centos是取第6个,mac是取第7个 # ping=`ping -c 1 $i | grep loss | awk ‘{print $6}‘ | awk -F "%" ‘{print $1}‘` ping=`ping -c 1 $i | grep loss | awk "{print $flag}" | awk -F "%" ‘{print $1}‘` if [ $ping == $flag2 ];then echo ping $i fail >> faillist.txt else echo ping $i ok >> oklist.txt fi done 测试文件iplist3.txt中数据: 10.0.1.1 10.10.1.1 103.249.130.42 10.211.55.11 10.0.1.46 127.0.0.1 10.0.1.1 10.10.1.1 10.211.55.11 10.0.1.46 =========================================================== =========================================================== =========================================================== =========================================================== 下面的未测试通过 先telnet登录,然后执行ping主机 # telnetpingip.sh # touch telnetpingip.sh && chmod 755 telnetpingip.sh && vim telnetpingip.sh #!/usr/bin/expect -f set timeout 30 set aip "10.0.1.1" set ausr "admin" set apwd "yhxt@123" spawn telnet ${aip} expect "Username:" send "${ausr}\r" expect "Password:" send "${apwd}\r" # TODO ping cmd expect "]*" interact (sleep 1;echo "root";sleep 1;echo "123456";sleep 1;echo "en";sleep 1;echo "1qazse4";sleep 1;echo "conf t";sleep 1;echo "int fa0/1";sleep 1;echo "switchport mode multi";sleep 1;echo "end";sleep 1;echo "exit") | telnet 10.32.17.10
标签:
原文地址:http://my.oschina.net/ylchou/blog/515604