Sign in to follow this  
Followers 0
creeping

Changing a network adaptor's IP via command line

1 post in this topic

#include <Array.au3>
#include <Constants.au3>
#include <Process.au3>

Main()

Func Main()
    $cmd    = "netsh interface ip show config"
    $quotes = chr(34)
    $line   = ""
    
    $foo = Run($cmd, @SystemDir, @SW_HIDE, $STDOUT_CHILD)

    While 1
        $line &= StdoutRead($foo)
    
        If @error Then ExitLoop
    WEnd

    $adaptorNames = ExtractStrBetweenChars($line, $quotes)
    
    If $adaptorNames[0] = 0 Then
        MsgBox(0, "", "No network adaptors found")
    Else
        $line = ""
        
        For $i = 1 To $adaptorNames[0] Step 1
            $line = $line & $i & "." & "  " & $adaptorNames[$i] & @CRLF
        Next
        
        $adaptorNum = InputBox("", $line & @CRLF & "Enter number of network adaptor to change")
        
        $IP      = InputBox("", "Enter IP")
        $subnet  = InputBox("", "Enter Subnet")
        $gateway = InputBox("", "Enter Gateway")
        
        $DNS1    = InputBox("", "Enter DNS 1")
        $DNS2    = InputBox("", "Enter DNS 2")
        
        SetNetworkAdaptorToStatic($adaptorNames[$adaptorNum], $IP, $subnet, $gateway, $DNS1, $DNS2)
    EndIf
EndFunc

Func ExtractStrBetweenChars($str, $char)
    Dim $array[1]
    
    $occurrence = 0

    While 1
        $occurrence += 1
        
        $start = StringInStr($str, $char, 0, $occurrence)
        
        If $start = 0 Then 
            ExitLoop
        Else
            $occurrence += 1
            $stop = StringInStr($str, $char, 0, $occurrence) - 1
        EndIf
        
        _ArrayAdd($array, StringMid($str, $start + 1, $stop - $start))
    WEnd
    
    $array[0] = UBound($array) - 1
    
    Return $array
EndFunc

Func SetNetworkAdaptorToStatic($adaptorName, $IP, $subnet, $gateway, $DNS1, $DNS2)
    $IP      = " " & $IP & " "
    $subnet  = " " & $subnet & " "
    $gateway = " " & $gateway & " "
    
    $DNS1 = " " & $DNS1 & " "
    $DNS2 = " " & $DNS2 & " "
    
    $TCPIP_static_cmd  = "netsh interface ip set address name=" & chr(34) & $adaptorName & chr(34) & " static " & $IP & $subnet & $gateway & "1"
    $DNS_static_cmd1   = "netsh interface ip set dns " & chr(34) & $adaptorName & chr(34) & " static " & $DNS1
    $DNS_static_cmd2   = "netsh interface ip add dns " & chr(34) & $adaptorName & chr(34) & $DNS2 & "index=2"

    _RunDos($TCPIP_static_cmd)
    _RunDos($DNS_static_cmd1)
    _RunDos($DNS_static_cmd2)
EndFunc

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  
Followers 0