Jump to content

Get default gateway


botanic
 Share

Recommended Posts

Is there any easy way to get this?

I tried

$tmp = RunWait("route print 0.0.0.0")

I was just going to format the string however that returns 0 how can I get the text that is normally printed to the command prompt?

Try this:

#include <Process.au3>
MsgBox(0,"Default Gateway",GetDefaultGateway())

Func GetDefaultGateway()
    _RunDOS("ipconfig >" & @TempDir & "\gateway.dat")
    $FILE = FileOpen(@TempDir & "\gateway.dat",0)
    $INDEX = 1
    While 1
        $LINE = FileReadLine($FILE,$INDEX)
        If @error = -1 Then Exit
        $SPLIT = StringSplit($LINE,":")
        If IsArray($SPLIT) Then
            If StringInStr($SPLIT[1],"Default Gateway") <> 0 Then Return StringStripWS($SPLIT[2],4)
        EndIf
        $INDEX += 1
    WEnd
    FileClose($FILE)
EndFunc

When the words fail... music speaks.

Link to comment
Share on other sites

nevermind got it

Func _get_default_gateway()
    $output = _get_cmd_output("netstat -rn")
    $arr = StringSplit($output, "0.0.0.0", 1)
    $arr2 = StringStripWS($arr[3], 1)
    $arr2 = StringSplit($arr2, " ")
    Return $arr2[1]
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

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