cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1645
Views
10
Helpful
3
Replies

Python: Numbering doesn't work as expected

write_erase
Level 1
Level 1

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}')
1 Accepted Solution

Accepted Solutions

ngkin2010
Level 7
Level 7
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?

View solution in original post

3 Replies 3

ngkin2010
Level 7
Level 7
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?

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.

Manoj Papisetty
Cisco Employee
Cisco Employee
as far your code is concerned, 'x' value is already 3 by the end of first "for" loop. And the second loop has no 'x' for enumeration and so the value stays at 3.