Automation

Scripting How-To: Loop through devices using skeleton-code

By Elevate posted 08-06-2015 04:47

  

Loop Through Devices Using Skeleton-Code

 

You use "skeleton-code" to loop through a list of hostnames using the same username and password.
 
The following skeleton-code defines a list of hosts, devlist, and prompts the you for your username and password. The username is prompted "in the clear" and the password is not echoed to the screen.
 

 

01 import sys
02 from getpass import getpass
03 from jnpr.junos import Device
04 from jnpr.junos.op.xcvr import XcvrTable
05  
06 devlist = ['mx240.chi','mx480.chi','mx80.chi','srx3400.chi']
07 user = raw_input('username: ')
08 passwd = getpass('password: ')
09  
10 for host in devlist:
11   sys.stdout.write('connecting to %s ... ' % host)
12   sys.stdout.flush()
13   
14   dev = Device(host,user=user,password=passwd)
15   dev.open()
16   print('ok.')
17  
18   # --------------------------------------------------
19   # actual code processing the Device goes here ...
20   # --------------------------------------------------
21    
22   dev.close()

 

 

You can use the above skeleton-code for a number of automation tasks when you need to process a list of devices. The list can come from just about anywhere, including external files or remote server systems.

 


#How-To
#Python
#example
#junospyez
0 comments
0 views

Permalink