As the other comment suggests, using an RPC would be best practice rather than opening a shell to execute a cli command.
The following code will perform the same task but leveraging an RPC and returning the output.
from jnpr.junos import Device
from lxml import etree
dev = 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?
the actual output of the command should look like:
nodebkup@regn06-cn-gosfw-1> show security nat resource-usage source-pool all
node0:
--------------------------------------------------------------------------
PAT pools(including address-shared pool) port utilization:
Pool : gos_src_pool_198_169_34_113
Address : 1 Used : 6849 Avail : 57663 Total : 64512 Usage : 10%
Pool : gos_src_pool_198_169_34_114
Address : 1 Used : 2555 Avail : 61957 Total : 64512 Usage : 3%
Pool : gos_src_pool_207_195_114_101
Address : 1 Used : 13822 Avail : 50690 Total : 64512 Usage : 21%
Pool : gos_src_pool_207_195_114_103
Address : 1 Used : 5961 Avail : 58551 Total : 64512 Usage : 9%
Pool : gos_src_pool_207_195_114_109
Address : 1 Used : 6696 Avail : 57816 Total : 64512 Usage : 10%
Pool : gos_src_pool_207_195_114_111
Address : 1 Used : 7580 Avail : 56932 Total : 64512 Usage : 11%
Pool : gos_src_pool_207_195_114_114
Address : 1 Used : 16522 Avail : 47990 Total : 64512 Usage : 25%
Pool : gos_src_pool_207_195_114_115
Address : 1 Used : 7418 Avail : 57094 Total : 64512 Usage : 11%
Pool : gos_src_pool_207_195_114_116
Address : 1 Used : 12529 Avail : 51983 Total : 64512 Usage : 19%
Pool : gos_src_pool_207_195_114_13
Address : 1 Used : 0 Avail : 64512 Total : 64512 Usage : 0%
Pool : gos_src_pool_207_195_114_48
Address : 1 Used : 95333 Avail : 1969051 Total : 2064384 Usage : 4%
Pool : gos_src_pool_207_195_114_95
Address : 1 Used : 1044 Avail : 63468 Total : 64512 Usage : 1%
Pool : gos_src_pool_216_174_136_98
Address : 1 Used : 0 Avail : 64512 Total : 64512 Usage : 0%
Pool : gos_src_pool_198_169_112_252
Address : 1 Used : 0 Avail : 64512 Total : 64512 Usage : 0%
Pool : gos_src_pool_198_169_112_253
Address : 1 Used : 1066 Avail : 63446 Total : 64512 Usage : 1%
Pool : gos_src_pool_198_169_112_254
Address : 1 Used : 1250 Avail : 514846 Total : 516096 Usage : 0%
Pool : gos_src_pool_198_169_34_115
Address : 1 Used : 0 Avail : 64512 Total : 64512 Usage : 0%
Pool : gos_src_pool_198_169_34_116
Address : 1 Used : 0 Avail : 64512 Total : 64512 Usage : 0%
Pool : gos_src_pool_216_174_134_104
Address : 1 Used : 0 Avail : 64512 Total : 64512 Usage : 0%
Pool : gos_src_pool_216_174_134_105
Address : 1 Used : 0 Avail : 64512 Total : 64512 Usage : 0%
Pool : gos_src_pool_216_174_134_106
Address : 1 Used : 0 Avail : 64512 Total : 64512 Usage : 0%
Pool : gos_src_pool_216_174_134_110
Address : 1 Used : 0 Avail : 64512 Total : 64512 Usage : 0%
Pool : gos_src_pool_216_174_134_115
Address : 1 Used : 0 Avail : 64512 Total : 64512 Usage : 0%
Pool : gos_src_pool_216_174_134_152
Address : 1 Used : 3012 Avail : 61500 Total : 64512 Usage : 4%
Pool : gos_src_pool_216_174_134_223
Ad
------------------------------
ROB MERRITT
------------------------------