Jump to content
Sign in to follow this  
magicboy

Find Default Gateway

Recommended Posts

Does anyone have a better solution to this problem?

I am trying to get the default gateway for the computer that the script is run on. The only solution that I have found is to run the command line below and save the information to a file.

c:\netsh interface ip show address

Configuration for interface "Main"
    DHCP enabled:                        No
    IP Address:                        10.24.29.9
    SubnetMask:                        255.0.0.0
    Default Gateway:                      10.24.35.70
    GatewayMetric:                      1
    InterfaceMetric:                      0

All I want to do is see if there is a default gateway and what it is.

Share this post


Link to post
Share on other sites

At a command prompt, you can enter:

ipconfig > c:\ipconfig.txt

Then enter:

find "Gateway" c:\ipconfig.txt

That should display only:

Default Gateway . . . . . : 111.111.111.111

I'm not sure if that will help you any. It's just the first thought I had on your situtation. Maybe there is some way you can use AutoIt to automate that.

Hope this helps,

Ian

Edit: New Thought, this works even better. Just enter:

ipconfig | find "Gateway"

Should have the same results as above, just less work.

Edited by ioliver

"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Share this post


Link to post
Share on other sites

@ioliver

Thanks for the info. I had completly forgot about "|find" I will use your idea and then use it to find the ip of the gateway. Thanls

c:\ipconfig |find "10.24.35.70" > gateway.txt

Again thanks

At a command prompt, you can enter:

ipconfig > c:\ipconfig.txt

Then enter:

find "Gateway" c:\ipconfig.txt

That should display only:

     Default Gateway . . . . . : 111.111.111.111

I'm not sure if that will help you any.  It's just the first thought I had on your situtation.  Maybe there is some way you can use AutoIt to automate that.

Hope this helps,

Ian

Edit: New Thought, this works even better.  Just enter:

ipconfig | find "Gateway"

Should have the same results as above, just less work.

<{POST_SNAPBACK}>

Edited by magicboy

Share this post


Link to post
Share on other sites

Hello,

Maybe useful when you have multiple network adapters in your computer:

Dim $Gateway[50]    ; Array of gateways
$RootKey="HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
$GatewayCounter=0   ; Number of gateways found
$InterfaceNumber = 0    ; Number of interfaces (do not have to be physical adapters)


do          ; Do for each interface in registry..

  $InterfaceNumber = $InterfaceNumber + 1   

  $Interface=RegEnumKey($RootKey, $InterfaceNumber) ; Get an interface key

  if not @error then            ; An interface found?

; Get the DHCP assigned gateways
    $GatewayCounter = GetArrayFromRegistry($RootKey & $Interface ,"DhcpDefaultGateWay", $GateWay, $GatewayCounter)

; Get any fixes assigned gateways
    $GatewayCounter = GetArrayFromRegistry($RootKey & $Interface ,"DefaultGateWay", $GateWay, $GatewayCounter)
  endif

until $Interface=""



; These lines are for displaying purposes only.

$DisplayString = ""

For $Counter = 0 to $GatewayCounter-1
    $DisplayString = $DisplayString & $GateWay[$Counter] & @CRLF
next

Msgbox (0,"Gateways","You have " & $InterfaceNumber & " interface(s)" & @CRLF & _
     "and " & $GatewayCounter & " gateway(s)." & @CRLF & @CRLF & _
     "These are your current gateways:" & @CRLF & $DisplayString )

exit



Func GetArrayFromRegistry($RegKey, $RegValue, ByRef $Array,ByRef $Position)

; Retrieves an REG_MULTI_SZ value from the given registry value
; And copies it into the given array from the given index $Position.
; The function skips empty strings.
;
; Returns: Number of non-empty strings found

Dim $TempArray[100]
Dim $TempCounter

 $TempArray=StringSplit(RegRead($RegKey,$RegValue),"@CRLF"); Get the value in a temporary array
 for $TempCounter = 1 to $TempArray[0]          ; For each string value in the REG_MULTI_SZ
    if $TempArray[$TempCounter] <> "" then      ; Not an empty string?
        $Array[$Position]=$TempArray[$TempCounter]; Place it in target array
        $Position = $Position + 1       ; Increase position in target array
    endif
 next

 return $Position

Endfunc

The same code could also be modified to find DNS-servers, WINS servers, etc. for each adapter.

Regards,

-Sven

Share this post


Link to post
Share on other sites

Try the following code. It's basically IPCONFIG with a window.

#include <GuiConstants.au3>
#include <File.au3>

Ask()

Func Ask()
GUICreate("IP Config",400,500) ; will create a dialog box that when displayed is centered

Opt("GUICoordMode", 1)
GUISetFont(12, 800,4,"Arial")
GUICtrlCreateLabel ("IP Config Options",  125, 20, 200)
GUISetFont(10, 800,0,"Arial")
GUICtrlCreateLabel ("Display",  160, 70, 200)
GUICtrlCreateLabel ("Update",  162, 140, 200)
GUISetFont(9,400,0,"")

$Sel1 = GUICtrlCreateRadio("Simple IP Info", 40, 100, 120, 20)
GUICtrlSetState(-1,$GUI_CHECKED)
$Sel2 = GUICtrlCreateRadio("All IP Info", 250, 100, 120, 20)
$Sel3 = GUICtrlCreateRadio("Renew IP", 40, 170, 120, 20)
$Sel4 = GUICtrlCreateRadio("Release IP", 250, 170, 120, 20)
$Sel5 = GUICtrlCreateRadio("Flush DNS", 40, 200, 120, 20)
$Sel6 = GUICtrlCreateRadio("Re-Register IP/DNS", 250, 200, 120, 20)
$ok = GUICtrlCreateButton("OK", 100, 400, 80)
$Cancel = GUICtrlCreateButton("Cancel/Exit", 200,400,80)
GUISetState()    ; will display an  dialog box with 2 button
; Run the GUI until the dialog is closed
While 1
  $msg = GUIGetMsg()
  Select
     Case $msg = -3
        Exit
     Case $msg = $ok
        ExitLoop
     Case $msg = $Cancel
        Exit
  EndSelect
Wend

If GUIRead($Sel1) = 1 Then $Sel = ""
If GUIRead($Sel2) = 1 Then $Sel = "/ALL"
If GUIRead($Sel3) = 1 Then $Sel = "/RENEW"
If GUIRead($Sel4) = 1 Then $Sel = "/RELEASE"
If GUIRead($Sel5) = 1 Then $Sel = "/FLUSHDNS"
If GUIRead($Sel6) = 1 Then $Sel = "/REREGISTERDNS"

GuiDelete()

GetInfo($Sel)
Ask()
EndFunc

Func GetInfo($Sel)
Dim $temp, $arr
;RunWait(@ComSpec & " /c " & "IPCONFIG.EXE /all > IPINFO.TXT", @MyDocumentsDir, @SW_HIDE)
RunWait(@ComSpec & " /c " & "IPCONFIG.EXE " & $Sel & " > " & "IPINFO.TXT", @MyDocumentsDir, @SW_HIDE)
_FileReadToArray(@MyDocumentsDir & "\IPINFO.TXT", $temp )

For $n = 1 to $temp[0]

   $arr = $arr & StringStripWS($temp[$n],4)

Next

MsgBox(0, "IP Configuration Information", "Logged User:" & @UserName & @LF & $arr & @LF & @LF & @LF)
RunWait(@ComSpec & " /c " & "DEL.EXE IPINFO.TXT", @MyDocumentsDir, @SW_HIDE)
EndFunc

RocTx

Share this post


Link to post
Share on other sites

@SvenP

Thanks for the great code. It is way more that I was first looking for but now the think I will change my progect and make it even better.

magicboy

Hello,

Maybe useful when you have multiple network adapters in your computer:

Dim $Gateway[50]    ; Array of gateways
$RootKey="HKLM\System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
$GatewayCounter=0    ; Number of gateways found
$InterfaceNumber = 0    ; Number of interfaces (do not have to be physical adapters)
do             ; Do for each interface in registry..

  $InterfaceNumber = $InterfaceNumber + 1    

  $Interface=RegEnumKey($RootKey, $InterfaceNumber); Get an interface key

  if not @error then            ; An interface found?

; Get the DHCP assigned gateways
    $GatewayCounter = GetArrayFromRegistry($RootKey & $Interface ,"DhcpDefaultGateWay", $GateWay, $GatewayCounter)

; Get any fixes assigned gateways
    $GatewayCounter = GetArrayFromRegistry($RootKey & $Interface ,"DefaultGateWay", $GateWay, $GatewayCounter)
  endif

until $Interface=""
; These lines are for displaying purposes only.

$DisplayString = ""

For $Counter = 0 to $GatewayCounter-1
    $DisplayString = $DisplayString & $GateWay[$Counter] & @CRLF
next

Msgbox (0,"Gateways","You have " & $InterfaceNumber & " interface(s)" & @CRLF & _
     "and " & $GatewayCounter & " gateway(s)." & @CRLF & @CRLF & _
     "These are your current gateways:" & @CRLF & $DisplayString )

exit
Func GetArrayFromRegistry($RegKey, $RegValue, ByRef $Array,ByRef $Position)

; Retrieves an REG_MULTI_SZ value from the given registry value
; And copies it into the given array from the given index $Position.
; The function skips empty strings.
;
; Returns: Number of non-empty strings found

Dim $TempArray[100]
Dim $TempCounter

 $TempArray=StringSplit(RegRead($RegKey,$RegValue),"@CRLF"); Get the value in a temporary array
 for $TempCounter = 1 to $TempArray[0]            ; For each string value in the REG_MULTI_SZ
    if $TempArray[$TempCounter] <> "" then        ; Not an empty string?
        $Array[$Position]=$TempArray[$TempCounter]; Place it in target array
        $Position = $Position + 1        ; Increase position in target array
    endif
 next

 return $Position

Endfunc

The same code could also be modified to find DNS-servers, WINS servers, etc. for each adapter.

Regards,

-Sven

<{POST_SNAPBACK}>

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...