欢迎移步博主CSDN:CSDN博客

linux下利用shell脚本测试网卡是否连通

话不多说,脚本奉上:

#!/bin/bash

#遍历27至28网段,查询是否能ping通该网段
for a in {27..28} 
do
# "100%"在ping的输出结果中(证明未ping该网段)
if ping -w 1 -c 3 172.$a.0.1 | grep "100%" >/dev/null  
#未ping通则重启网卡,并重新添加路由表
then
    echo "172.$a.0.1 is Not reachable"
    echo "starting ifconfig..."
    # wlx200db0339e03 为你的网卡名,不懂的请输入ifconfig查询
    #停止网卡
    sudo ifconfig wlx200db0339e03 down
    #启动网卡
    sudo ifconfig wlx200db0339e03 up
    #搜索无线网
    iwlist scanning
    #连接无线网
    sudo iw dev wlx200db0339e03 connect 你所要连接的wifi名称

    #释放获取的ip地址
    sudo dhclient wlx200db0339e03 -r
    #重新获取ip地址
    sudo dhclient wlx200db0339e03
    #将路由表写入iptables.up.rules中
    #sudo iptables-restore < /etc/init.d/iptables.up.rules
    
    #添加28和30网段,让其走172.27.0.1网关
    sudo route add -net 172.28.0.0/16 gw 172.27.0.1
    sudo route add -net 172.30.0.0/16 gw 172.27.0.1

    echo "ending ifconfig..."
#ping通则不作处理
else
    echo "172.$a.0.1 is reachable"
fi
done

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

最后修改:2020 年 03 月 14 日 12 : 38 PM
如果觉得我的文章对你有用,请随意赞赏