linux网络体检

来源:互联网 发布:lvs和nginx的主要区别 编辑:程序博客网 时间:2024/04/30 17:45

用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/shset -x#check whether wired  is ok#return value: 1:wired link ok,and NIC ok; 2:wired no link,but NIC ok; 3:NIC errorlinkinfo=`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 1elif [ "$wired_nolink" != "" ]; then    return 2elif [ "$linkinfo" = "" ]; then<span style="white-space:pre"></span>return 3fiset +x

<strong>3,检查网络环境</strong>
#!/bin/shset -x#check network configure whether ok#return value: 1:ip config ok  0:ip config errorinterface=`mii-tool | grep "eth" | sed 's/:.*$//g'`inet_address="inet 地址"mast_cast="广播"front="sed 's/^.*地址://g'"back="sed 's/广播.*$//g'"#check ipip=`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" != "" ];thenreturn 1elsereturn 0fielsereturn 0fi#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 errorset -xdhcp_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>fielse<span style="white-space:pre"></span>return 1fiset +x
</pre><p></p><pre>

<strong>5,检查dns</strong>
#!/bin/sh#check dns service whether ok#return value: 1: hostname ok 0:hostname errorset -x#check main domain nameresolv_domainname=`cat /etc/resolv.conf | grep -v '#' | grep "nameserver" | awk '{print $2}'`if [ "$resolv_domainname" = "" ]; thenreturn 0elsereturn 1fiset +x

<strong>检查gateway</strong>
#!/bin/sh#check gateway#return value: 1: gateway ok 0:gateway errorset -xgateway=`netstat -rn | egrep '^0.0.0.0' | tr -s ' ' | cut -d ' ' -f2`if [ "$gateway" = "" ]; thenreturn 0elsereturn 1fiset +x


<strong>6,检查hosts</strong>
#!/bin/sh#check hosts configuration whether ok #return value: 1:hosts configuration ok 0:hosts configuration errorset -xHOSTNAME=`hostname`#check duplicate line in /etc/hostscheck_dup_line(){if [ -f /etc/hosts ];thendup_line=`cat /etc/hosts | egrep -v '^(#|$)' | uniq -d`if [ "$dup_line" = "" ];thenecho "have no dupline"return 1elsereturn 0fielsereturn 0fi}#Check /etc/hosts contains an entry for this server namecheck_server_ip(){if [ -f /etc/hosts ];thenhost_name_ip=`cat /etc/hosts | egrep -v '^(#|$|::1|localhost)' | grep ${HOSTNAME}`if [ "$host_name_ip" = "" ];thenecho "No entry for the server name"return 0elsereturn 1fielsereturn 0fi}#Check server hostname mappingcheck_host_map(){if [ -f /etc/hosts ];thenmap=`cat /etc/hosts | egrep -v '^(#|$)' | egrep '(localhost|::1)' | grep ${HOSTNAME}`if [ "$map" = "" ];thenecho "this server hostname is not mapped to a local address" fifi}check_dup_linecheck_host_mapcheck_server_ipif [ $? = 1 ];thenreturn 1fiset +x
<strong>检查等待链接数</strong>
#!/bin/sh#check connection wait num#return value : 1: connection wait ok 0:connection wait errorset -xconnect_num=`netstat -an | grep WAIT | wc -l | awk '{ print $1 }'`if [ $connect_num -ge 100 ];thenecho "connect wait num error"return 0elsereturn 1fiset +x

修复网络

修复NIC
#!/bin/shset -x#repair NIC#We can do this only.interface=`mii-tool | grep "eth" | sed 's/:.*$//g'`ifconfig $interface downifconfig $interface upset +x
修复dhcp
#!/bin/shset -x#repair NICinterface=`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 ipdhclient $interface  #renew ip/etc/init.d/networking restartset +x
修复hosts
#!/bin/shset -x#repair hosts configuration #cp /usr/share/nfw/hosts /etc/  -rf hostname=`hostname`echo "127.0.0.1       localhost" > /etc/hostsecho "127.0.0.1       $hostname" >> /etc/hostsecho "::1     ip6-localhost ip6-loopback" >> /etc/hostsset +x

#hosts127.0.0.1localhost













0 0
原创粉丝点击