0

e.g.

r2 = '192.168.122.72'
r3 = '192.168.122.73'
hostnames = [r2,r3]
#commands = ['term len 0','show run']
for r in hostnames:
    print (r)
    print (hostnames)
    print str(r)

Output of this is....

192.168.122.72
['192.168.122.72', '192.168.122.73']
192.168.122.72
192.168.122.73
['192.168.122.72', '192.168.122.73']
192.168.122.73

I actually want to see an output that will say the items literal text, not its value, so i actually want to see this.......

r2
r3

Ultimately, want to create a file that is the name of the router, not its IP.

Thank you in advance!

kaj0103
  • 1
  • 1
  • 1
    You can't. Variable names refer to values unidirectionally. Use a dictionary mapping IPs to names (or vice versa) and then iterate over the dicts `items` attribute. – timgeb Nov 22 '18 at 16:59
  • It is the programmers job to properly organize your code if you want to associate a string to some other value. Variables are not strings, they are source code. You should use a `dict`. – juanpa.arrivillaga Nov 22 '18 at 17:09

1 Answers1

2

This is not possible. You have to use a dictionary for that.

Example:

r2 = '192.168.122.72'
r3 = '192.168.122.73'
hostnames = {'r2':r2,'r3':r3}
#commands = ['term len 0','show run']
for r in hostnames:
    print (r)
    print (hostnames)
    print str(r)
FMarazzi
  • 583
  • 1
  • 5
  • 14