Nov-07-2023, 09:49 PM
(This post was last modified: Nov-08-2023, 06:25 AM by Yoriz.
Edit Reason: Added code tags
)
I know that there are cisco python libs to login and execute CLI and get output , is there such a ting for Juniper/JUNOS?
currently I use the paramiko lib like below but it is a PITA , might be better with vendor supported libs
currently I use the paramiko lib like below but it is a PITA , might be better with vendor supported libs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#!/usr/bin/python3 import paramiko server = "10.228.9.75" un = "username" pw = "password" poolArray = [ "pools" ] ssh = paramiko.client.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) cmd_to_execute = "show security nat resource-usage source-pool all" ssh.connect(server, username = un, password = pw) ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute) x = 1 for line in ssh_stdout: try : lineArray = line.strip().split() if "node" in lineArray[ 0 ]: nodeIs = lineArray[ 0 ] if "%" in lineArray[ 5 ]: #print (nodeIs +" " + lineArray[0]) #poolArray.append(nodeIs +" " + lineArray[0]) poolArray.append(lineArray[ 0 ]) except Exception as e: pass x = x + 1 ssh.close() poolVals = "" #print(len(poolVals)) #print (len(poolArray)) for pool in poolArray: ssh = paramiko.client.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #cmd_to_execute = '"show security nat resource-usage source-pool ' + pool + "\"" cmd_to_execute = "show security nat resource-usage source-pool " + pool #print(cmd_to_execute) ssh.connect(server, username = un, password = pw) ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute) #print(ssh_stdout) x = 1 for line in ssh_stdout: lineArray = line.strip().split() if "%" in line and "-" not in line: if int (lineArray[ 6 ].replace( "%" ,"")) > 10 : print (pool + ": " + str (x) + " " + line.strip()) y = str (pool) + ": " + str (x) + " " + str (line) poolVals = poolVals + y x = x + 1 ssh.close() print ( len (poolVals)) if len (poolVals) > 1 : print ( "dump" ) print (poolVals) print ( "end" ) |