Running batch commands on remote junos devices
I am sure from time to time you need to run an operational command on multiple junos devices e.g deleting a specific file from all devices. It is up to you of course what sort of commands you can run. For the following script to work you should also install python paramiko module. You may possibly install this module on a linux system via the command “pip install paramiko” . I have also attached the script here as some characters may not be displayed properly in this post. Now let me explain how to use this script.
Once you save the script with the name junos_cmd.py, there are two types of execution
1) Single command on a single system
#junos_cmd.py --ip 192.168.1.1 --cmd 'cli -c "file delete /tmp/test.txt"'
This command will run the command “file delete /tmp/test.txt” on the device having ip 192.168.1.1 (root user should be used for this command to work)
2) Single command on multiple systems
#junos_cmd.py --file /tmp/ipaddr.txt --cmd 'cli -c "file delete /tmp/test.txt"'
This command will fetch all ip addresses written in the file /tmp/ipaddr.txt (each ip should be in a new line) and run the operational command one by one.
This is a very basic script but it may shed you some light to develop your own complex scripts. I also use it to fetch command outputs and then interpret the outputs via python again. Let me know if you need any help in using it.
#!/usr/bin/python import paramiko,sys import socket #Authentication credentials username='root' password='lab124' #Check command line options try: opt1=sys.argv[1] #value1: ip address or file path value1=sys.argv[2] opt2=sys.argv[3] #value2: command value2=sys.argv[4] except IndexError: print """Usage: junos_cmd.py {--ip|--file} {IP_ADDR|FILE_PATH} {--cmd 'COMMAND'} example: junos_cmd.py --ip 192.168.1.1 --cmd 'cli -c "show version"' junos_cmd.py --file /tmp/ip_file.txt 'cli -c "show interfaces"' """ sys.exit() #Check TCP 22 connection def Check_SSH(IP): s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.settimeout(3) try: s.connect((IP,22)) s.shutdown(2) return True except: print "%s SSH connection failed" % (IP) return False #Paramiko connection ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy()) def run_cmd(device_ip,cmd,username,password): if Check_SSH(device_ip): try: print "Command: %s is running on %s" % (cmd,device_ip) ssh.connect(device_ip,username=username,password=password) stdin,stdout,stderr = ssh.exec_command(cmd) if len(stderr.readlines()) > 0: print stderr.readlines() #If verbose print is required #print stdout.readlines() except paramiko.AuthenticationException: print "%s Authentication failed" % (device_ip) def run_batch_cmd(filepath,cmd): fd = open (filepath,'r') for ip_addr in fd.readlines(): ip_addr=ip_addr.rstrip() run_cmd(ip_addr,cmd,username,password) if opt1=="--ip": run_cmd(value1,value2,username,password) elif opt1=="--file": run_batch_cmd(value1,value2)
Hi,
I am new to the script , looking for a python script to change the user password for juniper devices(routers, switches and firewall) , Could anyone hlep me on this .
Thanks and Regards,
Midhun P.K