文章摘要
这篇文章介绍了在学生机房中编写VBS脚本以实现断网后强制关闭计算机的功能。作者针对学生企图脱离监控的情况,提出了两种检测方法:一种是通过检测网卡状态,另一种是通过ping指定IP地址检测连接情况。文章详细描述了两种方法的实现步骤,并提到了避免使用dos窗口以防止被发现的注意事项。最终总结了两种方法的优缺点,供用户根据具体情况选择使用。 总结:文章通过编写VBS脚本实现断网强制关机功能,介绍了两种检测方法及其实现细节,并强调了避免被发现的重要性。选择哪种方法取决于具体需求和场景。
受一个朋友委托,编写一段vbs代码实现断网就强行关闭计算机的功能,他说为学生机房上课时使用,上课时总有学生想脱离老师的监视,为此,会拔掉网线或者禁用网卡,所以,弄个vbs脚本检测网卡状态,如果断网马上强行关机。
Dim objWMIService,objShell
Set objWMIService=Getobject(“winmgmts:\\.\root\cimv2”)
Set objShell=CreateObject(“WScript.Shell”)
‘实现实时监测
do while true
Dim objNetworks,objNetwork
Set objNetworks=objWMIService.execQuery(“Select * From Win32_NetworkAdapter where NetConnectionID=’本地连接'”)
For Each objNetwork In objNetworks
if objNetwork.NetConnectionStatus<>2 then
‘objShell.run “shutdown -s -f -t 30 -c ” & chr(34) & “由于计算机网络断开,机器即将关闭” & chr(34)
msgbox “网络已断开”
exit for
end if
Set objWMIService=Getobject(“winmgmts:\\.\root\cimv2”)
Set objShell=CreateObject(“WScript.Shell”)
‘实现实时监测
do while true
Dim objNetworks,objNetwork
Set objNetworks=objWMIService.execQuery(“Select * From Win32_NetworkAdapter where NetConnectionID=’本地连接'”)
For Each objNetwork In objNetworks
if objNetwork.NetConnectionStatus<>2 then
‘objShell.run “shutdown -s -f -t 30 -c ” & chr(34) & “由于计算机网络断开,机器即将关闭” & chr(34)
msgbox “网络已断开”
exit for
end if
Next
set objNetworks=nothing
‘延时10秒
WScript.sleep 1000*10
Loop
注:方法一是通过检测网卡的状态来进行相应的操作。
strIP=”192.168.1.1″
‘实时监测
do while true
Set objShell=CreateObject(“WScript.Shell”)
If Not IsOnline_1(strIP) Then
? objShell.run “shutdown -s -f -t 30 -c ” & chr(34) & “机器即将关闭” & chr(34)?
wscript.quit
End If
wscript.sleep 1000*10
loop
‘实时监测
do while true
Set objShell=CreateObject(“WScript.Shell”)
If Not IsOnline_1(strIP) Then
? objShell.run “shutdown -s -f -t 30 -c ” & chr(34) & “机器即将关闭” & chr(34)?
wscript.quit
End If
wscript.sleep 1000*10
loop
=========此段函数仅供参考,不会在主程序中调用===========
通过dos窗口的方式ping,但是,屏幕上会出现黑色dos窗口,每运行一次下面的函数都会弹出一次dos和色窗口,很快就会被人发现。
Function IsOnline(strComputer)
? IsOnline=false
? strCommand=”%comspec% /c ping -n 2 -w 500 ” & strComputer & “”
? Set objExecObject=objShell.Exec(strCommand)
? Do While Not objExecObject.StdOut.AtEndOfStream
strText=objExecObject.StdOut.ReadAll()
If Instr(strText, “Reply”) > 0 Then
IsOnline=true
End If
? Loop
End Function
? IsOnline=false
? strCommand=”%comspec% /c ping -n 2 -w 500 ” & strComputer & “”
? Set objExecObject=objShell.Exec(strCommand)
? Do While Not objExecObject.StdOut.AtEndOfStream
strText=objExecObject.StdOut.ReadAll()
If Instr(strText, “Reply”) > 0 Then
IsOnline=true
End If
? Loop
End Function
纯vbs进行ping操作,这样不会弹出dos窗口,不会被发现。
Function IsOnline_1(URLstr)
? IsOnline_1=false
strComputer=”.”
Set objWMIService=GetObject(“winmgmts:{impersonationLevel=impersonate}!\” & strComputer & “\root\cimv2”)
Set colPings=objWMIService.ExecQuery (“Select * From Win32_PingStatus where Address='” & URLstr & “‘”)
? For Each objPing in colPings
? if instr(objPing.ProtocolAddress,URLstr)>0 then
? IsOnline_1=true
? exit for
? end if
? Next
End Function
? IsOnline_1=false
strComputer=”.”
Set objWMIService=GetObject(“winmgmts:{impersonationLevel=impersonate}!\” & strComputer & “\root\cimv2”)
Set colPings=objWMIService.ExecQuery (“Select * From Win32_PingStatus where Address='” & URLstr & “‘”)
? For Each objPing in colPings
? if instr(objPing.ProtocolAddress,URLstr)>0 then
? IsOnline_1=true
? exit for
? end if
? Next
End Function
注:方法二是通过ping一个指定的IP地址,用于检测网卡的运行状态,如果网卡无法通讯或者通讯失败这样就无法返回Ping的结果,根据这个结果判断是否进行强制关机操作。由此,还有个“意外收获”那就是,当断掉指定的IP地址连接后,所有机器会自动关闭计算机,因此,此段程序还可以有别的用途,自己想吧!!!
总结:以上两种方法都是为了检测网卡的运行状态才进行相应的操作的,只是实现方法完全不同,这你就要根据情况自行选择了!!!
您可能感兴趣的文章:VBS 断网后自动关机30秒后
© 版权声明
文章版权归作者所有,未经允许请勿转载。



