This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nms-oam/gloam_shell/device_config

229 lines
7.3 KiB
Plaintext
Raw Normal View History

2018-09-27 16:28:35 +08:00
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# created by zhangbin
from sys import argv
import json
import os
from pprint import pprint
import shutil
def update_route(data):
list = []
route_path = '/etc/sysconfig/static-routes'
requestOldParam = 'any net ' + data['destination_ip_old'] + ' netmask ' + data['ip_mask_old'] + ' gw ' + data['gateway_old'] + ' ' + \
data['interface']
result = os.popen(
'route add -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] + ' ' +
data['interface'])
result2 = os.popen(
'route del -net ' + data['destination_ip_old'] + ' netmask ' + data['ip_mask_old'] + ' gw ' + data['gateway_old'] + ' ' +
data['interface'])
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
if (result != 0):
print(result)
return
if (result2 != 0):
print(result)
return
# <20>޸<EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>Ϣ
with open(route_path, 'r+') as f_route:
lines = f_route.readlines()
for line in reversed(lines):
if (requestOldParam in line):
list.append('any net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] + ' ' + \
data['interface'] + '\n')
else:
list.append(line)
f_route.seek(0)
f_route.truncate()
f_route.writelines(list)
def new_route(data):
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
result = os.popen(
'route add -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] +' '+data['interface'])
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
if (result != 0):
print(result)
return
route_path = '/etc/sysconfig/static-routes'
# <20><><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>ӵ<EFBFBD><D3B5>ļ<EFBFBD><C4BC><EFBFBD>
result = os.path.exists(route_path)
if (result == False):
with open(route_path, 'w') as f:
f.close()
with open(route_path, 'r+') as f:
strData = 'any net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] + ' ' + \
data['interface']
f.seek(0, 2)
f.write('\n' + strData)
def remove_route(data):
<><C9BE>·<EFBFBD><C2B7><EFBFBD><EFBFBD>Ϣ
route_path = '/etc/sysconfig/static-routes'
list = []
requestParam='any net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] + ' ' + data['interface']
result = os.popen(
'route del -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] + ' ' +
data['interface'])
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
if (result != 0):
print(result)
return
with open(route_path, 'r+')as f_route:
lines = f_route.readlines()
for line in lines:
if (not requestParam in line):
list.append(line)
f_route.seek(0)
f_route.truncate()
f_route.writelines(list)
# <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
def exchange_maskint(mask_int):
bin_arr = ['0' for i in range(32)]
for i in range(mask_int):
bin_arr[i] = '1'
tmpmask = [''.join(bin_arr[i * 8:i * 8 + 8]) for i in range(4)]
tmpmask = [str(int(tmpstr, 2)) for tmpstr in tmpmask]
return '.'.join(tmpmask)
# <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>·<EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD>Ϣ
def query_route():
path = '/etc/sysconfig/static-routes'
list = []
if(os.path.exists(path)):
with open(path, 'a+') as f:
content=f.readlines()
for i in content:
if(not i.strip('\n')==''):
data=i.split(' ')
result = {}
result['destination_ip'] = data[2]
result['ip_mask'] = data[4]
result['gateway'] = data[6]
result['r_interface'] = data[7]
if(result):
list.append(result)
return list
# <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>·<EFBFBD>ɵ<EFBFBD><C9B5><EFBFBD>Ϣ
def query_single_route(data): # <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2>list
path = '/etc/sysconfig/network-scripts/route-'
list = []
with open(path + data, 'a+') as f:
content = f.readline()
a = content.split()
b = a[0].split('/')
result = {}
result['destination_ip'] = b[0]
result['ip_mask'] = exchange_maskint(int(b[1]))
result['gateway'] = a[2]
result['r_interface'] = a[4]
if(result):
list.append(result)
return list
# <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>Ϣ
def query_all_config():
path = '/etc/sysconfig/network-scripts/'
list = []
for file in os.listdir(path):
if ('ifcfg-e' in file):
with open(path + file, 'a+') as f:
result = {}
content = f.readline()
while (content):
res = content.split('=')
result[res[0]] = res[1].rstrip('\n').rstrip()
content = f.readline()
if(result):
list.append(result)
return list
'''
def query_all_config():
networknames=get_networkName()
path = '/etc/sysconfig/network-scripts/ifcfg-'
list = []
for networkname in networknames:
with open(path + networkname, 'a+') as f:
result = {}
content = f.readline()
while (content):
res = content.split('=')
result[res[0]] = res[1].rstrip('\n').rstrip()
content = f.readline()
list.append(result)
return list
'''
#<23><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
def get_networkName():
result = os.popen("ifconfig | awk '/^[a-z]/{print $1}'").readlines()
list = []
for i in result:
if ((i.strip("\n")).strip(":") == 'lo'):
continue
list.append((i.strip("\n")).strip(":"))
return list
# <20>޸<EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
def modify_config(data):
path = '/etc/sysconfig/network-scripts/'
path2 = '/tmp/'
# list<73><74>ΪN<CEAA><4E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
list = data['request']
for i in range(len(list)):
dic = {}
if (not (':' in list[i]['DEVICE'])):
with open(path + 'ifcfg-' + list[i]['DEVICE'], 'r+') as f:
for line in f:
(key, value) = line.strip().split('=')
dic[key] = value
dic['HWADDR'] = list[i]['HWADDR']
dic['NETMASK'] = list[i]['NETMASK']
dic['GATEWAY'] = list[i]['GATEWAY']
dic['IPADDR'] = list[i]['IPADDR']
list2 = []
for key in dic:
list2.append(key + '=' + dic[key] + '\n')
with open(path2 + 'ifcfg-' + list[i]['DEVICE'], 'w+') as f2:
f2.writelines(list2)
if (os.path.exists(path + 'ifcfg-' + list[i]['DEVICE'])):
os.remove(path + 'ifcfg-' + list[i]['DEVICE'])
shutil.move(path2 + 'ifcfg-' + list[i]['DEVICE'], path)
# <20>޸<EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
os.popen('service network restart')
if __name__ == '__main__':
path_manage = '/etc/sysconfig/network-scripts/ifcfg-unknown2'
data = json.loads(argv[1])[0]
if data['type'] == '1':
list=get_networkName()
if(list):
print(json.dumps(list))
elif data['type'] == '2':
update_route(data)
elif data['type'] == '3':
remove_route(data)
elif data['type'] == '4':
new_route(data)
elif data['type'] == '5':
list = query_route()
if(list):
print(json.dumps(list))
elif data['type'] == '6':
modify_config(data)
elif data['type'] == '7':
list = query_all_config()
if(list):
print(json.dumps(list))
exit(0)