使用Paramiko+CiscoConfPars读取远程配置

2024-04-23 12:11:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,它通过api连接到IPAM工具,获取新创建的vlan,然后通过paramiko在远程rancid服务器上执行“deviceq{vlan}”。从这里我得到了VLAN所在设备的字典。你知道吗

函数在这里

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()

这将输出

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

我想在每个配置中搜索vlan并打印它。CiscoConfParse允许搜索

parse.find_objects_w_child(r"interface", vlan)

但有没有办法通过帕拉米科远程实现这一点?或者一个更好的解决方案?你知道吗


Tags: keyinclienthostparamikofor远程device