Linux restarts Nic script based on ping results

Source: Internet
Author: User

This procedure to solve the CentOS system in some cases, the network card frequently failure caused by the Internet and restart can be normal after the internet phenomenon.

This program automatically obtains the gateway address of the ETH0 network card, pings the gateway every 5 minutes, and restarts the NIC command according to the feedback results.

After the detection is complete, the test results are displayed on the screen and the logs are saved in the/var/log/directory

The author is new to Linux, some commands may be wrong, please guide the correction

#本程序为解决centos系统某些情况下网卡频繁故障导致不能上网而重启以后可以正常上网的现象. #本程序自动获取eth0网卡的网关地址, Ping the gateway every 5 minutes, restart the NIC command according to the feedback results, #检测结束以后, the test results are displayed on the screen, and the logs are saved in the/var/log/directory # author for Linux novice, some commands may be wrong, Please instruct correction #by:fenei#qq:407603129 mail:[email protected] #Ver  2015.4.17 #!/bin/shclear# empty screen time = ' date+%y%m%d ' #查询系统当前日期echo  | route -n | grep eth0 | grep ug  | awk  ' {print $2} ' >/tmp/gateway.listcat /tmp/gateway.list | while  read gateway# Gets the gateway information for the NIC Eth0 to detect the gateway on-off condition in the next steps doecho  "**************** Please wait a moment, the network status is being detected ******************* * * "if ! ping -c 5   $gateway  > /tmp/ping.log 2>&1# Logging the Gateway Ping information to/tmp/ping.log # ping -c 5  indicates that the script runs ping the Gateway 5 times thenecho  "********************* The ">> /var/log/pingerror.logecho " Time Is "$" ( Date +%y%M Month%d day%h:%m:%s ),  "network failure, will be a network card restart operation!" echo  "Time is Now" $ (date&nbsP;+%y%M Month%d day%h:%m:%s ),  "network failure, will be a network card restart operation!"  >> /var/log/pingerror.logtail -3  /tmp/ping.log >> /var/log/ pingerror.log# Add the current timestamp to the error message and log it to the error log ifdown eth0ifup eth0 echo  "The network adapter is successfully restarted, check that the networks are available!" "#重启网卡命令elseecho  " ************************************************************************** ">>  /var/log/pingok.logecho  "Now Time is"  $ (date +%y year%M Month%d%h:%m:%s ), "Network link is OK, thank you for using!" echo  "Now Time is"  $ (date +%y year%M Month%d%h:%m:%s ), "Network link is OK, thank you for using!"  >> /var/log/pingok.logtail -3 /tmp/ping.log >> /var/log/ pingok.logfirm -rf /tmp/ping.logrm -rf /tmp/gateway.list# removal of temporary files produced during program run done

This article is from the "www.fenei.com" blog, make sure to keep this source http://453861.blog.51cto.com/443861/1633875

Linux restarts Nic script based on ping results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.