Option Explicit
Dim CheckPeriodSuccess, CheckPeriodFailure, CheckTimes, CheckTimesPeriod, oShell, LocalIP, UserName, Password, Client, Times
' =====================================================================
' Данные аутентификации
LocalIP = "192.168.1.1"
UserName = "admin"
Password = "pass"
' Периодичность проверки пинга в мсек. (если подключение есть)
CheckPeriodSuccess = 10000
' Задержка перед повторной проверкой, если роутер был перезагружен
CheckPeriodFailure = 120000
' Кол-во контрольных проверок пинга (пакетов подряд)
CheckTimes = 3
' с интервалом (мсек.)
CheckTimesPeriod = 1000
' Путь к программе-клиенту (например plink.exe)
Client = "telnet.exe"
' =====================================================================
Set oShell = WScript.CreateObject("WScript.Shell")
Do
if CheckRouter() then
' если все ОК
WScript.Sleep CheckPeriodSuccess
else
' если произведена перезагрузка
WScript.Sleep CheckPeriodFailure
end if
Loop
Function CheckRouter()
Dim Connected, ProcessID
Connected = false
Times = CheckTimes
Do
If IsOnline("www.ya.ru") then
Connected = true
elseif IsOnline("8.8.8.8") Then
Connected = true
end if
if not Connected then
if Times > 0 then
Times = Times - 1
WScript.Sleep CheckTimesPeriod
end if
end if
Loop while Times and (not Connected)
if Connected then
CheckRouter = true
else
ProcessID = CreateProcess(Client & " " & LocalIP, 1)
if 0 <> ProcessID then
WScript.Sleep 2000
oShell.AppActivate ProcessID
WScript.Sleep 500
oShell.SendKeys UserName & chr(13)
WScript.Sleep 500
oShell.SendKeys Password & chr(13)
WScript.Sleep 500
oShell.SendKeys "reboot" & chr(13)
WScript.Sleep 500
oShell.SendKeys "^]"
WScript.Sleep 100
oShell.SendKeys "quit" & chr(13)
WScript.Sleep 100
call TerminateByPID (ProcessID)
end if
End If
End Function
Sub TerminateByPID(PID)
Dim oWMISvc, oProcess, colProcesses
Set oWMISvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
set colProcesses = oWMISvc.ExecQuery("Select * from Win32_Process WHERE ProcessId=" & PID)
if colProcesses.Count > 0 then
For each oProcess in colProcesses
call oProcess.Terminate
Next
end if
End Sub
Function IsOnline (Address)
On Error Resume Next
Dim objPing, objStatus
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select Replysize from Win32_PingStatus where address = '" & Address & "'")
For Each objStatus in objPing
IsOnline = not IsNull(objStatus.ReplySize) or IsOnline
Next
End Function
Function CreateProcess(cmd, WindowStyle) 'вернет ProcessID
Dim objWMIService, objStartup, objConfig, objProcess, PID
Const SW_HIDDEN = 0
Const SW_NORMAL = 1
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = WindowStyle
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
call objProcess.Create (cmd, null, objConfig, PID)
if not IsNull (PID) then CreateProcess = PID else CreateProcess = 0
End Function