<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python: Numbering doesn't work as expected in Controllers</title>
    <link>https://community.cisco.com/t5/controllers/python-numbering-doesn-t-work-as-expected/m-p/4069891#M1996</link>
    <description>&lt;PRE&gt;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 &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;x,&lt;/STRONG&gt;&lt;/FONT&gt;ip in &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;enumerate&lt;/FONT&gt;(&lt;/STRONG&gt;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}')&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Btw, why would prefer to read the file twist while not just store the IP addresses into an list?&lt;/P&gt;</description>
    <pubDate>Mon, 20 Apr 2020 16:15:10 GMT</pubDate>
    <dc:creator>ngkin2010</dc:creator>
    <dc:date>2020-04-20T16:15:10Z</dc:date>
    <item>
      <title>Python: Numbering doesn't work as expected</title>
      <link>https://community.cisco.com/t5/controllers/python-numbering-doesn-t-work-as-expected/m-p/4069874#M1995</link>
      <description>&lt;P&gt;This code almost perfect, however the 2nd numbering is not working as expected.&lt;/P&gt;&lt;PRE&gt;user@linux:~$ cat ip.txt
10.1.1.1
10.2.2.2
10.3.3.3
user@linux:~$&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;script.py&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;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}')&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Output&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;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:~$&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Desired Output&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;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:~$&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;UPDATE&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;As advised by ngkin2010, the IP addresses have been stored into an list instead of being read twice.&lt;/P&gt;&lt;P&gt;Here is the updated code&lt;/P&gt;&lt;PRE&gt;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}')&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Apr 2020 11:21:39 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-numbering-doesn-t-work-as-expected/m-p/4069874#M1995</guid>
      <dc:creator>write_erase</dc:creator>
      <dc:date>2020-04-21T11:21:39Z</dc:date>
    </item>
    <item>
      <title>Re: Python: Numbering doesn't work as expected</title>
      <link>https://community.cisco.com/t5/controllers/python-numbering-doesn-t-work-as-expected/m-p/4069891#M1996</link>
      <description>&lt;PRE&gt;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 &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;x,&lt;/STRONG&gt;&lt;/FONT&gt;ip in &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;enumerate&lt;/FONT&gt;(&lt;/STRONG&gt;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}')&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Btw, why would prefer to read the file twist while not just store the IP addresses into an list?&lt;/P&gt;</description>
      <pubDate>Mon, 20 Apr 2020 16:15:10 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-numbering-doesn-t-work-as-expected/m-p/4069891#M1996</guid>
      <dc:creator>ngkin2010</dc:creator>
      <dc:date>2020-04-20T16:15:10Z</dc:date>
    </item>
    <item>
      <title>Re: Python: Numbering doesn't work as expected</title>
      <link>https://community.cisco.com/t5/controllers/python-numbering-doesn-t-work-as-expected/m-p/4070167#M1997</link>
      <description>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.</description>
      <pubDate>Tue, 21 Apr 2020 00:23:42 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-numbering-doesn-t-work-as-expected/m-p/4070167#M1997</guid>
      <dc:creator>Manoj Papisetty</dc:creator>
      <dc:date>2020-04-21T00:23:42Z</dc:date>
    </item>
    <item>
      <title>Re: Python: Numbering doesn't work as expected</title>
      <link>https://community.cisco.com/t5/controllers/python-numbering-doesn-t-work-as-expected/m-p/4070496#M1998</link>
      <description>&lt;P&gt;Thanks for helping. I'm new to Python btw. That was the easiest solution that I can think of.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Btw, I've modified the code and store the IP addresses into an list instead of reading it twice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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}')&lt;/PRE&gt;&lt;P&gt;If there is any room for improvement, please let me know.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 11:23:57 GMT</pubDate>
      <guid>https://community.cisco.com/t5/controllers/python-numbering-doesn-t-work-as-expected/m-p/4070496#M1998</guid>
      <dc:creator>write_erase</dc:creator>
      <dc:date>2020-04-21T11:23:57Z</dc:date>
    </item>
  </channel>
</rss>

