config_ip.py
4.7 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
from flask import Flask, render_template, request, Response
from app import app
import time
from threading import Thread
netprofilepath = r'/prog/smartshelf/app/netprofile/'
#app = Flask(__name__)
@app.route('/ipconfig')
def shelfconfig():
print("begin ip flask")
return render_template('ipconfig.html')
def Config_DHCP():
netconfigfile = ''
if os.path.exists('/etc/dhcpcd.conf'):
netprofile = netprofilepath+'dhcpcd.conf.bak'
netconfigfile = '/etc/dhcpcd.conf'
else:
netprofile = netprofilepath+'/NEOTEL.nmconnection.bak'
netconfigfile = '/etc/NetworkManager/system-connections/NEOTEL.nmconnection'
with open(netprofile, 'r') as f:
file_lines = f.readlines()
f.close()
with open(netconfigfile, 'w') as f:
# for line in file_lines:
f.writelines(file_lines)
f.flush()
f.close()
def Config_StaticIP(IPAddress, Router_IP):
netconfigfile = ''
if os.path.exists('/etc/dhcpcd.conf'):
netprofile = netprofilepath+'dhcpcd.conf.bak'
netconfigfile = '/etc/dhcpcd.conf'
with open(netprofile, 'r') as f:
file_lines = f.readlines()
f.close()
with open(netconfigfile, '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()
else:
netprofile = netprofilepath+'/NEOTEL.nmconnection.bak'
netconfigfile = '/etc/NetworkManager/system-connections/NEOTEL.nmconnection'
with open(netprofile, 'r') as f:
file_lines = f.readlines()
f.close()
with open(netconfigfile, 'w') as f:
# for line in file_lines:
for line in file_lines:
f.write(line)
if line.startswith('[ipv4]') and len(IPAddress) > 0:
if len(Router_IP) > 0:
f.write('address1='+ IPAddress + ','+Router_IP+"\n")
else:
f.write('address1='+ IPAddress + "\n")
# 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.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()