Ciao, torno nuovamente sull'argomento postando un nuovo script per accendere e spegnere il wi-fi senza accedere all'interfaccia del pannello di controllo da browser.
Ho perfezionato quanto scritto nel post precedente, infatti non occorrono componenti aggiuntivi per firefox o altri software, basta:
1) copiare il codice seguente in un file di testo con estensione .vbs, per es.: wireless.vbs
2) mettere i valori giusti alle due costanti iniziali IP_DEL_ROUTER_ALICE e PASSWORD_DEL_ROUTER_ALICE
Controllare il wireless del router eseguendo lo script vbs con doppio clic
(per esperti: lo script prevede anche gli argomenti startup e shutdown per esecuzioni automatiche accensione/spegnimento pc)
Questo script è testato solo per Alice Gate 2 plus Wifi versione 4.5.2 e Windows Xp
'Wireless control
'modificare i valori delle due costanti qui
Const IP_DEL_ROUTER_ALICE = "192.168.1.1"
Const PASSWORD_DEL_ROUTER_ALICE = "la mia password"
'fine modifiche
Dim ExecTime, Browser
Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.Count = 0 Then
ExecTime = "poweredon"
Else
ExecTime = WScript.Arguments(0)
End If
If ExecTime = "startup" Then
'startup: aspetta 1 minuto dal boot
WaitAtLeast(60)
End If
CheckRouterState(ExecTime)
Wlanstat = CheckWlanStat
If Wlanstat = "running" Then
'caso di wireless on
If (ExecTime = "startup" or ExecTime = "poweredon") Then
'startup o durante il lavoro: chiede ed eventualmente spegne
Msg = MsgBox("Lasciare attivato il Wireless?",4100,"Controllo WLan")
If Msg = 6 Then
Wscript.Quit(0)
Else
Wireless("off")
End If
'altrimenti significa che ExecTime = "shutdown", quindi lasciamo acceso
End If
ElseIf Wlanstat = "disabled" Then
'caso di wireless off
If ExecTime = "shutdown" Then
'in chiusura riaccende
Wireless("on")
ElseIf ExecTime = "startup" Then
'wireless spento all'accensione pc: avvisa di speghere il pc prima del router ed esce
Msg = WshShell.Popup("Il Wireless era disattivato. In chiusura ricordati che" & VbCrLf & "va spento prima il computer e poi il router",10,"Controllo WLan",4096)
Wscript.Quit(0)
Else
'altrimenti chiede ed eventualmente attiva
Msg = MsgBox("Attivare il Wireless?",4100,"Controllo WLan")
If Msg = 6 Then
Wireless("on")
End If
End If
ElseIf Wlanstat = "undefined" Then
'problemi
Msg = WshShell.Popup("Non riesco a controllare lo stato Wireless dell'apparecchio",10,"Controllo WLan",4096)
End If
Set WshShell = Nothing
Function CheckWlanStat
'restituisce stato wireless
For attempts = 1 to 3
BrowserIstance
BrowserRouterLogin
CheckWlanStat = BrowserWlanStatusResponse
Browser.Quit
If CheckWlanStat <> "undefined" Then Exit For
Next
End Function
Sub Wireless(byval switch)
'accende e spegne Wireless con macro su Internet explorer
'max tre tentativi
For attempts = 1 to 3
BrowserIstance
BrowserRouterLogin
Navigate "http://" & IP_DEL_ROUTER_ALICE & "/admin.cgi?active_page=9133"
If switch = "on" Then
Click "button", "Attiva"
Else
Click "button", "Configura Rete Wi-Fi"
Click "radio", "radio_interface_active_0"
Click "button", "Salva"
Click "button", "Avanti >"
End If
ResultWlanStat = BrowserWlanStatusResponse
Navigate "http://" & IP_DEL_ROUTER_ALICE & "/"
Click "img", "img33"
Browser.Quit
If (switch = "on" and ResultWlanStat = "running") or (switch = "off" and ResultWlanStat = "disabled") Then
Exit Sub
End If
Next
Msg = MsgBox("Il controllo del Wireless non è andato a buon fine",4096,"Controllo WLan")
End Sub
Sub BrowserIstance
'ritorna ie pronto, se startup istanzia e chiude ie n volte prima di tornare indietro
If ExecTime = "startup" Then
attempts = 2
Else
attempts = 1
End If
For i = 1 to attempts
Set Browser = CreateObject("InternetExplorer.Application")
with Browser
.Visible = false
.ToolBar = false
.StatusBar = false
.Height = 400
.Width = 600
.Navigate "http://" & IP_DEL_ROUTER_ALICE & "/"
End with
BrowserPause
If i < attempts Then Browser.Quit
Next
End Sub
Sub BrowserRouterLogin
'entra nel router con Ie
Navigate "http://" & IP_DEL_ROUTER_ALICE & "/mainFrame.html"
Click "button", "Avanti >"
WritePassword
Click "button", "Accedi"
End Sub
Function BrowserWlanStatusResponse
'controlla wireless con browsing ie nel router
Navigate "http://" & IP_DEL_ROUTER_ALICE & "/admin.cgi?active_page=9133"
BrowserPause
For Each HTMLEle In Browser.Document.getElementsByTagName("td")
str = HTMLEle.InnerHtml
If str = "Abilitato" Then
BrowserWlanStatusResponse = "running"
Exit Function
ElseIf inStr (str, "Wi-Fi non è attiva") Then
BrowserWlanStatusResponse = "disabled"
Exit Function
End If
Next
BrowserWlanStatusResponse = "undefined"
End Function
Sub Click(byval EleType, byval EleValue)
'clicca oggetti di internet explorer
If EleType = "button" Then
TagName = "INPUT"
TagProperty = "VALUE"
ElseIf EleType = "img" Then
TagName = "IMG"
TagProperty = "NAME"
ElseIf EleType = "radio" Then
TagName = "INPUT"
TagProperty = "ID"
End If
BrowserPause
For Each HTMLEle In Browser.Document.getElementsByTagName( TagName )
str = lcase(HTMLEle.getAttribute( TagProperty ))
If str = lcase(EleValue) Then
HTMLEle.click
IEElementClick = "True"
BrowserPause
exit sub
End if
Next
End Sub
Sub WritePassword
'scrive la password nel form di alice in IE
BrowserPause
For Each HTMLEle In Browser.Document.getElementsByTagName("INPUT")
str = lcase(HTMLEle.getAttribute("name"))
If Left(str, 8) = "password" Then
HTMLEle.setAttribute "value", PASSWORD_DEL_ROUTER_ALICE
IEValueChange = "True"
BrowserPause
exit sub
End if
Next
End Sub
Sub Navigate(byval url)
'naviga e aspetta che non For oIE to NOT be busy
BrowserPause
Browser.Navigate url
BrowserPause
End sub
Sub BrowserPause
'aspetta che ie sia pronto
Do Until Browser.ReadyState = 4 and not Browser.Busy
Loop
End Sub
Sub WaitAtLeast( byVal LagSecs )
'guarda da quanto è acceso il pc ed eventualmente aspetta fino al numero di secondi dichiarato
Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 )
For Each objItem in colItems
UpTimeSecs = DateDiff( "s", _
ParseDat( objItem.LastBootUpTime ), _
ParseDat( objItem.LocalDateTime ) )
Next
If UpTimeSecs < LagSecs Then
Wscript.sleep ( ( LagSecs - UpTimeSecs ) * 1000 )
End If
Set objWMIService = Nothing
Set colItems = Nothing
End Sub
Function ParseDat( ByVal strDate )
'restituisce la data da oggetti di sistema
'Grazie a Rob van der Woude
'http://www.robvanderwoude.com
strYear = Left( strDate, 4 )
strMonth = Mid( strDate, 5, 2 )
strDay = Mid( strDate, 7, 2 )
strDat = strDay & "-" & strMonth & "-" & strYear
strHour = Mid( strDate, 9, 2 ) - strTimeShift
strMins = Mid( strDate, 11, 2 )
strTime = strHour & ":" & strMins
ParseDat = strDat & " " & strTime
End Function
Function PingSite( myWebsite )
' This function checks if a website is running by sending an HTTP request.
' If the website is up, the function returns True, otherwise it returns False.
' Argument: myWebsite [string] in "www.domain.tld" format, without the
' "http://" prefix.
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
Dim intStatus, objHTTP
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
objHTTP.Open "GET", "http://" & myWebsite & "/", False
objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"
On Error Resume Next
objHTTP.Send
intStatus = objHTTP.Status
On Error Goto 0
Set objHTTP = Nothing
If intStatus = 200 Then
PingSite = True
Else
PingSite = False
End If
End Function
Sub CheckRouterState(ExecTime)
'controlla se il router è funzionante ed eventualmente aspetta la sua operatività
'nel caso non sia funzionante e siamo in shutdown esce
If PingSite( IP_DEL_ROUTER_ALICE ) = False Then
If ExecTime = "shutdown" Then
Wscript.Quit(0)
Else
timeout = 0
Msg = WshShell.Popup("Attendo operatività del router per 3 minuti",5,"Controllo WLan",4096)
Do While PingSite( IP_DEL_ROUTER_ALICE ) = False and timeout < 180
WScript.sleep 1000
timeout = timeout + 1
Loop
If timeout = 180 Then Wscript.Quit(0)
ExecTime = "poweredon"
End If
End If
End Sub
Sub Debug (byval dvar)
Wscript.echo dvar
End Sub