2024-09-27 09:16:37 +00:00
|
|
|
__author__ = 'RemiZOffAlex'
|
|
|
|
|
__email__ = 'remizoffalex@mail.ru'
|
|
|
|
|
|
2024-09-27 15:28:01 +00:00
|
|
|
import yaml
|
2024-09-27 09:16:37 +00:00
|
|
|
import npyscreen
|
2024-09-27 15:28:01 +00:00
|
|
|
|
2024-09-27 09:16:37 +00:00
|
|
|
from pyroute2 import NDB
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ndb = NDB()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def network_config(ip, mask, gateway):
|
|
|
|
|
# ip = IPRoute()
|
|
|
|
|
# index = ip.link_lookup(ifname='em1')[0]
|
|
|
|
|
# ip.addr('add', index, address='192.168.0.1', mask=24)
|
|
|
|
|
# ip.close()
|
|
|
|
|
# ip = IPDB()
|
|
|
|
|
# with ip.interfaces.br0 as br0:
|
|
|
|
|
# em1.add_ip('{ip}/{mask}'.format(ip=ip, mask=mask))
|
|
|
|
|
# ip.release()
|
|
|
|
|
with ndb.interfaces['br0'] as br0:
|
2024-09-27 15:28:01 +00:00
|
|
|
address = '{ip}/{mask}'.format(ip=ip, mask=mask)
|
|
|
|
|
br0.add_ip(address).commit()
|
2024-09-27 09:16:37 +00:00
|
|
|
|
|
|
|
|
ndb.routes.create(dst='0.0.0.0/0', gateway=gateway).commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def TestApp(*args):
|
|
|
|
|
# These lines create the form and populate it with widgets.
|
|
|
|
|
# A fairly complex screen in only 8 or so lines of code - a line for each control.
|
2024-09-27 15:28:01 +00:00
|
|
|
NetworkView = npyscreen.ActionForm(name = "SaaS",)
|
|
|
|
|
ip = NetworkView.add(npyscreen.TitleText, name = "IP:",)
|
|
|
|
|
mask = NetworkView.add(npyscreen.TitleText, name = "Netmask:",)
|
|
|
|
|
gateway = NetworkView.add(npyscreen.TitleText, name = "Gateway:",)
|
2024-09-27 09:16:37 +00:00
|
|
|
|
|
|
|
|
# This lets the user interact with the Form.
|
2024-09-27 15:28:01 +00:00
|
|
|
NetworkView.edit()
|
2024-09-27 09:16:37 +00:00
|
|
|
|
2024-09-27 15:28:01 +00:00
|
|
|
data = {
|
2024-09-27 09:16:37 +00:00
|
|
|
'ip': ip.value,
|
|
|
|
|
'mask': mask.value,
|
2024-09-27 15:28:01 +00:00
|
|
|
'gateway': gateway.value
|
2024-09-27 09:16:37 +00:00
|
|
|
}
|
2024-09-27 15:28:01 +00:00
|
|
|
with open('/root/network.yaml', 'w') as fd:
|
|
|
|
|
yaml.dump(data, fd)
|
|
|
|
|
network_config(**data)
|
|
|
|
|
return data
|