Python script for changing ip, subnet and gateway

197 Views Asked by At

Hi i'm trying to write a script for changing my ip, subnet mask and gateway.

The script runs, don't give out any error but doesn't change anything. Even the print, the new ip is still the same as the current one.

what am i doing wrong?

# importing os module 
import os
import ipaddress
import socket 
      
# Get the current working 
# directory (CWD) 
cwd = os.getcwd() 
      
# Print the current working 
# directory (CWD) 
print("Current working directory:", cwd) 

# Print current IP
hn = socket.gethostname()
ipa = socket.gethostbyname(hn)
print("Current IP: ",ipa)

os.system('netsh interface ip set address name="FirstSetup" static 192.168.0.20 255.255.255.0 192.168.0.1')
print("IP ADRESS CHANGED!")

# Print newIP
hn = socket.gethostname()
ipa = socket.gethostbyname(hn)
print("New IP: ",ipa)
2

There are 2 best solutions below

2
AudioBubble On

Changing all of this requires windows to make a reboot. Try restarting and you will see that your IP has changed.

0
JimmyLlama On

name="FirstSetup"

The name should be the name that is assigned to the interface you're trying to change, usually something like "Ethernet adapter Ethernet 1" or "Wireless LAN adapter Local Area Connection 1". Unless your interface is actually somehow named "FirstSetup" you're not changing anything.

Run "ipconfig /all" to see the names of your interfaces and choose the one with the IP address you're trying to change.