1

Below is my working code which is used to show my router arp table on Cisco Routers.

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.

commands in the router:

1- sh arp vrf INTERNET | INC 0835.71

2- ping vrf INTERNET 100.124.162.230**

I have step 1 done . 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.

print(" Pinging 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:

r-a>sh arp vrf INTERNET | 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.

I extracted my IP address, now I want to run a ping to the extracted IP, HOW can I do this in python. Please help ..

r-a>sh arp vrf INTERNET | inc 0835 Internet 100.114.162.230 110 0835.71ef.d0a1 ARPA GigabitEthernet0/0.901 r-a.> ['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)
zenki
  • 41
  • 4
  • 2
    Just to make sure, the string you are showing is the result of `output.encode('utf-8')`? – idjaw Jun 18 '17 at 23:15
  • yes, I was able to capture the IP and print it. Now I need to find out a way to get the printer and run a ping to see if its pinging. I have updated the output that I am extracting. – zenki Jun 20 '17 at 12:58

1 Answers1

0

Something like this might work, but then again perhaps I am missing something if you are able to ssh to it?:

_, 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:

jmunsch
  • 22,771
  • 11
  • 93
  • 114
  • yes I am able to ssh , everything works fine, I am new to python and not sure how to get the IP from the arp table and run a ping to it. – zenki Jun 19 '17 at 03:44
  • @zenki your question was a lot more involved than I initially thought. I added a link to some of the ways to get parts of the information using the lower level ioctl calls. – jmunsch Jun 19 '17 at 08:44
  • I extracted my IP address, now I want to run a ping to the extracted IP, HOW can I do this in python. Please help ...... – zenki Jun 19 '17 at 18:11