Last active
January 28, 2018 22:29
-
-
Save nathancatania/66939372a5f5355dbae4ee5695ae29a1 to your computer and use it in GitHub Desktop.
Multiprocessing demo for PyEZ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ncatania-mbp:Python ncatania$ python3 pyez-multiprocessing-demo.py | |
Number of processes (devices that will be processed simultaneously): 3 | |
Connecting to 172.16.141.140... <--- "vsrx1" | |
Connecting to 172.16.141.141... <--- "vsrx2" | |
Connecting to 172.16.141.142... <--- "vsrx3" | |
Connected to 172.16.141.140 OK! | |
Connected to 172.16.141.141 OK! | |
Connected to 172.16.141.142 OK! | |
*** Got Device Facts! *** | |
Hostname: vsrx1, Serial#: 6EXXXXXXXXXX | |
********** | |
Connecting to 172.16.141.143... <--- "vsrx4" | |
*** Got Device Facts! *** | |
Hostname: vsrx3, Serial#: 6EXXXXXXXXXX | |
********** | |
*** Got Device Facts! *** | |
Hostname: vsrx2, Serial#: 6EXXXXXXXXXX | |
********** | |
Connecting to 172.16.141.144... <--- "vsrx5" | |
Connecting to 172.16.141.145... <--- "vsrx6" | |
Connected to 172.16.141.143 OK! | |
Connected to 172.16.141.144 OK! | |
Connected to 172.16.141.145 OK! | |
*** Got Device Facts! *** | |
Hostname: vsrx4, Serial#: 6EXXXXXXXXXX | |
********** | |
*** Got Device Facts! *** | |
Hostname: vsrx5, Serial#: 6EXXXXXXXXXX | |
********** | |
*** Got Device Facts! *** | |
Hostname: vsrx6, Serial#: 6EXXXXXXXXXX | |
********** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Pool | |
from jnpr.junos import Device | |
from jnpr.junos.exception import ConnectError | |
def connect(host): | |
# CREDENTIALS - Change as required | |
username = "lab" | |
passwd = "lab123" | |
sshkey = "/path/to/sshkey.pem" | |
# Connect to the device - Remove passwd/sshkey parameters as required | |
dev = Device(host=host, user=username, passwd=passwd, ssh_private_key_file=sshkey) | |
try: | |
print ("Connecting to {}...".format(host)) | |
dev.open() | |
except ConnectError as err: | |
print ("Cannot connect to device: {0}".format(err)) | |
return None | |
except Exception as err: | |
print ("Cannot connect to device: {0}".format(err)) | |
return None | |
else: | |
print ("Connected to {} OK!".format(host)) | |
### | |
# Do some stuff with the device | |
### | |
# Eg: Prints hostname and serial number from device facts | |
print ("*** Got Device Facts! ***\nHostname: {}, Serial#: {}\n**********\n".format(dev.facts['hostname'], dev.facts['serialnumber'])) | |
### | |
# Close connection to the device | |
dev.close() | |
if __name__ == '__main__': | |
# List of hosts to connect to | |
hosts = ["172.16.141.140", "172.16.141.141", "172.16.141.142", "172.16.141.143", "172.16.141.144", "172.16.141.145"] | |
processes = 3 # Number of processes to create for multiprocessing. BE CAREFUL WITH THIS! | |
print("\nNumber of processes (devices that will be processed simultaneously): {}\n".format(processes)) | |
# pool.map will call the connect() function and pass a single element from hosts as the parameter. | |
# This is repeated for the length of hosts. | |
# If process limit is reached (eg: 3), then pool.map will wait until one of the 3 processes completes before calling connect() with the next hosts parameter. | |
pool = Pool(processes=processes) | |
pool.map(connect, hosts) | |
pool.close() | |
pool.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment