shell查看并修复网络连接


1.  shell监控网卡状态,故障时自动重启网卡 http://blog.slogra.com/post-425.html

 cat fix_eth0.sh

复制代码
#!/bin/bash

check_and_fix_eth0_link()
{
date="`date '+%Y-%m-%d %H:%M:%S'`"

# step3 : if have ip, ping $dns
        lost_rate=`ping -c 8 -w 8 $dns -I $eth0 | grep 'packet loss' \
                                                | awk -F'packet loss' '{ print $1 }' \
                                                | awk '{ print $NF }' | sed 's/%//g'`

# step4: if ping dns failed, fix the link of $eth0
        if [ $lost_rate -eq 0 ]
        then
                echo "network_ok $date $dns $dev" >>/root/network_ok.log
        elif [ $lost_rate -le 100 ]
        then
                echo "network_error $date $dns $dev" >>/root/network_error.log
                ifconfig $usb0 down
                sleep 1
                ifconfig $usb0 up
        fi
}

eth0="eth0"
usb0="usb0"
dns="61.139.2.69"

touch /root/network_ok.log
touch /root/network_error.log

while [ 1 ]
do
# step 1 : test if $eth0 have ip
    ipaddr=` /sbin/ifconfig $eth0 | grep "inet addr" | awk '{ print $2}' | awk -F: '{print $2}' `
    if [ ! -n "$ipaddr" ]
    then
# step2 : if have no ip, reset the $eth0, maybe the "udhcpc -i $eth0 ?"
            echo "    $0: NO IP Addr"
            /etc/init.d/networking restart
    else
            check_and_fix_eth0_link;
    fi
    sleep 10
done
复制代码

 

posted @   oucaijun  阅读(957)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2014-11-14 c++, 虚基派生 : 共同基类产生的二义性的解决办法
2014-11-14 c++,命名空间(namespace)
下载TeamViewer完整版 下载TeamViewer
点击右上角即可分享
微信分享提示