网络不通时自动重启网卡的脚本

近期有台AD的网卡经常死掉,所以考虑用脚本监控一旦网卡死掉之后重启网卡.....
找了个VBS脚本是手工启动网卡的.....
保存文件为ReloadNetcard.vbs

[php]
Const ssfCONTROLS = 3
sConnectionName = "本地连接"  '可改成需要控制的连接名称,如"无线网络连接"、"本地连接 2"等
sEnableVerb = "启用(&A)"
sDisableVerb = "停用(&B)"
set wmi = GetObject("winmgmts:")
set colSystem = wmi.ExecQuery("select * from Win32_OperatingSystem")
for each objSystem in colSystem
if Instr(UCase(objSystem.Caption),UCase("Windows XP")) then  '如果系统是WinXP
sNetConnections = "网络连接"
sDisableVerb = "停用(&B)"
else  '如果系统是Win2003
sNetConnections = "网络连接"
sDisableVerb = "禁用(&B)"
end if
next
set shellApp = createobject("shell.application")
set oControlPanel = shellApp.Namespace(ssfCONTROLS)
set oNetConnections = nothing
for each folderitem in oControlPanel.items
if folderitem.name  = sNetConnections then
set oNetConnections = folderitem.getfolder
exit for
end if
next
if oNetConnections is nothing then
' WScript.Echo "控制面板中未找到" & sNetConnections & "文件夹"
' wscript.quit
end if
set oLanConnection = nothing
for each folderitem in oNetConnections.items
'WScript.Echo folderitem.name
if lcase(folderitem.name)  = lcase(sConnectionName) then
set oLanConnection = folderitem
exit for
end if
next
if oLanConnection is nothing then
' WScript.Echo  sNetConnections & "中未找到'" & sConnectionName & "'项目"
' wscript.Quit
end if
bEnabled = true
set oEnableVerb = nothing
set oDisableVerb = nothing
s = "Verbs: " & vbcrlf
for each verb in oLanConnection.verbs
'WScript.Echo verb.name
's = s & vbcrlf & verb.name
if verb.name = sEnableVerb then
set oEnableVerb = verb
bEnabled = false
end if
if verb.name = sDisableVerb then
set oDisableVerb = verb
end if
next
'debugging displays left just in case...
'
'msgbox s ': wscript.quit
'msgbox "Enabled: " & bEnabled ': wscript.quit
'not sure why, but invokeverb always seemed to work
'for enable but not disable.
'
'saving a reference to the appropriate verb object
'and calling the DoIt method always seems to work.
'
if bEnabled then
'  oLanConnection.invokeverb sDisableVerb
'WScript.echo sConnectionName & " 当前状态:已启用"
oDisableVerb.DoIt
else
'  oLanConnection.invokeverb sEnableVerb
'WScript.echo sConnectionName & " 当前状态:已禁用"
oEnableVerb.DoIt
end if
'adjust the sleep duration below as needed...
'
'if you let the oLanConnection go out of scope
'and be destroyed too soon, the action of the verb
'may not take...
'
wscript.sleep 1000
set oNetConnections = nothing
set oLanConnection = nothing
set oEnableVerb = nothing
set oDisableVerb = nothing
[/php]

注:1、红色部分一定要根据实际情况填写连接名称;
2、蓝色部分在脚本调试时相当有用,但调试结束后记得一定要注释掉.......

这个脚本只是手工启动或停止的脚本而已

再来个批处理RebootNetCard.bat

[php]

@echo off
echo "网卡自动重启服务正在进行中......."
echo "停止服务请按 Ctrl+C"

:begin
ping 192.168.1.41 >ping.txt
rem echo %errorlevel%
if %ERRORLEVEL% == 1 goto ping2
goto loop

:ping2
ping 192.168.1.42>ping.txt
rem echo %errorlevel%
if %ERRORLEVEL% == 1 goto reboot
goto loop

:reboot
echo %date% %time% "网卡已重新启动" >>errlog.log
echo %date% %time% "网卡停用中...."
wscript ReloadNetcard.vbs
wscript ReloadNetcard.vbs
echo %date% %time% "网卡已重新启动...."
goto loop

:loop
sleep 60
goto begin

[/php]

有了这个BAT就可以自动监控网卡了哦,我定的时间间隔是60秒
说明:
首先用ping 其他主机的方式来判断网络的链接状态
然后要确认是否是本机网络问题,所以ping两台机器,如果都不通,那基本确定是本机的问题了;
再直接调用两次ReloadNetcard.vbs(一次停一次启动)这样就算是一个完整的重启的过程

在2003 Server默认是没有sleep.exe程序的,附件扩展名直接改成EXE就可以使用
我将vbs和bat还有sleep程序放在一个文件夹里面,CMD运行bat就搞定了

点击这里下载文件: sleep.exe

还没有评论,快来抢沙发!

发表评论

  • 😉
  • 😐
  • 😡
  • 😈
  • 🙂
  • 😯
  • 🙁
  • 🙄
  • 😛
  • 😳
  • 😮
  • emoji-mrgree
  • 😆
  • 💡
  • 😀
  • 👿
  • 😥
  • 😎
  • ➡
  • 😕
  • ❓
  • ❗
  • 69 queries in 0.424 seconds