cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2124
Views
5
Helpful
2
Replies

How to issue commands with variables in pyATS

oslopez
Level 1
Level 1

Hi,

I'm trying to automate the configuration of remote Cisco devices using pyATS. Going thru the online documentation I found the following script excerpt:

# establish basic connectivity
ios_1.connect()

# issue commands
print(ios_1.execute('show version'))
ios_1.configure('''
    interface GigabitEthernet0/0
        ip address 10.10.10.1 255.255.255.0
''')

This is pretty much what I was looking for. I tested and worked just fine. However, I'd like to use variables instead of fixed values, like : ip address <IP_Address> <Mask> or something like that.

By this way I could create a script that takes arguments and the argument could be the ip addres and the mask. Given that those are IOS commands I couldn't figure out how to make it work. I'd appreciate any clue on this or, maybe you can suggest a different approach.

 

Thanks!

2 Replies 2

@oslopez you could import ipaddress http://docs.python.org/3/library/ipaddress and use the input function.

 

Hope this helps.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

pmerlitt
Cisco Employee
Cisco Employee

This is what templating is for. 

  1. Create the template, for example,  ip address {{ ip_address }} {{ mask }}
  2. Combined with the data, for example, data1 = { 'ip_address': '10.1.2.3', 'mask': '255.255.255.0' }
  3. You render your final configuration: ip address 10.1.2.3 255.255.255.0.   

If you have simple needs, maybe this is probably overkill, but Jinja2 templates would be the way to go for more complex/complete templating.   https://svn.python.org/projects/external/Jinja-2.1.1/docs/_build/html/index.html