def get_md5_config(host, username, password, operation=0):
    dict_config = {}
    if operation == 0:  # 如果操作码为0,表示MD5和Config都要获取!
        try:
            # 获取完整的running-configuration
            run_config = QYT_SSHClient_SingleCMD(host, username, password,
                                                 'show run')
            # 下面部分在做running-configuration的裁剪操作,只留hostname开始的配置
            list_run_config = run_config.split('\r\n')
            location = 0
            host_location = 0
            for i in list_run_config:
                if re.match('.*hostname .*', i):
                    host_location = location  # 定位hostname所在位置
                else:
                    location += 1
            list_run_config = list_run_config[
                host_location:]  # 截取hostname开始往后的部分
            run_config = '\r\n'.join(list_run_config)  # 再次还原为字串形式的配置
            # 获取配置的md5值
            md5 = QYT_SSHClient_SingleCMD(host, username, password,
                                          'verify /md5 system:running-config')
            dict_config[host] = [run_config, md5.strip()[-32:]]
        # 仅仅截取最后32位的MD5值
        # 返回字典
        except Exception as e:
            print('%stErrorn %s' % (host, e))
    elif operation == 1:  # 如果操作码为1,表示只获取Config!
        try:
            run_config = QYT_SSHClient_SingleCMD(host, username, password,
                                                 'show run')
            list_run_config = run_config.split('\r\n')
            location = 0
            host_location = 0
            for i in list_run_config:
                if re.match('.*hostname .*', i):
                    host_location = location
                else:
                    location += 1
            list_run_config = list_run_config[host_location:]
            run_config = '\r\n'.join(list_run_config)
            dict_config[host] = run_config
        except Exception as e:
            print('%stErrorn %s' % (host, e))
    elif operation == 2:  # 如果操作码为1,表示只获取MD5值!
        try:
            md5 = QYT_SSHClient_SingleCMD(host, username, password,
                                          'verify /md5 system:running-config')
            dict_config[host] = md5.strip()[-32:]
        except Exception as e:
            print('%stErrorn %s' % (host, e))
    else:
        print('操作码传入错误!')
    return dict_config
Beispiel #2
0
def monitor_sshd(ip):
    username = '******'
    password = '******'
    result = QYT_SSHClient_SingleCMD(ip, username, password, 'systemctl status sshd')
    result_list = result.split('\n')
    for x in result_list:
        if x.split()[0] == 'Active:':
            #print(x)
            return x.split()[1] + x.split()[2]
def excel_ios_user_to_excel(ip, username, password, excelfile):
    # 执行'sh run | in username'并提取结果
    show_run = QYT_SSHClient_SingleCMD(ip, username, password, 'sh run | in username')
    # 把结果通过'\r\n'分离,产生清单
    show_run_list = show_run.split('\r\n')
    user_dict = {}
    for x in show_run_list:
        # 如果格式为username admin privilege 15 password 0 cisco,提取用户名,密码和级别
        if re.match('username (\w+) privilege (\d+) password \w (\w+)', x):
            re_result = re.match('username (\w+) privilege (\d+) password \w (\w+)', x).groups()
            user_dict[re_result[0]] = re_result[2], int(re_result[1])
        # 如果格式为username passuser password 0 12345,提取用户和密码,级别为1级
        elif re.match('username (\w+) password \w (\w+)', x):
            re_result = re.match('username (\w+) password \w (\w+)', x).groups()
            user_dict[re_result[0]] = re_result[1], 1
    # 把字典的用户名,密码和级别信息,写入Excel
    # sheel_name为IP地址
    excel_write(file=excelfile, sheel_name=ip, write_dict=user_dict)