检测网络连通性的shell脚本,代码如下:
复制代码代码示例:
#!/bin/bash
# check host alive # At 2013-7-22 #declare var GATEWAY=`cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep 'GATEWAY=' | sed 's/^.*GATEWAY=//g'` NAMESERVER=`cat /etc/resolv.conf | grep 'nameserver ' | sed 's/^.*nameserver //g'` ping -c 5 127.0.0.1 if [ "$?" != "0" ]; then echo "the interface error" fi ping -c 5 $GATEWAY if [ "$?" != "0" ]; then echo "the gateway is unreachable" fi ping -c 5 $NAMESERVER if [ "$?" != "0" ]; then echo "the remote host is down" fi exit 0
代码说明: (责任编辑:IT) |