ITPub博客

首页 > 自动化运维 > 应用服务器 > windows主机批处理命令检查网卡网络自动重启网卡

windows主机批处理命令检查网卡网络自动重启网卡

应用服务器 作者:strivechao 时间:2019-04-04 08:41:01 1 删除 编辑

最近有台windows服务器主机偶尔会出现网络断掉的情况,网络图标感叹号状态。其他电脑的网络都是正常。

原因分析与处理:

1.可能是服务器主机的ip被人顶掉?这个可能是有的,但应该比较小。

2.系统漏洞导致?

所以,更新了系统漏洞。

3.硬件驱动问题?

重新下载网卡驱动程序,重新安装。

4.网卡硬件问题,那只能换网卡了。现在还无法判断。

鉴于还是有可能出现断网的情况,为此,整理一段检查网络情况的代码,网络无法连接,可自动重启网卡。

代码如下,仅供参考:

@echo off

echo "网卡自动检查重启服务正在进行中......."

echo "停止服务请按 Ctrl+C"


:begin

set "year=%date:~0,4%"

set "month=%date:~5,2%"

set "day=%date:~8,2%"

echo %date% %time% "ping......" >> ping%year%%month%%day%.txt

ping 192.168.1.1 >>ping%year%%month%%day%.txt                        

rem echo %errorlevel%

if %ERRORLEVEL% == 1 goto reboot

goto loop



:reboot

echo %date% %time% "网卡已重新启动" >>errlog.log

echo %date% %time% "网卡停用中...."

netsh interface set interface "本地连接 2" disabled

echo %date% %time% "网卡启动中...."

netsh interface set interface "本地连接 2" enabled

echo %date% %time% "网卡已重新启动...."


:loop

ping 127.0.0.1 -n 60> nul

goto begin


说明:

rem 表示此命令后的字符为解释行,不执行,只是给自己今后查找用的。:: 也可以起到 rem 的注释作用, 而且更简洁有效;

%errorlevel% 返回上次命令的执行结果   不成功返回为1   成功返回为0

当使用if %errorlevel%==值 cmmand 句式时,它含义是:如果返回的错误码值等于值 的时候,将执行cmmand操作。

netsh interface set interface "本地连接 2" enabled

这个本地连接 2 是可以换成某个网卡的,譬如 internet



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25469263/viewspace-2640344/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-06-21

  • 博文量
    108
  • 访问量
    338459