Jump to content

Get Default Gateway


Recommended Posts

Helo all, :lmao:

I'm trying to automaticly get the default gateway of computers. I've found lots of sample script which are based on the "ipconfig /all" command, but, there is a big problem : In my case, the software will be "distributed" in 5 or 6 countries. So, making an ipconfig will work in my native language (French), but, if I run the program in Spain, it'll not work because of searching for "Passerelle par défaut" or "Default Gateway" etc etc...

Is there any other solution to get the default gateway of an active connection?? :P

By regedit or something else??

Thanks a lot...

Julien

What????????
Link to comment
Share on other sites

Okay.....

This works, but...

If the computer has two network interfaces....

How can I know which is the good one???? I want the wired one!! If the two cards are enabled (and, I think, it's quite frequent because users don't know how to use their computer), I cannot know which is the good!!

Moreover, macros @IPAddress1 and @IPAddress2 don't always refer to the same card.

E.G : If I only activate my wired card, @IPAddress1 refers to Ethernet connection, and the @IPAddress2 macro returns 0.0.0.0. If my two cards are activated, @IPAdress1 refers to Wireless card, and the second to the wired card!!!!

So, when I do : netstat -rn (equal to "route print"), there are two lines with 0.0.0.0 0.0.0.0 XXX.XXX.XXX.XXX with different gateways...

How can I be sure to choose the good card???

Thanks a lot...

Julien

Edited by Julien.alkaza
What????????
Link to comment
Share on other sites

...How can I know which is the good one???? I want the wired one!!...

In your first post, you stated that you wanted the "default gateway"... as I understand it, this may not be the "wired one". The routing table only shows one "default gateway" no matter how many NICs are active. You should be able to pick up this value no matter the language of the system by parsing from right to left.

Please understand that my knowledge of networking is limited, but it has been my experience that the "default gateway" that shows in the routing table is the one that will be used no matter how many other "default gateways show via IP config /all.

If you dont mind sharing --- what are you going to do with this "default gateway" info?

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Posted Image

Have a look at this little screenshot.....

There are two lines showing a 0.0.0.0 route....

And the last line, "Passerrelle par défaut : ......." is in the OS lang......

Here is my problem...

Posted Image

2nd screenshot... I have two default gateways again. And they depend of the OS Lang...

Thanks for your help....

I have found a solution which is not very good, but it works : In our network (International Entreprise) we have lots of sub-network. Every sub-network has a default gateway with an XXX.XXX.XXX.254 IP address...So, I just replace the last Byte by 254.... But, if a network is not set up like that, it won't work......

Edited by Julien.alkaza
What????????
Link to comment
Share on other sites

I will try using IPCONFIG /ALL My laptop has two network cards 1 nic another one wireless and when Im at work and connect my laptop to the station a this point I have three nic cards. When you run ipconfig /all the card with network connection is the only one that show Default Gateway.

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

@Julien.alkaza,

You might want to chech your links, the screenshots are not coming thru on this end...

[EDIT1 - now they are coming thru...]

@Danny35d,

IPconfig and IPconfig /all should show one or more default gateways for each NIC enabled.

netstat -r shows all of the same "gateways" and then one line with what I think is the default gateway in use.

Surely someone in the forum is a bit of a networking guru. Perhaps WMI is the answer.

@Julien.alkaza,

EDIT2 - you only have one "Default" gateway in the routing table. Will this not work for you? In the case of the screenshots shown - it seems to be the "wired one". Do you need AutoIt code to parse that IP impendent of OS language? Or are you still concerned that it might be the wireless gateway and not the wired one? [i wish knew the value in knowing a non-active gateway.]

BTW, alt-printscreen should put only the active window into the clipboard and not require cropping.

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Sorry for pictures...It was just à extension problem...

My problem is that we have different languages. So parsing for "Passerelle par défaut" or for "Default Gateway" or the same in german, spanish, chinese, Turkish, Hungarian, Bulgarian and others and others is quite hard...

But, never mind, I'll do with my .254 at the end of the IP address!!!

Thanks a lot...

Julien

What????????
Link to comment
Share on other sites

In case you change your mind...

;port routing table to text file
;(or use std in/out in beta AutoIt)
RunWait(@ComSpec & " /c " & 'netstat -r > c:\temp\netstatout.txt')

;Read contents of file
$file1txt = FileRead('c:\temp\netstatout.txt', FileGetSize('c:\temp\netstatout.txt'))

;split to array
$file1txtARRAY = StringSplit($file1txt, @CRLF, 1)

;find/display the line of interest
$cnt = 0
For $i = 1 To $file1txtARRAY[0]
    If StringInStr($file1txtARRAY[$i], "===") <> 0 Then $cnt = $cnt + 1
    If $cnt = 4 Then ExitLoop
Next
MsgBox(0, "", $file1txtARRAY[$i - 1])

;get the length of that line
$len = StringLen($file1txtARRAY[$i - 1])

;find the position of the 3rd dot from the right side
$3rdDOT = StringInStr($file1txtARRAY[$i - 1], ".", 0, -3)

;generate default gateway IP
$IP = StringRight($file1txtARRAY[$i - 1], $len - $3rdDOT + 4)

;strip any leading blanks from the first part of the IP
$IP = StringStripWS($IP, 8)
MsgBox(0, "", $IP)
...or you can do it in AutoIt beta and WMI with one or two lines of code:

http://www.autoitscript.com/forum/index.ph...62entry127962

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

In case you change your mind...

;port routing table to text file
;(or use std in/out in beta AutoIt)
RunWait(@ComSpec & " /c " & 'netstat -r > c:\temp\netstatout.txt')

;Read contents of file
$file1txt = FileRead('c:\temp\netstatout.txt', FileGetSize('c:\temp\netstatout.txt'))

;split to array
$file1txtARRAY = StringSplit($file1txt, @CRLF, 1)

;find/display the line of interest
$cnt = 0
For $i = 1 To $file1txtARRAY[0]
    If StringInStr($file1txtARRAY[$i], "===") <> 0 Then $cnt = $cnt + 1
    If $cnt = 4 Then ExitLoop
Next
MsgBox(0, "", $file1txtARRAY[$i - 1])

;get the length of that line
$len = StringLen($file1txtARRAY[$i - 1])

;find the position of the 3rd dot from the right side
$3rdDOT = StringInStr($file1txtARRAY[$i - 1], ".", 0, -3)

;generate default gateway IP
$IP = StringRight($file1txtARRAY[$i - 1], $len - $3rdDOT + 4)

;strip any leading blanks from the first part of the IP
$IP = StringStripWS($IP, 8)
MsgBox(0, "", $IP)
...or you can do it in AutoIt beta and WMI with one or two lines of code:

http://www.autoitscript.com/forum/index.ph...62entry127962

you can get this value with netsh also... i don't remember the exact sytax, but i know it's possible because on my laptop i have a script to toggle home network or other network for the wireless connection, and it has to change the TCP/IP settings (including the gateway). When i get home (about 4-5 hours from now) i'll look up the syntax if someone else hasn't posted it by then...
Link to comment
Share on other sites

in netstat, if the destination IP does not match any of the routes above, it will forward the packet through the default gateway. The default gateway listed is the one for the system (each NIC can also have a default gateway, as they are on separate networks).

Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Link to comment
Share on other sites

I'm trying to automaticly get the default gateway of computers.

I use Sysinternals PSEXEC to launch IPCONFIG /ALL and snag the default gateway from the line that looks like this:

Default Gateway . . . . . . . . . : 192.168.123.254

This allows me to query any computer for their ip address, macid, etc.
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

  • 2 weeks later...

Use the beta and this code by scriptomatic.. You can striip some of the information out that you don't need

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "Caption: " & $objItem.Caption & @CRLF
      $strDefaultIPGateway = $objItem.DefaultIPGateway(0)
      $Output = $Output & "DefaultIPGateway: " & $strDefaultIPGateway & @CRLF
      $Output = $Output & "DHCPEnabled: " & $objItem.DHCPEnabled & @CRLF
      $Output = $Output & "DHCPLeaseExpires: " & WMIDateStringToDate($objItem.DHCPLeaseExpires) & @CRLF
      $Output = $Output & "DHCPLeaseObtained: " & WMIDateStringToDate($objItem.DHCPLeaseObtained) & @CRLF
      $Output = $Output & "DHCPServer: " & $objItem.DHCPServer & @CRLF
      $Output = $Output & "DNSDomain: " & $objItem.DNSDomain & @CRLF
      $strDNSDomainSuffixSearchOrder = $objItem.DNSDomainSuffixSearchOrder(0)
      $Output = $Output & "DNSDomainSuffixSearchOrder: " & $strDNSDomainSuffixSearchOrder & @CRLF
      $Output = $Output & "DNSEnabledForWINSResolution: " & $objItem.DNSEnabledForWINSResolution & @CRLF
      $Output = $Output & "DNSHostName: " & $objItem.DNSHostName & @CRLF
      $strDNSServerSearchOrder = $objItem.DNSServerSearchOrder(0)
      $Output = $Output & "DNSServerSearchOrder: " & $strDNSServerSearchOrder & @CRLF
      $Output = $Output & "DomainDNSRegistrationEnabled: " & $objItem.DomainDNSRegistrationEnabled & @CRLF
      $strIPAddress = $objItem.IPAddress(0)
      $Output = $Output & "IPAddress: " & $strIPAddress & @CRLF
      $strIPSubnet = $objItem.IPSubnet(0)
      $Output = $Output & "IPSubnet: " & $strIPSubnet & @CRLF
      $Output = $Output & "MACAddress: " & $objItem.MACAddress & @CRLF

      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_NetworkAdapterConfiguration" )
Endif


Func WMIDateStringToDate($dtmDate)

    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc
Link to comment
Share on other sites

ipconfig witouth /all also return the default gateway

and its under the netmask

so scan for the first occurance of \s255\.

and after that the first occurance after the 255

of \d{1,3\.\d{1,3\.\d{1,3\.\d{1,3

get that value using stringregexp

and you have the default gateway

this also solves the problem if people have more ip addresses per

$stringfromipconfig = StringTrimLeft ( $stringfromipconfig, StringInStr ( $stringfromipconfig, " 255." ))
$somearray = stringregexp($stringfromipconfig,"(\d{1,3\.\d{1,3\.\d{1,3\.\d{1,3})",1)
$gateway= $somearray[0]
Edited by MrSpacely
Link to comment
Share on other sites

and what if people have a subnet masker of 0.0.0.0 or 140.51.24.45

WMI is way better.

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
$colNicConfigs = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 48)

ConsoleWrite("IP Addresses & Subnet Masks" & @CRLF)

For $objNicConfig In $colNicConfigs
    ConsoleWrite("Network Adapter       " & $objNicConfig.Description & @LF)
    ConsoleWrite("IP Address(es):")
    For $strIPAddress In $objNicConfig.IPAddress
        ConsoleWrite("      " & $strIPAddress)
    Next
    ConsoleWrite(@LF & "Subnet Mask(s):")
    For $strIPSubnet In $objNicConfig.IPSubnet
        ConsoleWrite("      " & $strIPSubnet)
    Next
    ConsoleWrite(@CRLF)
Next
Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

and what if people have a subnet masker of 0.0.0.0 or 140.51.24.45

WMI is way better.

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
$colNicConfigs = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

ConsoleWrite("IP Addresses & Subnet Masks" & @CRLF)

For $objNicConfig In $colNicConfigs
    ConsoleWrite("Network Adapter       " & $objNicConfig.Description & @LF)
    ConsoleWrite("IP Address(es):")
    For $strIPAddress In $objNicConfig.IPAddress
        ConsoleWrite("      " & $strIPAddress)
    Next
    ConsoleWrite(@LF & "Subnet Mask(s):")
    For $strIPSubnet In $objNicConfig.IPSubnet
        ConsoleWrite("      " & $strIPSubnet)
    Next
    ConsoleWrite(@CRLF)
Next

Read up on subnetmasks please

subnetmask are redicilous if used with 145 at the start this will never be used

not on the internet and not in networks

(espescialy because no one has a network bigger then the internet)

subnetmask should never start with something less then 255 otherwise you would kill the whole network performance.

Then my point is right again. :P please sing praises to my genius

Ofcourse the WMI way is sweeter but not because of the subnet because of the directer way to get the information

Wow I can agree and still people must sing praises to my geniu"s :lmao:

Edited by MrSpacely
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...