Jump to content

Search the Community

Showing results for tags 'netsh tcpip nic ip address'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • General
    • Announcements and Site News
    • Administration
  • AutoIt v3
    • AutoIt Help and Support
    • AutoIt Technical Discussion
    • AutoIt Example Scripts
  • Scripting and Development
    • Developer General Discussion
    • Language Specific Discussion
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office

Categories

  • AutoIt Team
    • Beta
    • MVP
  • AutoIt
    • Automation
    • Databases and web connections
    • Data compression
    • Encryption and hash
    • Games
    • GUI Additions
    • Hardware
    • Information gathering
    • Internet protocol suite
    • Maths
    • Media
    • PDF
    • Security
    • Social Media and other Website API
    • Windows
  • Scripting and Development
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Member Title


Location


WWW


Interests

Found 1 result

  1. 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
×
×
  • Create New...