config_ip.py
3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
from flask import Flask, render_template, request, Response
from app import app
import time
from threading import Thread
#app = Flask(__name__)
@app.route('/ipconfig')
def shelfconfigA():
print("begin ip flask")
return render_template('ipconfig.html')
def Config_DHCP():
with open('/etc/dhcpcd.conf.bak', 'r') as f:
file_lines = f.readlines()
f.close()
with open('/etc/dhcpcd.conf', 'w') as f:
# for line in file_lines:
f.writelines(file_lines)
f.flush()
f.close()
def Config_StaticIP(IPAddress, Router_IP):
with open('/etc/dhcpcd.conf.bak', 'r') as f:
file_lines = f.readlines()
f.close()
with open('/etc/dhcpcd.conf', 'w') as f:
# for line in file_lines:
f.writelines(file_lines)
# f.write("static ip_address=" + str(request.args.get('new_ip')))
# interface eth0
#IPAddress = str(request.args.get('new_ip')
#Router_IP = str(request.args.get('new_ip')
f.write("interface eth0" + "\n")
if len(IPAddress) > 0:
f.write("static ip_address=" + IPAddress + "\n")
if len(Router_IP) > 0:
f.write("static routers=" + Router_IP + "\n")
# f.write("")
f.flush()
f.close()
def reboot_after_delay():
# Wait for 5 seconds before rebooting
time.sleep(5)
os.system('sudo reboot')
def Reenable_NetworkService():
#os.system('sudo systemctl stop networking')
#os.system('sudo systemctl start networking')
#os.system('sudo systemctl stop dhcpcd.service')
#print("services stop")
#os.system('sudo ip addr flush dev eth0')
#os.system('sudo systemctl start dhcpcd.service')
# Wait for 3 seconds before rebooting
reboot_thread = Thread(target=reboot_after_delay)
reboot_thread.start()
return "Apply Successfully, System will reboot in 5 seconds"
#return "Raspberry Pi will reboot in 3 seconds"
#os.system('sudo reboot')
def netmask_to_length(netmask):
# Convert the netmask to a list of integers
netmask = [int(x) for x in netmask.split(".")]
# Count the number of set bits in the netmask
length = 0
for octet in netmask:
while octet > 0:
length += octet & 1
octet >>= 1
return length
@app.route('/ip_config',methods=['POST'])
def IP_Config():
dhcp_enabled = request.form.get('dhcp')
if dhcp_enabled:
Config_DHCP()
print("DHCP ENABLED")
else:
IP_ADDR = str(request.form.get('new_ip'))
print("input:",str(request.form.get('new_ip')))
Router_ADDR = str(request.form.get('router_ip'))
NET_MASK = str(request.form.get('new_mask'))
IP_ADDR +="/"+str(netmask_to_length(NET_MASK))
print(IP_ADDR)
print(Router_ADDR)
Config_StaticIP(IP_ADDR,Router_ADDR)
print(IP_ADDR,Router_ADDR)
print("IP CHANGED")
#Reenable_NetworkService()
#Response('Apply Successfully', status=200, mimetype='text/plain')
return Reenable_NetworkService()
#return #'Apply Successfully ' #{} with DHCP enabled: {}'.format(IP_ADDR, 'True' if dhcp_enabled else 'False')
#Config_StaticIP("192.168.1.59/24", "192.168.1.1")
#Reenable_NetworkService()