init import

This commit is contained in:
bsdbc
2019-04-02 09:52:52 +08:00
parent 48f09fa63d
commit 4f1a03aeb9
122 changed files with 23956 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,231 @@
#!/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.system(
'route add -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] + ' ' +
data['interface'])
result2 = os.system(
'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><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><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><C2B7>
result = os.system(
'route add -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] +' '+data['interface'])
# <20><><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><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.system(
'route del -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] + ' ' +
data['interface'])
# <20><><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><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 and not('.' in file)):
with open(path + file, 'a+') as f:
result = {}
content = f.readline()
while (content.strip()):
if (not (content.strip().startswith('#'))):
res = content.split('=')
result[res[0]] = res[1].rstrip('\n').rstrip()
content = f.readline()
if(result):
if('IPADDR' in result.keys()):
if(not result['IPADDR'].strip()==''):
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") == 'lo'):
break
list.append(i.strip("\n"))
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.system('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)

Binary file not shown.

View File

@@ -0,0 +1,231 @@
#!/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.system(
'route add -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] + ' ' +
data['interface'])
result2 = os.system(
'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><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><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><C2B7>
result = os.system(
'route add -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] +' '+data['interface'])
# <20><><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><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.system(
'route del -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] + ' ' +
data['interface'])
# <20><><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><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 and not('.' in file)):
with open(path + file, 'a+') as f:
result = {}
content = f.readline()
while (content.strip()):
if (not (content.strip().startswith('#'))):
res = content.split('=')
result[res[0]] = res[1].rstrip('\n').rstrip()
content = f.readline()
if(result):
if('IPADDR' in result.keys()):
if(not result['IPADDR'].strip()==''):
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") == 'lo'):
break
list.append(i.strip("\n"))
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.system('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)

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,231 @@
#!/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.system(
'route add -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] + ' ' +
data['interface'])
result2 = os.system(
'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><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><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><C2B7>
result = os.system(
'route add -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] +' '+data['interface'])
# <20><><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><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.system(
'route del -net ' + data['destination_ip'] + ' netmask ' + data['ip_mask'] + ' gw ' + data['gateway'] + ' ' +
data['interface'])
# <20><><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><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 and not('.' in file)):
with open(path + file, 'a+') as f:
result = {}
content = f.readline()
while (content.strip()):
if (not (content.strip().startswith('#'))):
res = content.split('=')
result[res[0]] = res[1].rstrip('\n').rstrip()
content = f.readline()
if(result):
if('IPADDR' in result.keys()):
if(not result['IPADDR'].strip()==''):
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") == 'lo'):
break
list.append(i.strip("\n"))
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.system('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)

Binary file not shown.

Binary file not shown.

344
oam/oam_install/install.sh Normal file
View File

@@ -0,0 +1,344 @@
#!/bin/bash
#trap 'rm -rf /home/ceiec/oam;rm -rf /home/ceiec/collector;' EXIT
#带颜色输出内容
#$1 颜色 red green yellow 警告 red 成功 green 提示/输入 yellow
#$2 输出语句
#$3 -n y/n 是否换行输出
function pinfo(){
LINE=""
if [[ "$#" -gt 2 ]]; then
#statements
if [[ "$3" = "n" ]]; then
#statements
LINE="-n"
fi
fi
case $1 in
"red")
echo -e $LINE "\e[1;31m "${2}"\e[0m"
;;
"green")
echo -e $LINE "\e[1;32m "${2}"\e[0m"
;;
"yellow")
echo -e $LINE "\e[1;33m "${2}"\e[0m"
;;
"blue")
echo -e $LINE "\e[1;34m "${2}"\e[0m"
;;
*)
echo $LINE $2
;;
esac
}
READ_NOTNULL_TEMP=""
function read_notNull(){
echo -n "$1"
stty erase '^H'
read READ_NOTNULL_TEMP
length=$#
if [[ length -gt 1 ]]; then #参数大于1个验证输入是否正确
#statements
FLAG=0
while [[ true ]]; do
#statements
for i in "$@"; do
if [[ "$READ_NOTNULL_TEMP" = "$i" ]]; then
#statements
FLAG=1
break
fi
done
if [[ "$FLAG" = 1 ]]; then
#statements
break
else
echo "input failed,please input again"
echo -n "$1"
read READ_NOTNULL_TEMP
fi
done
else
while [ -z "$READ_NOTNULL_TEMP" ]
do
echo "input is empty,please input again"
echo -n "$1"
read READ_NOTNULL_TEMP
done
fi
}
function modify_file(){
if [ $# != 2 ]
then
echo "usage: modify_file [prop_name] [prop_value]"
exit 0
fi
prop_name="$1"
prop_value="$2"
echo "modify_file $PROP_FILE: $prop_name $prop_value"
if [ -z "$(cat $PROP_FILE |grep $prop_name)" ]
then
echo "" >> $PROP_FILE
echo "$prop_name=$prop_value" >> $PROP_FILE
else
sed -i "s|^$prop_name.*|$prop_name=$prop_value|" $PROP_FILE
fi
}
#当前目录
temp=`dirname $0`
CURDIR=`cd $temp;pwd`
#设备类型
DEVICE='ADC-A016'
#java打包程序路径
PROC_PATH=${CURDIR}"/process/oam.zip"
#脚本及配置文件路径
SCRIPT_HOME=${CURDIR}"/A016/master/script"
#安装路径
OAM_HOME="/home/ceiec"
#配置文件路径
CONFIG_FILE="/home/ceiec/oam/conf/config.properties"
read_notNull 'please input device type[ASEM-T102/ADC-A016](t/a):' t a
device_type="$READ_NOTNULL_TEMP"
is_master="1"
#安装类型标志 1 A106 master 2 A106 slave 3 T102
role=-1
DB_FILE="oam.db.A016"
if [ "t" = "$device_type" ];then
DEVICE="ASEM-T102"
SCRIPT_HOME=${CURDIR}"/T102/script"
DB_FILE="oam.db.T102"
role=3
else
role=1
read_notNull 'please input install as master or slave (m/s)' m s
flag="$READ_NOTNULL_TEMP"
#echo "$flag"
if [ "s" = "$flag" ];then
is_master="0"
role=2
fi
fi
#echo "is_master:$is_master"
#安装java程序
if [ "$is_master" = "1" ];then
pinfo green "install oam program..."
unzip $PROC_PATH -d $OAM_HOME >/dev/null
if [ $? = 1 ];then
pinfo red "install FAILD."
exit 0
fi
PROP_FILE="$CONFIG_FILE"
modify_file 'equipment_name' $DEVICE
cp ${CURDIR}"/db/$DB_FILE" ${OAM_HOME}"/oam/db/oam.db"
read_notNull 'start oam right now?(y/n)' y n
start="$READ_NOTNULL_TEMP"
if [ "$start" = 'y' ];then
pinfo green "now start program..."
bash ${OAM_HOME}"/oam/shell/startup.sh" >/dev/null 2>$1
if [ $? = 0 ];then
pinfo green "start SUCCESS"
else
pinfo red "start FAILD.check \"$OAM_HOME/oam/log/gloam.log\" for help..."
exit 0
fi
else
echo "please start program with \"$OAM_HOME/oam/shell/startup.sh\""
fi
fi
enter_password=""
function readPasswd(){
enter_password=""
stty -echo cbreak erase '^H'
while true
do
character=$(dd if=/dev/tty bs=1 count=1 2> /dev/null)
case $character in
$(echo -e "\n"))
break
;;
$(echo -e "\b"))
if [ -z "$enter_password" ]; then
echo -n -e "\b \b"
enter_password=$(echo "$password" | sed 's/.$//g')
fi
;;
*)
enter_password=$enter_password$character
echo -n '*'
;;
esac
done
stty -cbreak echo
echo ""
}
enter_info=""
function readInfo_notNull(){
if [ $# != 1 ];then
echo "usage:readInfo_notNull [hint]"
exit 1
fi
hint="$1"
stty erase '^H'
read -p "$hint:" enter_info
while [ -z "enter_info" ];do
read -p "this parameter is must,please input again:" enter_info
done
}
NODE_ID=""
function getNodeIdByIP(){
if [ $# != 1 ];then
echo "usage:getNodeIdByIP [node_ip]"
exit 1
fi
node_ip="$1"
NODE_ID=$(sqlite3 ${CURDIR}"/db/$DB_FILE" <<EOF |awk '{print $0}'
select id from node_table where ip="$node_ip";
EOF
)
echo "NODE_ID=$NODE_ID"
}
#安装脚本程序方法
function installScript(){
if [ $# != 1 ];then
echo "usage:installScript [script_dir]"
fi
script_dir=$1
read -p "please input script file home[default /home/ceiec]:" script_home
if [ -z "$script_home" ];then
script_home="/home/ceiec"
else
if [ ! -d "$script_home" ];then
mkdir -p "$script_home"
fi
fi
#echo "$script_home"
unzip ${script_dir}"/collector.zip" -d "$script_home" >/dev/null
if [ $? != 0 ];then
pinfo red "script file unzip FAILD"
exit 1
fi
cd ${script_home}"/collector"
#修改主机配置文件config
if [[ -f config ]];then
for i in 1 2 3 4
do
ipinfo=$(sqlite3 ${CURDIR}"/db/"${DB_FILE} <<EOF |awk -va="$i" 'NR==a{print $0}'
select ip||","||user||","||pwd from node_table;
EOF
)
#写入配置
PROP_FILE="$script_home/collector/config"
modify_file 'ip'${i} "$ipinfo"
if [ "t" = "$device_type" ];then
break
fi
done
#key src_dir
srcDir="$script_home"
#key dest_dir
destDir="$script_home"
#写入配置
PROP_FILE="$script_home/collector/config"
modify_file 'src_dir' "$srcDir"
modify_file 'dest_dir' "$destDir"
fi
#为脚本授权
chmod +x *.sh
if [ -f expect_scp ];then
chmod +x expect_scp
fi
#新建定时任务
if [ -z "$(grep "$script_home/collector/getSingleMacInfo.sh" /var/spool/cron/root | grep -v 'grep' 2>/dev/null)" ];then
echo "*/5 * * * * $script_home/collector/getSingleMacInfo.sh" >>/var/spool/cron/root
fi
if [[ -f ${script_home}"/collector/merge.sh" && -z "$(grep "$script_home/collector/merge.sh" /var/spool/cron/root |grep -v 'grep' 2>/dev/null)" ]];then
echo "*/1 * * * * $script_home/collector/merge.sh">>/var/spool/cron/root
fi
#处理configs目录下配置文件
cd ${script_home}"/collector/configs"
<<UNUSE
cpu="cpu_cpu.cfg"
disk="disk_disk.cfg"
mem="memory_memory.cfg"
net="net_net.cfg"
sys="systeminfo_servicessysinfo.cfg"
UNUSE
#获取node_table的id
readInfo_notNull "please input the localhost"
#只针对ADC-A016
ip="$enter_info"
getNodeIdByIP "$ip"
while [ -z "$NODE_ID" ];do
#获取node_table的id
readInfo_notNull "this ip is not exist,please input the device ip again"
#只针对ADC-A016
ip="$enter_info"
getNodeIdByIP "$ip"
done
node_id="$NODE_ID"
for file in *
do
temp=$(awk 'NR==1{print $0}' $file|cut -d'=' -f2)
value=${node_id}${temp}
PROP_FILE=$(readlink -f "$file")
modify_file 'pubInfo' "$value"
done
#处理device_config 脚本
if [ ! -f '/usr/local/bin/device_config' ];then
cp $script_dir/device_config /usr/local/bin
if [ $? = 0 ];then
chmod +x /usr/local/bin/device_config
fi
fi
}
#安装脚本程序
case $role in
1 )
installScript "$SCRIPT_HOME"
;;
2 )
SCRIPT_HOME="${CURDIR}/A016/slave"
installScript "$SCRIPT_HOME"
;;
3 )
SCRIPT_HOME="${CURDIR}/T102/script"
installScript "$SCRIPT_HOME"
;;
* )
pinfo red "`basename $0` exec error "
exit 1
;;
esac

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.