Jump to content

Function to set IP Address


Recommended Posts

I noticed a lot of people talking about how to set your IP address, but no one seems to have had a stab at how to do it using NetSH. Here is a function that I wrote which seems to do it. It could be improved and provide feedback etc, but I just needed somthing quick to replace a broken registry slam system I used previously. This grabs a list of NICs, then verifies if on PCI bus and hence a real one, not tunnel or loopback etc (you may wish to switch PCI for USB). If real, then it grabs interface name and then uses NetSH to set IP Address, Subnet, Gateway and up to 3 DNS servers. Leave the $myip field blank or set to dhcp if you wish DHCP instead of a STATIC IP

 

#RequireAdmin
#include <Constants.au3>

;Developed by Robin Johnston May 2015

;To test, comment out or delete the ones you don't want

;IP & GW only
SetIPAddress("10.1.1.1", "255.255.0.0", "10.1.1.254", "")

;IP, GW & 2 DNS
SetIPAddress("10.1.1.1", "255.255.0.0", "10.1.1.254", "10.1.1.253, 8.8.8.8")

;DHCP
SetIPAddress("dhcp", "", "", "")
;OR
SetIPAddress("", "", "", "")
;OR
SetIPAddress()

Exit

 

Func SetIPAddress($myip="",$mysub="",$mygate="",$mydns="")
    ;Updated May 2015 to set just the primary PCI NIC using NetSH

   ;Get list of active connections - can't use runwait here
   Local $pid = Run(@ComSpec & " /c netsh.exe int show int", "", @SW_HIDE, $STDOUT_CHILD)
   ;Wait until the process has closed using the PID returned by Run.
   ProcessWaitClose($pid)
   ;Read the Stdout stream of the PID returned by Run
   Local $output = StdoutRead($pid)
   ;Use StringSplit to split the output of StdoutRead to an array. All carriage returns (@CRLF) are stripped and @CRLF (line feed) is used as the delimiter.
   Local $lans = StringSplit(StringTrimRight(StringStripCR($output), StringLen(@CRLF)), @CRLF)
   ;Loop through array looking for 'dedicated' connections

   For $i = 1 to UBound($lans)-1 Step 1
      If StringInStr($lans[$i],"Dedicated") Then
         ;A valid connection so lets check it out - strip out connection ID
         $pos=StringInStr($lans[$i],"  ",0,-1)
         $conn=StringTrimLeft($lans[$i],$pos+1)
         ;Use WMIC to verify connection goes to a real adapter
         $pid = Run(@ComSpec & ' /c wmic.exe nic where(NetConnectionID="'& $conn &'") get PNPDeviceID', "", @SW_HIDE, $STDOUT_CHILD)
         ProcessWaitClose($pid)
         $output = StdoutRead($pid)
         $device=StringSplit(StringTrimRight(StringStripCR($output), StringLen(@CRLF)), @CRLF)[2]
         ;Valid devices types can be PCI for onboard/internal NICs, USB for plugin USB NICs and ROOT for software based adapters such as tunnels or loopback NICs
         If StringInStr($device,"PCI\") Then
            ConsoleWrite("Connection '" & $conn & "' on device " & $device & " is being set..." & @CR)
            If StringInStr($myip,"dhcp") Or $myip="" Then
               RunWait(@ComSpec & ' /c netsh.exe int ip set address "' & $conn & '" dhcp',"", @SW_HIDE)
               RunWait(@ComSpec & ' /c netsh.exe int ip set dns "' & $conn & '" dhcp', "", @SW_HIDE)
               ConsoleWrite("-Setting DHCP IP & DNS" & @CR)
            Else
               RunWait(@ComSpec & ' /c netsh.exe int ip set address "' & $conn & '" static ' & $myip & ' ' & $mysub & ' ' &  $mygate & ' 1', "", @SW_HIDE)
               ConsoleWrite("-Setting IP Addr" & @CR)
               Local $dns=StringSplit($mydns,",")
               If UBound($dns)>1 And $dns[1]<>"" Then
                  ConsoleWrite("-Setting DNS1" & @CR)
                  RunWait(@ComSpec & ' /c netsh.exe int ip set dns name="' & $conn & '" static ' & $dns[1] & ' validate=no', "", @SW_HIDE)
                  If UBound($dns)>2 And $dns[2]<>"" Then
                     ConsoleWrite("-Setting DNS2" & @CR)
                     RunWait(@ComSpec & ' /c netsh.exe int ip add dns name="' & $conn & '" ' & $dns[2] & ' index=2 validate=no', "", @SW_HIDE)
                     If UBound($dns)>3 And $dns[3]<>"" Then
                        ConsoleWrite("-Setting DNS3" & @CR)
                        RunWait(@ComSpec & ' /c netsh.exe int ip add dns name="' & $conn & '" ' & $dns[3] & ' index=3', "", @SW_HIDE)
                     EndIf
                  EndIf
               EndIf
            EndIf
         EndIf
      EndIf
  Next

EndFunc ;SetIPAddress

 

Link to comment
Share on other sites

  • Moderators

Did you have a question on the script, or were you just providing as an example? If the former, could you clearly post what you're having trouble with? If you're just sharing, you might ask a Mod to move this to the Examples forum. You should also use the code tags on the forum for your script for readability.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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