利用Telnet连接对探针进行备份

对探针进行备份功能

#!/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()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值