def gather_info(device, path):
    data = dict()
    conn = IOSXEDriver(**device)
    conn.open()
    # выполняем необходимые команды
    show_version = conn.send_command("show version")
    config = conn.send_command("show running-config")
    show_cdp = conn.send_command("show cdp neighbors")
    conn.send_configs(NTP_COMMANDS)
    show_ntp = conn.send_command("show ntp status")
    conn.close()

    # записываем инфу для вывода
    data["hostname"] = re.search(r"\s*(\S+)\s+uptime\s+is", show_version.result).group(1)
    data["model"] = re.search(r"[Cc]isco\s+(\S+)\s+\(", show_version.result).group(1)
    data["software"] = re.search(r".*Software\s+\((?P<package>\S+)\),\s+Version\s+(?P<version>\S+),\s+", show_version.result).groupdict()

    # выяснем NPE или PE
    if re.search(r"NPE", data["software"]["package"]):
        data["payload"] = "NPE"
    else:
        data["payload"] = "PE"

    # делаем бэкап
    backup_path = os.path.join(path, 'backups')
    now = datetime.datetime.now()
    if not os.path.exists(backup_path):
        os.mkdir(backup_path)
    with open(f"{backup_path}/{data['hostname']}-{now.strftime('%Y_%m_%d-%H_%M_%S')}.conf", "w") as input:
        input.write(config.result)

    # проверяем cdp
    if re.search(r"cdp\s+is\s+not\s+enabled", show_cdp.result, re.IGNORECASE):
        data["cdp_status"] = "CDP is OFF"
        data["cdp_peers"] = "0 peers"
    else:
        data["cdp_status"] = "CDP is ON"
        peers = re.search(r"total\s+cdp\s+entries\s+displayed\s*:\s*(\d+)", show_cdp.result, re.IGNORECASE)
        data["cdp_peers"] = f"{peers.group(1)} peers"
    
    # проверяем ntp
    if re.search(r"clock\s+is\s+unsynch", show_ntp.result, re.IGNORECASE):
        data["ntp_status"] = "Clock not Sync"
    elif re.search(r"clock\s+is\s+synch", show_ntp.result, re.IGNORECASE):
        data["ntp_status"] = "Clock in Sync"
    elif re.search(r"ntp\s+is\s+not\s+enabled", show_ntp.result, re.IGNORECASE):
        data["ntp_status"] = "Clock not Sync"
    print(f"{data['hostname']}|{data['model']}|{data['software']['package']} {data['software']['version']}|{data['payload']}|{data['cdp_status']}, {data['cdp_peers']}|{data['ntp_status']}")
示例#2
0
    "auth_username": "******",
    "auth_password": "******",
    "auth_strict_key": False
}, {
    "host": "sbx-iosxr-mgmt.cisco.com",
    "auth_username": "******",
    "auth_password": "******",
    "port": 8181,
    "auth_strict_key": False
}]

os.system("clear")

conn_XE = IOSXEDriver(**devices[0])
conn_XE.open()
response = conn_XE.send_command("show ip int brief")
print("RESPONSE")
print("*" * 100)
print(response.result)
conn_XE.close()

conn_XR = IOSXRDriver(**devices[1])
conn_XR.open()
response = conn_XR.send_command("show ip int brief")

print("RESPONSE")
print("*" * 100)
# print(response.result)
# print(re.findall("Giga.+", response.result))
print(json.dumps(re.findall("Giga.+", response.result), indent=2))
conn_XR.close()
示例#3
0
}

conn = IOSXEDriver(**args)
conn.open()

print("***** Get Prompt:")
print(conn.get_prompt())

print("***** Show run | i hostname:")
result = conn.send_commands("show run | i hostname")
print(result, result[0].result)

print("***** Clear logging buffer:")
interact = [("clear logg", "Clear logging buffer [confirm]"), ("", "3560CX#")]
result = conn.send_interactive(interact)
print(result, result[0].result)

print("***** Disable Paging:")
result = conn.send_commands("term length 0")
print(result, result[0].result)

print("***** Show run:")
result = conn.send_commands("show run")
print(result, result[0].result)

if device["keepalive"]:
    print("***** Waiting for keepalive....")
    time.sleep(5)

conn.close()