1

下面是我的工作代码,用于在 Cisco 路由器上显示我的路由器 arp 表。

我手头有一项任务,要求我从“sh arp”命令的输出中读取特定值,并将其用作要在 Cisco 路由器上执行的另一个命令的输入。

路由器中的命令:

1- sh arp vrf 互联网 | 公司 0835.71

2- ping vrf 互联网 100.124.162.230**

我已经完成了第 1 步。我可以得到输出,但我需要 hep 来完成第 2 步,以捕获 IP“100.124.162.230 对其运行 ping。

print(" ping NMD....")

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    ssh.connect("r-a" ,timeout = 10)
    chan = ssh.invoke_shell()
    time.sleep(.1)
    chan.send('sh arp vrf INTERNET | INC  0835.71 \n')
    time.sleep(20)


    #Print command output
    while chan.recv_ready():
            output = str(chan.recv(99999999))
            output.encode('utf-8')
            #print(type(output))
            #print("\n\n\nPrinting total output: {0}".format(output))

输出:

ra>sh arp vrf 互联网 | INC 0835.71 互联网 100.124.162.230 74 0835.71ef.d0a1 ARPA GigabitEthernet0/0.901

2-下一步是获取IP并对其运行ping,请帮助。

我提取了我的 IP 地址,现在我想对提取的 IP 运行 ping,我如何在 python 中执行此操作。请帮忙 ..

ra>sh arp vrf 互联网 | inc 0835 互联网 100.114.162.230 110 0835.71ef.d0a1 ARPA GigabitEthernet0/0.901 ra.> ['100.114.162.230']

    ssh.connect("r-a" + name + ".us", username = 'cisco', password = '1234', timeout = 10)
    chan = ssh.invoke_shell()
    time.sleep(.1)
    chan.send('sh arp vrf INTERNET' + ' ' + '|'+ ' ' + 'inc 0835' '\n')
    time.sleep(2)
    chan.send('ping' + ' ' + 'ip' '\n' )      

    #Print command output
    while chan.recv_ready():
            output = str(chan.recv(99999999))
            output.encode('utf-8')
            print("\n\n\nPrinting total output: {0}".format(output))
    ip = re.findall(r'100.\d+\d+.\d+.\d',output)
    print(ip)
4

1 回答 1

0

像这样的东西可能会起作用,但是如果你能够 ssh 到它,我可能又错过了一些东西?:

_, stdout, _ = ssh.exec_command('hostname')
hostname = stdout.read()
ssh.close()
HOST_UP  = True if os.system("ping -c 1 " + hostname) is 0 else False

有关的:

于 2017-06-18T23:43:38.557 回答