对探针进行备份功能
#!/usr/bin/python
# encoding:utf-8
'''collect config of TMA
@author : wd
@license : Copyright@haohandata
@version : 1.0
@todo : created in 2019-05-08
'''
import time
import get_ip_list
import os
import re
import sys
import socket
plugins_path = os.getenv('INSPECTOR_HOME') + '/plugins'
sys.path.append(plugins_path + '/common/')
import tools
import telnetlib
config = tools.get_file_config_from_type('install')
host_ip = config.get('dispatcher', 'tftp_server')
tftp_file_path = config.get('dispatcher', 'tftp_server_dir')
def collect_config_tma(ip, host_ip, timestamp):
ftp_user = config.get('dispatcher', 'ftp_user')
ftp_passwd = config.get('dispatcher', 'ftp_password')
cmds = ['Begin uploadConfigFile\r\n',
'add,{0},{1},{2},{3},1\r\n'.format(
host_ip,
ftp_user,
ftp_passwd,
tftp_file_path),'End uploadConfigFile items=1\r\n']
try:
tn = telnetlib.Telnet(ip, port=6011)
#tn.set_debuglevel(2) 打印
tn.write('hello \r\n') #进行握手
tn.read_until('hello')
for cmd in cmds :
tn.write(cmd)
time.sleep(float(1))
flag=tn.read_until('OK',timeout=1)
if 'OK' in flag:
flag=flag.replace('\n','-').split('-')[1] #解决换行问题
pattern=re.compile(r"([\w\d._]*)\s+(\w+)",re.IGNORECASE)
res1=re.match(pattern,flag)
file_name=res1.groups()[0]
new_file_name = '{0}_{1}.cfg'.format(
ip,
timestamp
)
print '{0}\t{1}\t{2}'.format(ip, 'OK', new_file_name)
# 成功后将文件进行重命名
os.rename(os.path.join(tftp_file_path, file_name),
os.path.join(tftp_file_path, new_file_name))
break
else:
pass
except EOFError as e:
print e
except socket.error as s:
print s
finally:
tn.close()
sys.exit(0)
def main(ip='', timestamp=''):
if ip == '':
tma_ip_list = get_ip_list.get_ip_list('TMA')
else:
tma_ip_list = [ip]
if timestamp == '':
timestamp = time.strftime('%s', time.localtime())
for ip in tma_ip_list:
if ip !='' :
collect_config_tma(ip, host_ip, timestamp)
else:
pass
if __name__ == "__main__":
main()