I want my computer to switch between 2 different IP configuration, everyday at two different time. I figured i could automate this with a python script.
import sys
sys.path.append(r"c:\users\user\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages")
import os
import ipaddress
import socket
import time
import schedule
def firstIP():
hn = socket.gethostname()
ipa = socket.gethostbyname(hn)
print("Current IP: ",ipa)
os.system('netsh interface ip set address name="Wi-Fi" static 192.168.1.20 255.255.255.0 192.168.1.1')
os.system('netsh interface ip set dns name="Wi-Fi" static 8.8.8.8')
os.system('netsh interface ip set dns name="Wi-Fi" static 8.8.4.4 index=2')
print("IP ADRESS CHANGED!")
def secondIP():
hn = socket.gethostname()
ipa = socket.gethostbyname(hn)
print("Current IP: ",ipa)
os.system('netsh interface ip set address name="Wi-Fi" static 192.168.1.50 255.255.255.0 192.168.1.1')
os.system('netsh interface ip set dns name="Wi-Fi" static 8.8.8.8')
os.system('netsh interface ip set dns name="Wi-Fi" static 8.8.4.4 index=2')
print("IP ADRESS CHANGED!")
schedule.every().day.at("01:00").do(firstIP)
schedule.every().day.at("15:00").do(secondIP)
while True:
schedule.run_pending()
time.sleep(1)
I made this script and it seems to work, but for it to run i need to:
- open cmd and run the script
- leave the cmd open with the script running
is there a way for running the script once, closing the cmd window and the schedule will still work? as a sort of os chron job? or something similar, without leaving the prompt open?
Thank you