Python Forum
Juniper Python libs? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: Juniper Python libs? (/thread-41082.html)



Juniper Python libs? - merrittr - Nov-07-2023

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


#!/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")