8
\$\begingroup\$

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)

  1. 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
    
  2. Regular.bat.ink

    netsh interface ip set address "Wi-Fi" dhcp
    netsh interface ip set dnsservers "Wi-Fi" source=dhcp
    

Python 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()
\$\endgroup\$
  • \$\begingroup\$ Are you using WDS? \$\endgroup\$ – 200_success Sep 24 '15 at 23:32
  • \$\begingroup\$ @200_success, No, should I? \$\endgroup\$ – Galc127 Sep 25 '15 at 7:32
  • \$\begingroup\$ That seems like the proper way to extend your WiFi range, making one seamless network at Layer 2, without weird workarounds like this. \$\endgroup\$ – 200_success Sep 25 '15 at 7:34
  • \$\begingroup\$ @200_success, I tried using WDS. Unfortuneately, didnt get the required result... \$\endgroup\$ – Galc127 Sep 25 '15 at 12:12
6
\$\begingroup\$

This seems pretty straightforward, so I don't have a ton of critiques.

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.

\$\endgroup\$
3
\$\begingroup\$

If you wanted to, you could do this entirely in batch, and in only 1 batch file very easily. Using your existing code I threw this together for you.

@ECHO OFF
COLOR 0F

:Start
TITLE SET IP
ECHO.
ECHO.
ECHO.
ECHO 1) Set DHCP IP
ECHO 2) Set static IP
ECHO.
SET /P Area=Make your selection [1,2]... 
IF %Area%==1 GOTO :Regular
IF %Area%==2 GOTO :DeadSpot
CLS
TITLE INVALID SELECTION
COLOR FC
ECHO "%Area%" isn't a valid selection choice, please try a valid selection.
ECHO We'll return to the previous menu.
PAUSE
GOTO :Start

:Regular
netsh interface ip set address "Wi-Fi" dhcp
netsh interface ip set dnsservers "Wi-Fi" source=dhcp
GOTO :EOF

:DeadSpot
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
GOTO:EOF

Than if you wanted to run the batch file as an Administrator, just add this to your batch file, it should work I grabbed it from another thread here on SO

:: BatchGotAdmin (Put after @ECHO OFF, towards the top)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.