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

欢迎移步博主小站:白亮吖雅黑丫の小站

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值