Passerelle par défaut
Monday, 16. February 2009, 08:25:51
Mais comment déterminer celle-ci ?
Nous pouvons bien entendu le faire en VBS, mais aussi en AutoIt.
Exemple :
' VBS
strComputer = "." 'pour l ordinateur
Set WshShell = WScript.CreateObject("WScript.Shell")
' Set strComputer = WshShell.Environment("VOLATILE").Item("CLIENTNAME") 'pour environnement virtuel
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objAdapter in colAdapters
If Not IsNull(objAdapter.DefaultIPGateway) Then
For i = 0 To UBound(objAdapter.DefaultIPGateway)
WScript.Echo " Default gateway: " & objAdapter.DefaultIPGateway(i)
If objAdapter.DefaultIPGateway(i) = "192.168.1.6" Then
WScript.Echo "Votre Gateway : " & DefaultIPGateway(i)
End If
Next
End If
Next
Cela fonctionne à merveille. L'inconvéniant avec le VBS c'est les structures conditionnelles, et puis pourquoi le faire en VBS alors qu'on peux facilement le faire en AutoIt ?
Exemple :
Func _get_default_gateway()
$output = _get_cmd_output("netstat -rn")
$arr = StringSplit($output, "0.0.0.0", 1)
$arr2 = StringStripWS($arr[3], 1)
$arr2 = StringSplit($arr2, " ")
Return $arr2[1]
EndFunc
Func _get_cmd_output($cmd)
$cmd_timeout = 2000
$pid = Run($cmd,@WorkingDir,@SW_HIDE,2)
$timer = TimerInit()
while (ProcessExists($pid))
sleep(50)
If TimerDiff($timer) > $cmd_timeout Then ProcessClose($pid)
WEnd
$output = StdoutRead($pid)
Return StringStripCR($output)
EndFunc
Pour avoir la valeur, une simple boite de dialogue et le tour est joué :
Dim $i $i = _get_default_gateway() MsgBox(0, "","Votre passerelle : " & $i)
La structure conditionnelle (si plusieurs passerelles sont possible) est faisable hors de la fonction

