简体   繁体   English

Python - 从sh命令捕获输出,获取IP地址并运行ping

[英]Python - capture the output from a sh command, obtain IP address and run a ping

Below is my working code which is used to show my router arp table on Cisco Routers. 下面是我的工作代码,用于在Cisco Routers上显示我的路由器arp表。

I have a task at my hand which requires me to read specific values from the output of the "sh arp " command and use it as input for another command to be executed on the Cisco router. 我手头有一个任务,它要求我从“sh arp”命令的输出中读取特定值,并将其用作在Cisco路由器上执行的另一个命令的输入。

commands in the router: 路由器中的命令:

1- sh arp vrf INTERNET | 1- sh arp vrf INTERNET | INC 0835.71 INC 0835.71

2- ping vrf INTERNET 100.124.162.230** 2- ping vrf INTERNET 100.124.162.230 **

I have step 1 done . 我已完成第1步。 I can get the output but I need hep to get step 2 done, to capture the IP "100.124.162.230 run a ping to it. 我可以得到输出,但我需要肝脏来完成第2步,捕获IP“100.124.162.230运行ping它。

print(" Pinging the NMD....") 打印(“Ping the 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))

output: 输出:

ra>sh arp vrf INTERNET | ra> sh arp vrf INTERNET | INC 0835.71 Internet 100.124.162.230 74 0835.71ef.d0a1 ARPA GigabitEthernet0/0.901 INC 0835.71 Internet 100.124.162.230 74 0835.71ef.d0a1 ARPA GigabitEthernet0 / 0.901

2- Next step is to get the IP and run a ping to it please help. 2-下一步是获取IP并运行ping请帮助。

I extracted my IP address, now I want to run a ping to the extracted IP, HOW can I do this in python. 我提取了我的IP地址,现在我想对提取的IP运行ping,我怎么能在python中执行此操作。 Please help .. 请帮忙 ..

ra>sh arp vrf INTERNET | ra> sh arp vrf INTERNET | inc 0835 Internet 100.114.162.230 110 0835.71ef.d0a1 ARPA GigabitEthernet0/0.901 ra.> ['100.114.162.230'] inc 0835 Internet 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)

Something like this might work, but then again perhaps I am missing something if you are able to ssh to it?: 像这样的东西可能有用,但是如果你能够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

Related: 有关:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM