Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Juniper Python libs?
#1
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")
Yoriz write Nov-08-2023, 06:25 AM:
Please post all code, output and errors (in its entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to ensure the code is presented as expected before hitting the "Post Reply/Thread" button.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020