config_ip.py 3.2 KB
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()