Jump to content

Gateway address


cuicui
 Share

Recommended Posts

Hi,

I'd like to get the IP address of the default gateway in order to choose a "close" server to connect to. I'm not a fan of redirecting the output of a command on a file and parsing it, i'd like to avoid file writing/reading. Looking for it in the registry is quite a pain in the ass and using netsh is not recommended because the "name" of the connection is needed and it's not the same on every computer.

I'm almost done but the last digit of the IP address of the gateway is cropped :( I don't get why...

; "Passerelle" is french for "gateway". 
; let's grab the line that contains the gw address
$route = Run(@ComSpec & " /c" & 'route print | find "Passerelle"', "", @SW_HIDE, 2)

; put the line in a variable
$route_output=StdoutRead($route)

; split the address from the text
$gw_tmp=StringSplit($route_output, ":")

; cleaning up
$gw=StringStripWS($gw_tmp[2], 8)
MsgBox(0, "", $gw)

Seems that already in $route_output, the last digit is cropped but if i run route print | find "Passerelle" by hand the output is OK.

Any help is welcomed!

Link to comment
Share on other sites

#include <Array.au3>

$PID = Run("cmd /c ipconfig", "", @SW_HIDE, 2)

Dim $output = ""
While 1
    $output &= StdoutRead($PID)
    If @error Then ExitLoop
WEnd
    
$sResult = StringRegExp($output,"[0-9][0-9]?[0-9]?.[0-9][0-9]?[0-9]?.[0-9][0-9]?[0-9]?.[0-9][0-9]?[0-9]?",3)

$gateway_adress = $sResult[2]

MsgBox(0x40,"Gateway adress", "This is your default gateway adress: " & $gateway_adress)

Link to comment
Share on other sites

A couple of years ago I wrote this UDF which seems to be holding up well:

;==============================================================================
; Function: _NetGetDefaultGateway  ( $sNIC_Description='', $iMatch=3, $iNulGateways=0, $strComputer='localhost' )
;
; Paramaters:
;------------
;   $sNIC_Description - Name of NIC to query.  Default = '' for all active NICs
;
;   $iMatch - How to match the NIC string. Default is 3 (match any part of NIC name)
;       0 = Exact Match
;       1 = Match from the beginning of the NIC name passed (NicName*)
;       2 = Match from the end of the NIC name passed (*NicName)
;       3 = Match any part of NIC name passed (*NicName*)
;
;   $iNulGateways - Allow NUL Gateways. Default = 0
;       0 = Do NOT allow NUL gateways
;       1 = ALLOW NUL gateways
;
;   $strComputer - Computer to query. Default is 'localhost'
;
;
; Returns two dimensional array:
;-------------------------------
;   element [0][0] contains the number of Gateways found
;   element [x][0] contains the description (Name) of the NIC
;   element [x][1] contains the gateway for that NIC
;
;
; Error Returns:
;---------------
;   @error = 1 - WMI Object could not be created
;   @error = 2 - Collection Object could not be created
;   @error = 9 - No Gateways Found
;
; NOTES:
; ------
;   1. A value of -1 can be used instead of default integer values to make it
;      easier to use them.
;
;==============================================================================

Func _NetGetDefaultGateway ( $sNIC_Description='', $iMatch=3, $iNulGateways=0, $strComputer='localhost' )
    If $iMatch = -1 Then
        $iMatch = 3
    EndIf
    If $iNulGateways = -1 Then
        $iNulGateways = 0
    EndIf
    Local Const $wbemFlagReturnImmediately = 0x10
    Local Const $wbemFlagForwardOnly = 0x20
    Local $colItems = ''
    Local $objWMIService = ''
    Local $i=1
    Local $iErrNoWMIObj = 1
    Local $iErrNoColObj = 2
    Local $iErrNoGateways = 9
    Local $sQueryStr = 'SELECT * FROM Win32_NetworkAdapterConfiguration  WHERE (IPEnabled = True'
    Dim $aRet[1][2]
    $aRet[0][0] = 0
    $aRet[0][1] = '[0][0] = Number of NICs found'
    $objWMIService = ObjGet ('winmgmts:\\' & $strComputer & '\root\CIMV2' )
    If NOT IsObj ( $objWMIService ) Then
        SetError ( $iErrNoWMIObj )
        Return
    EndIf

    If $sNIC_Description <> '' Then
        If $iMatch = 4 Then
            $sQueryStr = $sQueryStr & ' AND Description NOT LIKE "%' & $sNIC_Description & '%")'
        Else
            $sQueryStr = $sQueryStr & ' AND Description LIKE "'
            If $iMatch = 2 OR $iMatch = 3 Then
                $sQueryStr = $sQueryStr & '%'
            EndIf
            $sQueryStr = $sQueryStr & $sNIC_Description
            If $iMatch = 1 OR $iMatch = 3 Then
                $sQueryStr = $sQueryStr & '%'
            EndIf
            $sQueryStr = $sQueryStr & '")'
        EndIf
    Else
        $sQueryStr = $sQueryStr & ')'
    EndIf
    $colItems = $objWMIService.ExecQuery ( $sQueryStr, 'WQL', $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If NOT IsObj ( $colItems ) Then
        SetError ( $iErrNoColObj )
        Return
    Else
        For $objItem In $colItems
            If $objItem.DefaultIPGateway(0) OR $iNulGateways Then
                $i = $i + 1
                ReDim $aRet[$i][2]
                $aRet[0][0] = $aRet[0][0] + 1
                $aRet[$i-1][0] = $objItem.Description
                $aRet[$i-1][1] = $objItem.DefaultIPGateway(0)
            EndIf
        Next
    EndIf
    $colItems = ''
    $objWMIService = ''
    If $aRet[0][0] = 0 Then
        SetError ( $iErrNoGateways )
    Else
        SetError ( 0 )
    EndIf
    Return $aRet
EndFunc ; ==> _DefaultGateway
Link to comment
Share on other sites

#include <Array.au3>

$PID = Run("cmd /c ipconfig", "", @SW_HIDE, 2)

Dim $output = ""
While 1
    $output &= StdoutRead($PID)
    If @error Then ExitLoop
WEnd
    
$sResult = StringRegExp($output,"[0-9][0-9]?[0-9]?.[0-9][0-9]?[0-9]?.[0-9][0-9]?[0-9]?.[0-9][0-9]?[0-9]?",3)

$gateway_adress = $sResult[2]

MsgBox(0x40,"Gateway adress", "This is your default gateway adress: " & $gateway_adress)
Thanks but it doesn't works with IPv6 enabled :)

Posted Image

Link to comment
Share on other sites

Finally I isolated the gw address using the "=" and ":" delimiters and giving up the "find" command. RegExp would be a lot cleaner but there is something I don't get with StringRegExp(). I also find some good stuff in this topic (with temp files).

$route = Run(@ComSpec & " /c" & "route print", "", @SW_HIDE, 2)
$route_output=StdoutRead($route)

$gw_tmp1=StringSplit($route_output, ":")
$gw_tmp2=StringSplit($gw_tmp1[3], "=")

$gw=StringStripWS($gw_tmp2[1], 8)

MsgBox(0, "", $gw)

It works with IPv6, but probably not if there are more than one active interface.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...