patternpythonMinor
WiFi adapter settings
Viewed 0 times
adaptersettingswifi
Problem
Yesterday I have found that I can't use the wireless network at some spots in my house. I used another modem as a WiFi booster and I managed to cover these spots.
The problem is that when I go to these dead spots I need to use static IP and change my primary DNS servers, or I get limited connection. Also, I still want to use DHCP when I'm not in these spots.
I have written two Batch files and a Python script to define the wireless adapter settings. I would like someone to take a look and suggest how to improve it.
Batch Files (I'm using shortcuts because of the option to run them as administrator)
-
DeadSpots.bat.ink
-
Regular.bat.ink
Python code
The problem is that when I go to these dead spots I need to use static IP and change my primary DNS servers, or I get limited connection. Also, I still want to use DHCP when I'm not in these spots.
I have written two Batch files and a Python script to define the wireless adapter settings. I would like someone to take a look and suggest how to improve it.
Batch Files (I'm using shortcuts because of the option to run them as administrator)
-
DeadSpots.bat.ink
netsh interface ip set address "Wi-Fi" static 192.168.x.x 255.255.255.0 192.168.x.x
netsh interface ip set dns "Wi-Fi" static 192.168.x.x primary # This is the second modem
netsh interface ip add dns "Wi-Fi" ISP.dns.IP index=2-
Regular.bat.ink
netsh interface ip set address "Wi-Fi" dhcp
netsh interface ip set dnsservers "Wi-Fi" source=dhcpPython code
import subprocess as sub
def WiFi():
filepath1 = Path_To_DeadSpots.bat.ink
filepath2 = Path_To_Regular.bat.ink
loc = input("Please choose your location: 1-Rooms, 2-Rest \n")
while(loc != "1" and loc != "2"):
print("Wrong input, please choose again")
loc = input("Please choose your location: 1-Rooms, 2-Rest \n")
if loc == "1":
p = sub.Popen(filepath1,shell=True,stdout=sub.PIPE)
else:
p = sub.Popen(filepath2,shell=True,stdout=sub.PIPE)
WiFi()Solution
This seems pretty straightforward, so I don't have a ton of critiques.
Naming
PEP8 conventions say your function should probably be named
The names
Input
You're repeating yourself unnecessarily while getting user input. Furthermore, try to keep your functions pure and handle user input separately.
Magic numbers
It might be cleaner to make
Unused variables
You never use
Here is my full rewrite of your Python program. I don't know enough about the batch scripting to comment there.
Honestly, given the small nature of this it might not even make sense to make it a function. If you plan on expanding this then it might be worthwhile to implement these changes.
Naming
PEP8 conventions say your function should probably be named
wifi (which is not a great name)The names
filepath1 and filepath2 aren't very helpful.Input
You're repeating yourself unnecessarily while getting user input. Furthermore, try to keep your functions pure and handle user input separately.
Magic numbers
It might be cleaner to make
"1" and "2" constants. Even cleaner is to use a dictionary, like I did.Unused variables
You never use
p, so I don't see a reason to assign to it.Here is my full rewrite of your Python program. I don't know enough about the batch scripting to comment there.
import subprocess as sub
room_to_batchfile = {
"1": "Path/To/DeadSpots.bat.ink",
"2": "Path/To/Regular.bat.ink"
}
def setup_wifi(room):
sub.Popen(room_to_batchfile[room], shell=True, stdout=sub.PIPE)
if __name__ == '__main__':
loc = ""
while loc not in room_to_batchfile:
loc = input("Please choose your location: 1-Rooms, 2-Rest \n")
setup_wifi(loc)Honestly, given the small nature of this it might not even make sense to make it a function. If you plan on expanding this then it might be worthwhile to implement these changes.
Code Snippets
import subprocess as sub
room_to_batchfile = {
"1": "Path/To/DeadSpots.bat.ink",
"2": "Path/To/Regular.bat.ink"
}
def setup_wifi(room):
sub.Popen(room_to_batchfile[room], shell=True, stdout=sub.PIPE)
if __name__ == '__main__':
loc = ""
while loc not in room_to_batchfile:
loc = input("Please choose your location: 1-Rooms, 2-Rest \n")
setup_wifi(loc)Context
StackExchange Code Review Q#105629, answer score: 6
Revisions (0)
No revisions yet.