pythonparamikociscoconfparse

Read remote configs using Paramiko + CiscoConfParse


I've a script that connects to an IPAM tool via api, gets newly created vlans and then performs 'device q {vlan}' on a remote rancid server via paramiko. From this I get a dictionary of devices where the vlans are present.

Function for that is here

def check_rancid_for_vlan():
vlans = get_vlans()

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(rancid_host, username=username, password=password)

results = {}
for i in vlans:
    stdin, stdout, stderr = client.exec_command('cd /var/home/configs; device q Vlan{}'.format(i))

    for line in stdout:
        if "no" not in line:
            device = line.split(":")[0]
            if i in results:
                results[device].append(i)
            else:
                results[device] = [i]

for key, val in results.iteritems():
    for i in val:
        print(key,i)

client.close()

This will output

(u'switch1', 1001)
(u'switch2', 1002)
(u'switch3', 1003)
(u'switch4', 1004)

I want to search each config for the vlan and print it. CiscoConfParse allows for searching an interface with

parse.find_objects_w_child(r"interface", vlan)

but is there any way to do this remotely over paramiko? Or a better solution potentially?


Solution

  • Solution was to copy the config directory from remote to local and perform parse there