0

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:

  1. open cmd and run the script
  2. 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

rita1989
  • 39
  • 7
  • 2
    You could [implement a Windows service](https://stackoverflow.com/questions/32404/how-do-you-run-a-python-script-as-a-service-in-windows), or you could [schedule it using the Windows task scheduler](https://stackoverflow.com/questions/44727232/scheduling-a-py-file-on-task-scheduler-in-windows-10) instead of using the Python `schedule` module. – larsks Jan 05 '23 at 15:01
  • how to do that? – rita1989 Jan 10 '23 at 13:06

0 Answers0