用shell完成网络检查与修复
---------------------------------------------------------------------------------------------
#Readme
#检查所有网络的步骤step 1: check wired whether ok
------- [wired_check.sh]
step 2: check NIC whether ok
------- [wired_check.sh]step 3: check network configure whether ok
------- [net_config_check.sh]
step 4: check dhcp service whether ok
------- [dhcp_service_check.sh]
step 5: check dns service whether ok
------- [dns_service_check.sh]
step 6: check host system file whether ok
------- [hosts_check.sh]
step 7: check connect wait num
------- [connect_num_check.sh]
#repair the network error
repair NIC
------ [repair_NIC.sh]
repair DHCP
------ [repair_dhcp.sh]
repair hosts
------ [repair_hosts.sh]
<strong>1,2,检查网线和NIC</strong>
#!/bin/sh
set -x
#check whether wired is ok
#return value: 1:wired link ok,and NIC ok; 2:wired no link,but NIC ok; 3:NIC error
linkinfo=`mii-tool`
wired_link=`echo $linkinfo | grep "ok"`
wired_nolink=`echo $linkinfo | grep "no link"`
network_card_error=`echo $linkinfo | grep "failed"`
if [ "$wired_link" != "" ]; then
<span style="white-space:pre"> </span>return 1
elif [ "$wired_nolink" != "" ]; then
return 2
elif [ "$linkinfo" = "" ]; then
<span style="white-space:pre"> </span>return 3
fi
set +x
<strong>3,检查网络环境</strong>
#!/bin/sh
set -x
#check network configure whether ok
#return value: 1:ip config ok 0:ip config error
interface=`mii-tool | grep "eth" | sed 's/:.*$//g'`
inet_address="inet 地址"
mast_cast="广播"
front="sed 's/^.*地址://g'"
back="sed 's/广播.*$//g'"
#check ip
ip=`ifconfig $interface | grep "inet 地址"| sed 's/^.*地址://g' | sed 's/广播.*$//g'`
ip1=`ifconfig $interface | grep "inet addr"| sed 's/^.*addr://g' | sed 's/Bcast.*$//g' `
if [ "$interface" != "" ];then
if [ "$ip" != "" -o "$ip1" != "" ];then
return 1
else
return 0
fi
else
return 0
fi
#check netmask
#netmask=`ifconfig $interface | grep `
set +x
<strong>4,检查dhcp服务</strong>
<pre name="code" class="plain">#!/bin/sh
#check dhcp service whether ok
#return vlaue: 1: dhcp serveice ok 0: dhcp service error
set -x
dhcp_status=`ps aux | grep "dhclient" | grep -v "grep"`
cur_network_staus=`cat /etc/NetworkManager/system-connections/* | grep method | sed -n '2p' | sed 's/^.*=//g'`
if [ "$cur_network_staus" = "auto" ];then
<span style="white-space:pre"> </span>if [ "$dhcp_status" != "" ];then
<span style="white-space:pre"> </span>return 1
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>return 0
<span style="white-space:pre"> </span>fi
else
<span style="white-space:pre"> </span>return 1
fi
set +x
</pre><p></p><pre>
<strong>5,检查dns</strong>
#!/bin/sh
#check dns service whether ok
#return value: 1: hostname ok 0:hostname error
set -x
#check main domain name
resolv_domainname=`cat /etc/resolv.conf | grep -v '#' | grep "nameserver" | awk '{print $2}'`
if [ "$resolv_domainname" = "" ]; then
return 0
else
return 1
fi
set +x
<strong>检查gateway</strong>
#!/bin/sh
#check gateway
#return value: 1: gateway ok 0:gateway error
set -x
gateway=`netstat -rn | egrep '^0.0.0.0' | tr -s ' ' | cut -d ' ' -f2`
if [ "$gateway" = "" ]; then
return 0
else
return 1
fi
set +x
<strong>6,检查hosts</strong>
#!/bin/sh
#check hosts configuration whether ok
#return value: 1:hosts configuration ok 0:hosts configuration error
set -x
HOSTNAME=`hostname`
#check duplicate line in /etc/hosts
check_dup_line()
{
if [ -f /etc/hosts ];then
dup_line=`cat /etc/hosts | egrep -v '^(#|$)' | uniq -d`
if [ "$dup_line" = "" ];then
echo "have no dupline"
return 1
else
return 0
fi
else
return 0
fi
}
#Check /etc/hosts contains an entry for this server name
check_server_ip()
{
if [ -f /etc/hosts ];then
host_name_ip=`cat /etc/hosts | egrep -v '^(#|$|::1|localhost)' | grep ${HOSTNAME}`
if [ "$host_name_ip" = "" ];then
echo "No entry for the server name"
return 0
else
return 1
fi
else
return 0
fi
}
#Check server hostname mapping
check_host_map()
{
if [ -f /etc/hosts ];then
map=`cat /etc/hosts | egrep -v '^(#|$)' | egrep '(localhost|::1)' | grep ${HOSTNAME}`
if [ "$map" = "" ];then
echo "this server hostname is not mapped to a local address"
fi
fi
}
check_dup_line
check_host_map
check_server_ip
if [ $? = 1 ];then
return 1
fi
set +x
<strong>检查等待链接数</strong>
#!/bin/sh
#check connection wait num
#return value : 1: connection wait ok 0:connection wait error
set -x
connect_num=`netstat -an | grep WAIT | wc -l | awk '{ print $1 }'`
if [ $connect_num -ge 100 ];then
echo "connect wait num error"
return 0
else
return 1
fi
set +x
修复网络
修复NIC
#!/bin/sh
set -x
#repair NIC
#We can do this only.
interface=`mii-tool | grep "eth" | sed 's/:.*$//g'`
ifconfig $interface down
ifconfig $interface up
set +x
修复dhcp
#!/bin/sh
set -x
#repair NIC
interface=`mii-tool | grep "eth" | sed 's/:.*$//g'`
#repair dhcp client
#dhcpclient always exists, though kill it;when kill it,dhcpclient will be reboot.
dhclient -r #release current ip
dhclient $interface #renew ip
/etc/init.d/networking restart
set +x
修复hosts
#!/bin/sh
set -x
#repair hosts configuration
#cp /usr/share/nfw/hosts /etc/ -rf
hostname=`hostname`
echo "127.0.0.1 localhost" > /etc/hosts
echo "127.0.0.1 $hostname" >> /etc/hosts
echo "::1 ip6-localhost ip6-loopback" >> /etc/hosts
set +x
#hosts
127.0.0.1 localhost