Created
September 30, 2022 21:37
-
-
Save anddoellinger/5543a904e765710d79aab5e2f8ae59dc to your computer and use it in GitHub Desktop.
Setup tool to configurate static IP Address on RaspberryPi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
# variables | |
static_ip = "" | |
static_netmask = "" | |
static_gateway = "" | |
# Network configuration path | |
config_path = "/etc/dhcpcd.conf" | |
interfaces_path = "/etc/network/interfaces" | |
# Enable static or dynamic IP-address | |
print("Welcome to RaspberryPi network interface setup.") | |
ip_input = input("Is a static IP-address required? (Y/n): ") | |
if ip_input.lower() == 'yes' or ip_input.lower() == 'y': | |
# Passing static IP | |
while True: | |
static_ip = str(input("Enter preferred static IP Address (e.g. 192.168.1.xxx): ")) | |
# Passing just if the re.match condition is fulfilled and required information is provided in the correct form | |
# if not the question is looped... | |
if not re.match(r"\d\d\d[.]\d\d\d[.]\d[.]\d\d\d", static_ip): | |
print("The provided IP-Address is not correct, please enter valid IP-Address.") | |
else: | |
print(f"Provided IP Address is: {static_ip}") | |
break | |
# Passing Gateway (Router IP Address) | |
while True: | |
static_gateway = str(input("Enter the corresponding gateway (e.g. 192.168.1.254 for Life QRO): ")) | |
# Passing just if the re.match condition is fulfilled and required information is provided in the correct form | |
# if not the question is looped... | |
if not re.match(r"\d\d\d[.]\d\d\d[.]\d[.]\d\d\d", static_gateway): | |
print("The provided netmask is not correct, please enter valid gateway.") | |
else: | |
print(f"Provided IP Address is: {static_gateway}") | |
break | |
# Writing static IP information to RaspberryPi | |
with open(config_path, 'a') as settings: | |
settings.write("interface eth0\n" | |
f"static ip_address={static_ip}/24\n" | |
f" static_routers={static_gateway}\n" | |
f" static_domain_name_servers={static_gateway} 208.67.222.222\n" | |
"\n" | |
"allow-hotplug eth0\n" | |
"iface eth0 inet static\n" | |
) | |
with open(interfaces_path, "w") as settings: | |
settings.write( | |
"# interfaces(5) file used by ifup(8) and ifdown(8)\n" | |
"# Please note that this file is written to be used with dhcpcd\n" | |
"# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'\n" | |
"\n" | |
"# Include files from /etc/network/interfaces.d:\n" | |
"source-directory /etc/network/interfaces.d\n" | |
"\n" | |
"auto lo\n" | |
"iface lo inet loopback\n" | |
"\n" | |
"iface eth0 inet manual\n" | |
"\n" | |
"allow-hotplug wlan0\n" | |
"iface wlan0 inet manual\n" | |
"wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf\n" | |
"\n" | |
"allow-hotplug wlan1\n" | |
"iface wlan1 inet manual\n" | |
"wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf\n" | |
) | |
elif ip_input.lower() == 'no' or ip_input.lower() == 'n': | |
# Writing dynamic IP information to RaspberryPi | |
with open(config_path, 'a') as settings: | |
settings.write("auto lo\n" | |
"iface lo inet loopback\n" | |
"iface eth0 inet manual\n" | |
" auto eth0\n" | |
" iface eth0 inet dhcp\n" | |
"THIS SHOULD HAVE WORKED\n" | |
) | |
else: | |
print("No valid input") | |
print("Setup completed. \n") | |
# ("Following commands must be executed:\n" | |
# "sudo service dhcpcd stop\n" | |
# "sudo systemctl disable dhcpcd" | |
# "Recommended reboot command: sudo service networking restart") | |
# # Install VNC Server connection | |
# os.system("sudo apt install realvnc-vnc-server realvnc-vnc-viewer") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment