mirror of
https://github.com/PhotonVision/photonvision
synced 2026-07-05 03:21:40 +00:00
added ip handler
This commit is contained in:
42
backend/app/handlers/IPHandler.py
Normal file
42
backend/app/handlers/IPHandler.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import subprocess
|
||||
import netifaces
|
||||
|
||||
|
||||
class ChangeIP:
|
||||
def __init__(self, ip_type, ip, netmask, gateway, hostname):
|
||||
|
||||
adapter = self.find_adapter()
|
||||
|
||||
if ip_type == "DHCP":
|
||||
self.change_to_dhcp(adapter=adapter)
|
||||
elif ip_type == "Static":
|
||||
self.change_to_static(adapter=adapter, ip=ip, netmask=netmask, gateway=gateway)
|
||||
|
||||
self.change_hostname(hostname=hostname)
|
||||
self.restart_adapter(adapter=adapter)
|
||||
|
||||
@staticmethod
|
||||
def change_to_dhcp(adapter):
|
||||
subprocess.call(['dhclient -r', adapter])
|
||||
|
||||
@staticmethod
|
||||
def change_to_static(adapter, ip, netmask, gateway):
|
||||
subprocess.call(['ifconfig', adapter, ip, 'netmask', netmask])
|
||||
subprocess.call(['route add default gw', gateway, adapter])
|
||||
|
||||
@staticmethod
|
||||
def restart_adapter(adapter):
|
||||
subprocess.call(['ifconfig', adapter, 'down'])
|
||||
subprocess.call(['ifconfig', adapter, 'up'])
|
||||
|
||||
@staticmethod
|
||||
def find_adapter():
|
||||
for i_name in netifaces.interfaces():
|
||||
interface = netifaces.ifaddresses(i_name)[netifaces.AF_INET][0]
|
||||
address = interface['addr'].split('.')[0]
|
||||
if address == "10":
|
||||
return str(i_name)
|
||||
|
||||
@staticmethod
|
||||
def change_hostname(hostname):
|
||||
subprocess.call(['hostnamectl set-hostname', "Chameleon-Vision-{}".format(hostname)])
|
||||
Reference in New Issue
Block a user