Tempo fa ho pubblicato un articolo nel quale si fa uso del comando ping per capire se un computer è acceso oppure no.
Purtroppo il comando ping di windows, almeno nella versione in mio possesso, non ha una gestione degli ERRORLEVEL in uscita; in pratica alla fine della esecuzione, restituisce sempre il medesimo valore, sia che il ping abbia successo o meno, rendendo vane le possibilità di impiegarlo all’interno di uno script.
Di seguito presento un breve listato scritto in vbscript che esegue il comando ping e restituisce un codice di errore in modo da poterlo sfruttare all’interno di un file batch dos.
‘######################################
‘ServerPing.vbs ver 1.0
‘di Benintende Vittorio
‘Questo software viene rilasciato sotto
‘licenza GPL e senza garanzia alcuna.
‘######################################
‘Il seguente codice esegue un Ping e tiene
‘traccia dell’output generato per capire
‘l’esito del comando.
‘Poichè quando il comando va a buon fine
‘contiene sempre il TTL del pacchetto
‘inviato, se l’output contiene “TTL=” nella
’sequenza di caratteri allora si presume che
‘il ping abbia raggiunto la destinazione.
‘Di seguito due esempi esplicativi:
‘Esempio 1:
‘Esecuzione di Ping 192.168.0.1 con 32 byte di dati:
‘Risposta da 192.168.1.20: Host di destinazione irraggiungibile.
‘Risposta da 192.168.1.20: Host di destinazione irraggiungibile.
‘Risposta da 192.168.1.20: Host di destinazione irraggiungibile.
‘Richiesta scaduta.
‘Statistiche Ping per 192.168.0.1:
‘ Pacchetti: Trasmessi = 4, Ricevuti = 3, Persi = 1 (25% persi),
‘Tempo approssimativo percorsi andata/ritorno in millisecondi:
‘ Minimo = 0ms, Massimo = 0ms, Medio = 0ms
‘Esempio 2:
‘Esecuzione di Ping 192.168.0.1 con 32 byte di dati:
‘Risposta da 192.168.0.1: byte=32 durata=73ms TTL=64
‘Risposta da 192.168.0.1: byte=32 durata=87ms TTL=64
‘Risposta da 192.168.0.1: byte=32 durata=103ms TTL=64
‘Risposta da 192.168.0.1: byte=32 durata=68ms TTL=64
‘Statistiche Ping per 192.168.0.1:
‘ Pacchetti: Trasmessi = 4, Ricevuti = 4, Persi = 0 (0% persi),
‘Tempo approssimativo percorsi andata/ritorno in millisecondi:
‘ Minimo = 68ms, Massimo = 103ms, Medio = 82ms
option explicit
dim strMyComputer
Dim objExec, objArgs, objShell
Dim strPingResults
dim lngPos
‘command line
Set objArgs = WScript.Arguments
For lngPos = 0 to objArgs.Count – 1
Select Case UCase(objArgs(lngPos))
Case Else
if cbool(len(trim(objArgs(lngPos)))) then strMyComputer=objArgs(lngPos)
End Select
Next
If not cbool(len(Trim(strMyComputer))) then wscript.quit(100)
Set objShell = CreateObject(“WScript.Shell”)
Set objExec = objShell.Exec(“ping -n 2 -w 1000 ” & strMyComputer)
strPingResults = UCase(objExec.StdOut.ReadAll)
wscript.quit(Not CBool(InStr(UCase(strPingResults), ” TTL=”)))
Per pingare l’indirizzo IP 192.168.0.1 da console ad esempio è sufficiente impartire la seguente istruzione:
cscript ServerPing.vbs 192.168.0.1
Ad esempio è possibile utilizzare il precedente script per accedere a condivisioni di rete solo quando queste sono raggiungibili:
::
:: abilita gli shares se i server sono attivi
::
@ECHO OFF
SET LOGFILE=”%APPDATA%\_share.log”
SET DOMAIN=miodominio
SET SERVER1=tizio
SET SERVER2=caio
SET SERVER3=sempronio
ECHO ———- >> %LOGFILE%
DATE /T >> %LOGFILE%
TIME /T >> %LOGFILE%
ECHO Looking for shares at %SERVER1% >> %LOGFILE%
cscript ServerPing.Vbs %SERVER1%.%DOMAIN% >NUL 2>&1
IF ERRORLEVEL 0 GOTO SETSERVER1
GOTO SERVER2
:SETSERVER1
ECHO Try to mount M: Drive (share1 on %SERVER1%) >> %LOGFILE%
NET USE M: \\%SERVER1%.%DOMAIN%\share1 >NUL 2>&1
ECHO Exit with error %ERRORLEVEL% >> %LOGFILE%
ECHO Try to mount L: Drive (share_nascosto$ on %SERVER1%) >> %LOGFILE%
NET USE L: \\%SERVER1%.%DOMAIN%\share_nascosto$ >NUL 2>&1
ECHO Exit with error %ERRORLEVEL% >> %LOGFILE%
ECHO Try to mount N: Drive (foto on %SERVER1%) >> %LOGFILE%
NET USE N: \\%SERVER1%.%DOMAIN%\foto >NUL 2>&1
ECHO Exit with error %ERRORLEVEL% >> %LOGFILE%
:SERVER2
ECHO Looking for shares at %SERVER2% >> %LOGFILE%
cscript ServerPing.Vbs %SERVER2%.%DOMAIN% >NUL 2>&1
IF ERRORLEVEL 0 GOTO SETSERVER2
GOTO SERVER3
:SETSERVER2
ECHO Try to mount O: Drive (share1 on %SERVER2%) >> %LOGFILE%
NET USE O: \\%SERVER2%.%DOMAIN%\share1 >NUL 2>&1
ECHO Exit with error %ERRORLEVEL% >> %LOGFILE%
:SERVER3
IF %COMPUTERNAME%==%SERVER3% GOTO GOEXIT
ECHO Looking for shares at %SERVER3% >> %LOGFILE%
cscript ServerPing.Vbs %SERVER3%.%DOMAIN% >NUL 2>&1
IF ERRORLEVEL 0 GOTO SETSERVER3
GOTO GOEXIT
:SETSERVER3
ECHO Try to mount P: Drive (Share1 on %SERVER3%) >> %LOGFILE%
NET USE P: \\%SERVER3%.%DOMAIN%\share1 >NUL 2>&1
ECHO Exit with error %ERRORLEVEL% >> %LOGFILE%
:GOEXIT
SET LOGFILE=
SET DOMAIN=
SET SERVER1=
SET SERVER2=
SET SERVER3=
Con questo ho finito.
Alla prossima.