Jump to content

Change IP address


Recommended Posts

This code isn't changing the static IP address.

I think it's because $Networkname doesn't have Quotes around it.  I can't figure out how to add Quotes.

;Sometimes when I am working on a PC the IP settings get removed.  This will put the IP settings back in.

;I would like this script to run everytime the machine reboots or when I run it manually.
;I would like it to check to make sure there is an internet connection.
;I would like to run this on a PC that is remote to me.  I can't connect if it's not connected to the internet.

;AutoIt_Debugger_Command:Disable_Debug
Opt("WinWaitDelay", 100)
Opt("WinTitleMatchMode", 4)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 0)
Opt("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number
#RequireAdmin

;AutoIt_Debugger_Command:Enable_Debug
$Networkname = "Local Area Connection"

$CMD = 'netsh interface ipv4 set address ' & $Networkname & ' static 192.168.2.5 255.255.255.0 192.168.2.254'
_DosRun($CMD)
_DosRun('netsh interface ipv4 add dns ' & $Networkname & ' 66.51.205.100')
_DosRun('netsh interface ipv4 add dns ' & $Networkname & ' 66.51.206.100 index=2')

Exit

Func _DosRun($sCommand)
    Local $nResult = Run('"' & @ComSpec & '" /c ' & $sCommand, @SystemDir, @SW_HIDE, 6)
    Sleep(1000)
    ProcessWaitClose($nResult)
    Return StdoutRead($nResult)
EndFunc   ;==>_DosRun

 

Link to comment
Share on other sites

1 hour ago, Docfxit said:

...  I can't figure out how to add Quotes.

something like this:

$CMD = 'netsh interface ipv4 set address "' & $Networkname & '" static 192.168.2.5 255.255.255.0 192.168.2.254'

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

10 hours ago, Chimp said:

something like this:

$CMD = 'netsh interface ipv4 set address "' & $Networkname & '" static 192.168.2.5 255.255.255.0 192.168.2.254'

 

That worked perfectly in Windows 7 32bit.  Thank you very much,

I can't get it to replace the IP addresses in Windows 10 64bit.

;Sometimes when I am working on a PC the IP settings gets removed.  This will put the IP settings back in.

;I would like this script to run everytime the machine reboots or when I run it manually.
;I would like it to check to make sure there is an internet connection.
;I would like to run this on a PC that is remote to me.  I can't connect if it's not connected to the internet.

Opt("WinWaitDelay", 100)
Opt("WinTitleMatchMode", 4)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 0)
Opt("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number
#RequireAdmin
Global $CMD
$Networkname = "Ethernet"
$IPAddress = "192.168.168.7"
$Gateway = "192.168.168.168"
_DosRun($CMD)
_DosRun('netsh interface ipv4 set address "' & $Networkname & '" static ' & $IPAddress & ' 255.255.255.0 ' & $Gateway)
_DosRun('netsh interface ipv4 add dns "' & $Networkname & '" 66.51.205.100')
_DosRun('netsh interface ipv4 add dns "' & $Networkname & '" 66.51.206.100 index=2')
MsgBox(32, "Set IP Address", 'netsh interface ipv4 set address "' & $Networkname & '" static ' & $IPAddress & ' 255.255.255.0 ' & $Gateway)
Exit

Func _DosRun($sCommand)
    Local $nResult = Run('"' & @ComSpec & '" /c ' & $sCommand, @SystemDir, @SW_HIDE, 6)
    Sleep(1000)
    ProcessWaitClose($nResult)
    Return StdoutRead($nResult)
EndFunc   ;==>_DosRun

 

Edited by Docfxit
Link to comment
Share on other sites

...not sure, but worth try this line at the beginning of your script...

#include <WinAPIFiles.au3>

;Turn off redirection for a 32-bit script on 64-bit system.
If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

That's a great idea.

I tried it.  The script still isn't working. 

I put in some if statements and it is running "X64 and it is "NOT @AutoItx64"

So the if statement should be working.

Thanks,

Edited by Docfxit
Link to comment
Share on other sites

8 hours ago, Nine said:

Do you get any error messages after netsh commands ?  You do not seem to report it.

I'm not seeing any error messages.  I'm not sure my script is written to show any errors.

I don't know how to write something into the script to capture any errors.

Thanks,

Docfxit

Link to comment
Share on other sites

Replace those lines by this :

ConsoleWrite ("1 = " & _DosRun($CMD) & @CRLF)
ConsoleWrite ("2 = " & _DosRun('netsh interface ipv4 set address "' & $Networkname & '" static ' & $IPAddress & ' 255.255.255.0 ' & $Gateway) & @CRLF)
ConsoleWrite ("3 = " & _DosRun('netsh interface ipv4 add dns "' & $Networkname & '" 66.51.205.100') & @CRLF)
ConsoleWrite ("4 = " & _DosRun('netsh interface ipv4 add dns "' & $Networkname & '" 66.51.206.100 index=2') & @CRLF)

Run your script from Scite, report here the console, so we can have a look.

Link to comment
Share on other sites

6 hours ago, Nine said:

Replace those lines by this :

ConsoleWrite ("1 = " & _DosRun($CMD) & @CRLF)
ConsoleWrite ("2 = " & _DosRun('netsh interface ipv4 set address "' & $Networkname & '" static ' & $IPAddress & ' 255.255.255.0 ' & $Gateway) & @CRLF)
ConsoleWrite ("3 = " & _DosRun('netsh interface ipv4 add dns "' & $Networkname & '" 66.51.205.100') & @CRLF)
ConsoleWrite ("4 = " & _DosRun('netsh interface ipv4 add dns "' & $Networkname & '" 66.51.206.100 index=2') & @CRLF)

Run your script from Scite, report here the console, so we can have a look.

Writing out the output was a really great idea.  Thank you very much for the code.

When I run it in Windows 10 I get:

1 = 
2 = The object already exists.



3 = The object is already in the list.



4 = The object is already in the list.

This is what the network screen looks like after it's run:

StaticIPAddressResults.thumb.jpg.404ca9712095b2a57ddd9681461931a0.jpg

When I run it in Windows 7 line #2 is empty and it inserts the IP addresses like it should.

Thanks for the help,

Docfxit

Link to comment
Share on other sites

i had a problem with this also with a .bat script (that was run as admin), i think it was Win7, and i didn't look deep into it, i just did it like this, reset to dhcp then set ip  and i worked.

netsh interface ipv4 set address "Local Area Connection" dhcp
netsh interface ipv4 add address "Local Area Connection" 192.168.1.44 255.255.255.0 192.168.1.1
pause

also look in the advanced options in the internet protocol version4 tcpip properties, there you can add several IP addresses to the same network card, look if the IP addresses you are trying to add aren't already in the list, in my case they where not strangely, also it seems according to the console writes you posted that they are already added to the interface, so let know what you see there.

Link to comment
Share on other sites

I ended up fix it with this code:

;Sometimes when I am working on a PC the IP settings gets removed.  This will put the IP settings back in.

;I would like this script to run everytime the machine reboots or when I run it manually.
;I would like it to check to make sure there is an internet connection.
;I would like to run this on a PC that is remote to me.  I can't connect if it's not connected to the internet.

#RequireAdmin
#include <WinAPIFiles.au3>
;Turn off redirection for a 32-bit script on 64-bit system.
If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)

Opt("WinWaitDelay", 100)
Opt("WinTitleMatchMode", 4)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 0)
Opt("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number
Global $CMD
FileDelete(@ScriptDir & "\StaticIPAddressDebug.txt")

$Networkname = "Ethernet 2"
$IPAddress = "192.168.2.5"
$Gateway = "192.168.2.254"

FileWrite(@ScriptDir & "\StaticIPAddressDebug.txt" ,("1 = " & _DosRun($CMD) & @CRLF))
FileWrite(@ScriptDir & "\StaticIPAddressDebug.txt" ,("2 = " & _DosRun('netsh int ip reset') & @CRLF))
FileWrite(@ScriptDir & "\StaticIPAddressDebug.txt" ,("3 = " & _DosRun('netsh interface ipv4 set address "' & $Networkname & '" static ' & $IPAddress & ' 255.255.255.0 ' & $Gateway) & @CRLF))
FileWrite(@ScriptDir & "\StaticIPAddressDebug.txt" ,("4 = " & _DosRun('netsh interface ipv4 add dns "' & $Networkname & '" 66.51.205.100') & @CRLF))
FileWrite(@ScriptDir & "\StaticIPAddressDebug.txt" ,("5 = " & _DosRun('netsh interface ipv4 add dns "' & $Networkname & '" 66.51.206.100 index=2') & @CRLF))
Exit

Func _DosRun($sCommand)
    Local $nResult = Run('"' & @ComSpec & '" /c ' & $sCommand, @SystemDir, @SW_HIDE, 6)
    Sleep(1000)
    ProcessWaitClose($nResult)
    Return StdoutRead($nResult)
EndFunc   ;==>_DosRun

When I have a network problem on a remote PC  I:

1. Compile the script above and put a link to it in Programs, Starup

2. Add the program to my antivirus exclusions.

3. Run a bat file that resets the network and does a reboot.

 

Thanks to everyone for the help,

Docfxit

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