04-20-2020 08:51 AM - edited 04-21-2020 04:21 AM
This code almost perfect, however the 2nd numbering is not working as expected.
user@linux:~$ cat ip.txt 10.1.1.1 10.2.2.2 10.3.3.3 user@linux:~$
script.py
from netmiko import Netmiko with open('ip.txt') as f: print('List of Hosts') print('-' * 13) for x,y in enumerate(f.read().split(), 1): print(f'{x} - {y}') f.seek(0) ip_list = f.read().splitlines() print('\nNo Hostname \t IP Address') print('-' * 27) for ip in ip_list: net_connect = Netmiko(ip=ip, device_type='cisco_ios', username='u',password='p',secret='p') hostname = net_connect.find_prompt()[:-1] print(f'{x} {hostname} \t {ip}')
Output
user@linux:~$ python3 script.py List of Hosts ------------- 1 - 10.1.1.1 2 - 10.2.2.2 3 - 10.3.3.3 No Hostname IP Addressi --------------------------- 3 R1 10.1.1.1 3 R2 20.2.2.2 3 R3 30.3.3.3 user@linux:~$
Desired Output
user@linux:~$ python3 script.py List of Hosts ------------- 1 - 10.1.1.1 2 - 10.2.2.2 3 - 10.3.3.3 No Hostname IP Addressi --------------------------- 1 R1 10.1.1.1 2 R2 20.2.2.2 3 R3 30.3.3.3 user@linux:~$
UPDATE
As advised by ngkin2010, the IP addresses have been stored into an list instead of being read twice.
Here is the updated code
from netmiko import Netmiko with open('ip.txt') as f: ip_list = f.read().split() print('List of Hosts') print('-' * 13) for x,y in enumerate(ip_list, 1): print(f'{x} - {y}') print('\nNo Hostname \t IP Address') print('-' * 27) for x, ip in enumerate(ip_list, 1): net_connect = Netmiko(ip=ip, device_type='cisco_ios', username='u',password='p',secret='p') hostname = net_connect.find_prompt()[:-1] print(f'{x} {hostname} \t {ip}')
Solved! Go to Solution.
04-20-2020 09:11 AM - edited 04-20-2020 09:15 AM
from netmiko import Netmiko with open('ip.txt') as f: ....print('List of Hosts') ....print('-' * 13) ....for x,y in enumerate(f.read().split(), 1): ........print(f'{x} - {y}') ....f.seek(0) ....ip_list = f.read().splitlines() ....print('\nNo Hostname \t IP Address') ....print('-' * 27) ....for x,ip in enumerate(ip_list,1): ........net_connect = Netmiko(ip=ip, device_type='cisco_ios', username='u',password='p',secret='p') ........hostname = net_connect.find_prompt()[:-1] ........print(f'{x} {hostname} \t {ip}')
Btw, why would prefer to read the file twist while not just store the IP addresses into an list?
04-20-2020 09:11 AM - edited 04-20-2020 09:15 AM
from netmiko import Netmiko with open('ip.txt') as f: ....print('List of Hosts') ....print('-' * 13) ....for x,y in enumerate(f.read().split(), 1): ........print(f'{x} - {y}') ....f.seek(0) ....ip_list = f.read().splitlines() ....print('\nNo Hostname \t IP Address') ....print('-' * 27) ....for x,ip in enumerate(ip_list,1): ........net_connect = Netmiko(ip=ip, device_type='cisco_ios', username='u',password='p',secret='p') ........hostname = net_connect.find_prompt()[:-1] ........print(f'{x} {hostname} \t {ip}')
Btw, why would prefer to read the file twist while not just store the IP addresses into an list?
04-21-2020 03:58 AM - edited 04-21-2020 04:23 AM
Thanks for helping. I'm new to Python btw. That was the easiest solution that I can think of.
Btw, I've modified the code and store the IP addresses into an list instead of reading it twice.
from netmiko import Netmiko with open('ip.txt') as f: ip_list = f.read().split() print('List of Hosts') print('-' * 13) for x,y in enumerate(ip_list, 1): print(f'{x} - {y}') print('\nNo Hostname \t IP Address') print('-' * 27) for x, ip in enumerate(ip_list, 1): net_connect = Netmiko(ip=ip, device_type='cisco_ios', username='u',password='p',secret='p') hostname = net_connect.find_prompt()[:-1] print(f'{x} {hostname} \t {ip}')
If there is any room for improvement, please let me know.
04-20-2020 05:23 PM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide