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']}")
from scrapli.driver.core import IOSXEDriver switch = { "host": "192.168.65.137", "auth_username": "******", "auth_password": "******", "auth_strict_key": False } cli = IOSXEDriver(**switch) cli.open() sh_int = cli.send_command("show interface") print(sh_int.output)
"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()
def main(): """Example demonstrating basic logging with scrapli""" conn = IOSXEDriver(**MY_DEVICE) conn.open() print(conn.get_prompt()) print(conn.send_command("show run | i hostname").result)
from scrapli.driver.core import IOSXEDriver device = { "host": "ios-xe-mgmt-latest.cisco.com", "auth_username": "******", "auth_password": "******", "port": 8181, "auth_strict_key": False, } conn = IOSXEDriver(**device) conn.open() response = conn.send_command("show interface Gi3 description") print("*" * 100) print(response.result) conn.send_configs( ["interface GigabitEthernet3", "description Configured by Scrapli"]) response = conn.send_command("show interface Gi3 description") print("*" * 100) print(response.result)
from scrapli.driver.core import IOSXEDriver logging.basicConfig( filename=f"{Path(__file__).resolve().parents[0]}/iosxe_driver.log", level=logging.DEBUG ) logger = logging.getLogger("scrapli") conn = IOSXEDriver(**iosxe_device) conn.open() print("***** Get Prompt:") prompt = conn.get_prompt() print(prompt) print("***** Show run | i hostname:") result = conn.send_command("show run | i hostname") print(result, result.result) print("***** Clear logging buffer:") interact = [("clear logg", "Clear logging buffer [confirm]"), ("", prompt)] result = conn.send_interactive(interact) print(result, result.result) print("***** Show run:") result = conn.send_command("show run") print(result, result.result) if iosxe_device["keepalive"]: print("***** Waiting for keepalive....") time.sleep(2)
from scrapli.driver.core import IOSXEDriver import re import os os.system("clear") device = { "host": "ios-xe-mgmt-latest.cisco.com", "auth_username": "******", "auth_password": "******", "auth_strict_key": False } conn = IOSXEDriver(**device) conn.open() responses = conn.send_command("show version") # print(responses.result) # print(re.findall("Compiled.+", responses.result)) # responses = conn.send_commands(["show version", "show ip int brief"]) # for response in responses: # print(response.result) # conn.close() shver_parsed = responses.genie_parse_output() # print(shver_parsed) hostname = shver_parsed['version']['hostname'] platform = shver_parsed['version']['platform']
import logging from scrapli.driver.core import IOSXEDriver logging.basicConfig(filename="scrapli.log", level=logging.DEBUG) logger = logging.getLogger("scrapli") args = { "host": "172.18.0.11", "auth_username": "******", "auth_password": "******", "auth_strict_key": False, } conn = IOSXEDriver(**args) conn.open() print(conn.get_prompt()) print(conn.send_command("show run | i hostname").result)
from scrapli.driver.core import IOSXEDriver switch = { "host": "172.16.100.168", "auth_username": "******", "auth_password": "******", "auth_strict_key": False } cli = IOSXEDriver(**switch) cli.open() sh_int = cli.send_command("show ip ospf int brief") print(sh_int.result)
from scrapli.driver.core import IOSXEDriver my_device = { "host": "172.18.0.11", "auth_username": "******", "auth_password": "******", "auth_strict_key": False, } conn = IOSXEDriver(**my_device) conn.open() response = conn.send_command("show run") print(response.result)
from scrapli.driver.core import IOSXEDriver from ttp import ttp ttp_template = """ {{hostname}} {{l}} {{int}} {{nei_ip | IP}} {{state}} {{hold}} {{cir}} """ my_device = { "host": "10.10.21.121", "auth_username": "******", "auth_password": "******", "auth_strict_key": False, } conn = IOSXEDriver(**my_device) conn.open() response = conn.send_command("show isis nei") print(response.result) data_to_parse = response.result # create parser object and parse data using template: parser = ttp(data=data_to_parse, template=ttp_template) parser.parse() # print result in JSON format results = parser.result(format='json')[0] print(results)