Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

First, let me tell you that I am a novice and I have tried the below code :

conn = netmiko.ConnectHandler(ip='10.254.60.10', device_type='cisco_ios', 

                                username='user', password='P@ssw0rd')

print (conn.send_command('show interface Ethernet0/0 | i line|Des|Int'))

output like this

Ethernet0/0 is up, line protocol is up Description: CUSTOMER A

The Internet address is 10.254.60.69/30

I want to know how can I auto ping it to IP PtP with the help of conn.send_command() based on the result of the show interface command?

example ping to 10.254.60.70

1 Answer

0 votes
by (108k points)

Let's suppose this is your text:

text = '''Ethernet0/0 is up, line protocol is up Description: CUSTOMER A

Internet address is 10.254.60.70/30'''

and you have the below IP/MASK using string functions:

address = text.split(' ')[-1]

print(address)  # 10.254.60.70/30

and then you can use the usual module ipaddress package:

import ipaddress

net = ipaddress.ip_interface(address)

ip = str(net.network.broadcast_address)

print( ip )   # 10.254.60.71 

Or you can also use the netaddr module:

import netaddr

net = netaddr.IPNetwork(address)

ip = str(net.broadcast)

print( ip )   # 10.254.60.71 

If you want to explore more about Python, then do refer to the python training course. 

Related questions

0 votes
1 answer
asked Feb 16, 2021 in Python by ashely (50.2k points)
0 votes
1 answer
asked Dec 12, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer
asked Dec 17, 2020 in Linux by blackindya (18.4k points)
0 votes
1 answer
asked Nov 21, 2020 in AWS by devin (5.6k points)

Browse Categories

...