Jump to content

Default Gateway UDF


RìggníX
 Share

Recommended Posts

  • 4 weeks later...
  • 1 year later...

I ran across this thread looking for a quick way to get the default gateway. I couldn't find it so I wrote it:

This code will actually get the default gateway:

MsgBox(0,"default gateway", _get_default_gateway())

Func _get_default_gateway()
    $output = _get_cmd_output("netstat -rn")
    $arr = StringSplit($output, "Default Gateway:", 1)
    $arr2 = StringSplit($arr[2], "=",1)
    Return StringReplace(StringStripWS($arr2[1],1), @LF, '')
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
Link to comment
Share on other sites

Hi!

Don't run for me.

But it's normal, because :

- I have TWO gateways (it's some ROUTE statements who choose the gateway).

- My Windows is Vista ; and Netsh changed few presentations of infos.

- My Windows is in french, and some words are translated.

Perso, for found the first (metric=1) gateway, I use :

ipconfig /ALL | find "Passerelle"

("Passerelle" is the french translation for the english word "Gateway")

@+

Edited by Michel Claveau
Link to comment
Share on other sites

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...