Skip navigation.

digital-nation

Blog-note d'un informaticien procrastinate...

Posts tagged with "passerelle par défaut"

Passerelle par défaut

, , ,

Il est parfois utile d'avoir la passerelle par défaut, que ce soit pour une installation logicielle, matérielle, ou tout autre objectif.

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 :D
December 2009
M T W T F S S
November 2009January 2010
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31