Jump to content
Sign in to follow this  
readmedottxt

Obtaining default gateway

Recommended Posts

readmedottxt

I found a few default gateway scripts but many error'ed in Win7 and WinPE.

This script returns the first default gateway found when you do an ipconfig /all based on a network in the 10.* range. You can change it to 192.* etc quite easily.

Hope it's of some use

$i = 0
while 1
    $i = $i + 1
    if $i > 10 Then
        MsgBox(0, "", "Could not get default gateway, aborting ...", 5)
        ExitLoop
    EndIf

    $foo = Run(@ComSpec & ' /c IpConfig.exe /All | Find "Gateway"', @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    $line = ""
    Sleep(1000)
    $line = StdoutRead($foo)

    if StringInStr($line, ": 10.") > 0 Then
        MsgBox(0, "", "Found GW: " & StringMid($line, StringInStr($line, ": 10.") + 2))
        ExitLoop
    EndIf
WEnd
Edited by readmedottxt

Share this post


Link to post
Share on other sites
BrettF

Something slightly different:

; Demonstrates StdoutRead()
#include <Constants.au3>

MsgBox (0, "", _GetDefaultGateway())

Func _GetDefaultGateway()
    Local $foo = Run(@ComSpec & " /c ipconfig /all | Find ""Gateway""", @SystemDir, @SW_HIDE, $STDOUT_CHILD)

    Local $line

    While 1
        $line = StdoutRead($foo)
        If @error Then ExitLoop
        $array = StringRegExp($line, "(?i)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", 3)
        If Not @error Then Return SetError(0, 0, $array[0])
    WEnd
    Return SetError(1, 0, False)
EndFunc   ;==>_GetDefaultGateway
Edited by BrettF

Share this post


Link to post
Share on other sites
ripdad

$array = StringRegExp("132.234.212.1", "(?i)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", 3)

Should be:

$array = StringRegExp($line, "(?i)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", 3)

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
BrettF

Share this post


Link to post
Share on other sites
ripdad

BrettF: Which reminds me. This could also be done with WMI.

Without IpConfig? Where is it?! <grin>


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
BrettF

#include <Array.au3>
$a = _GetDefaultGateway()

_ArrayDisplay ($a)

Func _GetDefaultGateway()
    Local $count = 0, $aRet[$count+1]
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    $colNicConfigs = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 48)
    For $objNicConfig In $colNicConfigs
        For $strIPDefaultIPGateway In $objNicConfig.DefaultIPGateway
            $count += 1
            ReDim $aRet[$count+1]
            $aRet[$count] = $strIPDefaultIPGateway
        Next
    Next
    $aRet[0] = $count
    Return $aRet
EndFunc   ;==>_GetDefaultGateway

Share this post


Link to post
Share on other sites
ripdad

Very nice - thanks


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
Wilenty

Hello :idea:

And script for RAS ip v4 & gateway {PPP/PPPoE/PPPoA/DialUP} works fine from XP to 7

$RasAPI32 = DllOpen("rasapi32.dll")

    $RASCONN = DllStructCreate("dword dwSize;hwnd hRasConn;char szEntryName[256];" & _
                                "char szDeviceType[17]; char szDeviceName[128];" & _
                                "char szPhonebook[260];" & _
                                "dword dwSubEntry;byte guidEntry[16];dword dwFlags;byte luid[8]")

    $Conn = DllStructGetSize($RASCONN)
    DllStructSetData($RASCONN, "dwSize", $Conn)
    $Byte = DllStructCreate("dword")
    DllStructSetData($Byte, 1, $Conn)
    $Conn = DllStructCreate("dword")

    $dwRet = DllCall($RasAPI32, "int", "RasEnumConnections", _
                                        "ptr", DllStructGetPtr($RASCONN), _
                                        "ptr", DllStructGetPtr($Byte), _
                                        "ptr", DllStructGetPtr($Conn))
    If $dwRet[0] Then Exit MsgBox(16,'dll error', $dwRet[0])
    If DllStructGetData($Conn,1) < 1 Then Exit MsgBox(16,'error','no RAS connection!')


    $RASPPPIP = DllStructCreate("DWORD dwSize; DWORD dwError; CHAR szIpAddress[16]; CHAR szServerIpAddress[16];" & _
                                    "DWORD dwOptions; DWORD dwServerOptions")

    DllStructSetData($RASPPPIP, "dwSize", DllStructGetSize($RASPPPIP))
    $buff = DllStructCreate("dword")
    DllStructSetData($buff, 1, DllStructGetSize($RASPPPIP))

    $dwRet = DllCall($RasAPI32, 'int', "RasGetProjectionInfo", "dword", DllStructGetData($RASCONN, "hRasConn"), "int", 0x8021, "ptr", DllStructGetPtr($RASPPPIP), "ptr", DllStructGetPtr($buff) )

    If Not $dwRet[0] Then
        MsgBox( 64,'RAS Info','Conn: ' & DllStructGetData($RASCONN, "szEntryName") & @CRLF & 'IP: ' & DllStructGetData($RASPPPIP, "szIpAddress") & @CRLF & 'Gateway: ' & DllStructGetData($RASPPPIP, "szServerIpAddress") )
    EndIf

    DllClose($RasAPI32)

Greets wilenty.

Edited by Wilenty

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×