Ha! that is perfect thanks again! I will take your advice on using json/xml and finish this off
Original Message:
Sent: 02-05-2025 08:23
From: asharp
Subject: using pyez
From the CLI you can find out what the RPC is by typing the command + " | display xml rpc".
e.g.
show security nat resource-usage source-pool gos_src_pool_198_169_34_12 | display xml rpc
Which returns the result:
<rpc> <retrieve-source-nat-pool-resource-usage> <pool-name>gos_src_pool_198_169_34_12</pool-name> </retrieve-source-nat-pool-resource-usage> </rpc>
Based on that response the RPC to be used with PyEz would then be:
rpc_text = dev.rpc.retrieve_source_nat_pool_resource_usage({'format':'text'}, pool_name='gos_src_pool_198_169_34_12')
Regards
Original Message:
Sent: 02-05-2025 07:50
From: ROB MERRITT
Subject: using pyez
sure Ill post the full script but is there a command to do this to show just a pool?
cmd_to_execute = "show security nat resource-usage source-pool " + pool
nodebkup@regn06-cn-gosfw-1> show security nat resource-usage source-pool gos_src_pool_198_169_34_12
node0:
--------------------------------------------------------------------------
Pool name: gos_src_pool_198_169_34_12
Total address: 1
Port-overloading-factor: 1
Total ports: 64512 Used: 16284 Avail: 48228
Current usage: 25% Peak usage: 71% at 2025-01-07 12:00:54 CST
Address Factor-index Port-range Used Avail Total Usage
198.169.34.12
0 Single Ports 16284 46180 62464 26%
- Alg Ports 0 2048 2048 0%
node1:
--------------------------------------------------------------------------
Pool name: gos_src_pool_198_169_34_12
Total address: 1
Port-overloading-factor: 1
Total ports: 64512 Used: 15978 Avail: 48534
Current usage: 24% Peak usage: 0% at 1969-12-31 18:00:00 CST
Address Factor-index Port-range Used Avail Total Usage
198.169.34.12
0 Single Ports 15978 46486 62464 25%
- Alg Ports 0 2048 2048 0%
{primary:node0}
nodebkup@regn06-cn-gosfw-1>
------------------------------
ROB MERRITT
Original Message:
Sent: 02-05-2025 07:36
From: asharp
Subject: using pyez
If that is the case, the you don't want to be collecting this data as text, you want to process it as XML or JSON and then iterate through that data to perform whichever tasks you need to perform.
Better to explain what you actually want a script to do in full, and then see if someone can assist you in writing it.
Regards
Original Message:
Sent: 02-05-2025 07:24
From: ROB MERRITT
Subject: using pyez
hey that is great!
here is another question however I am developing this to then loop through that text and execute
cmd_to_execute = "show security nat resource-usage source-pool " + pool
------------------------------
ROB MERRITT
Original Message:
Sent: 02-05-2025 06:54
From: asharp
Subject: using pyez
It looks like you have posted the same question in multiple communities.
As per my suggestion in the other thread.
The following code will perform the same task but leveraging an RPC and returning the output.
from jnpr.junos import Devicefrom lxml import etreedev = Device(host='10.1.1.1', user='<USERNAME>', password='<PASSWORD>')dev.open()rpc_text = dev.rpc.retrieve_source_nat_pool_resource_usage({'format':'text'}, all=True)print(etree.tostring(rpc_text, encoding='unicode'))
The above will return the output wrapped in <output>....</output> tags, which is how the data is returned via an RPC when using text format. If you don't want to have the <output> tags displayed, you can change the print statement to the following instead which will just output the contents of the output tag.
print(rpc_text.xpath('//output')[0].text)
Regards
Original Message:
Sent: 02-04-2025 17:07
From: ROB MERRITT
Subject: using pyez
Trying to susbstitute PyEz instead of Paramiko
I have
from jnpr.junos import Device
from pprint import pprint
from jnpr.junos.utils.start_shell import StartShell
import sys, io, os
dev = Device(host='10.1.1.1', user='<USERNAME>', password='<PASSWORD>')
dev.open()
ss = StartShell(dev)
ss.open()
version2 = ss.run('cli -c "show security nat resource-usage source-pool all"')
print (version2)
and the out put is truncated and has \r\n instead of interpreting them
[serveradmin@RHEL8-20230921 junos-pyezP311]$ ./test1.py
(False, 'cli -c "show security nat resource-usage source-pool all"\r\r\nnode0:\r\n--------------------------------------------------------------------------\r\n\r\nPAT pools(including address-shared pool) port utilization:\r\n Pool : gos_src_pool_198_169_34_113\r\n Address : 1 Used : 6663 Avail : 57849 Total : 64512 Usage : 10%\r\n')
False
how do I get the full output and the \r and \n are honoured?
------------------------------
ROB MERRITT
------------------------------