Juniper / py-junos-eznc

Python library for Junos automation
https://www.juniper.net/documentation/en_US/junos-pyez/information-products/pathway-pages/junos-pyez-developer-guide.html
Apache License 2.0
668 stars 342 forks source link

Get full RPC response in cli function #1315

Open dkaplan1 opened 1 month ago

dkaplan1 commented 1 month ago

junos-eznc's execute function only returns the first child element, described in https://github.com/Juniper/py-junos-eznc/blob/a64698b280fb717730dddf75a2d4d8cacb2e4775/lib/jnpr/junos/device.py#L896-L901 Normally this isn't an issue, since commands return the full payload in one <output> as part of a <rpc-reply> i.e.

<rpc-reply message-id="xxx"><output>
Slot 0   Online       xxx
  PIC 0  Online       xxx
  PIC 1  Online       xxx
Slot 1   Online       xxx
  PIC 0  Online       xxx
  PIC 1  Online       xxx
</output></rpc-reply>

However, for some commands, like show system core-dumps, the <rpc-reply> contains multiple <output>s.

<rpc-reply message-id="urn:uuid:xxx"><output>
/var/crash/*core*: No such file or directory
</output><output>
/var/tmp/*core*: No such file or directory
</output><output>
/var/tmp/pics/*core*: No such file or directory
</output><output>
/var/crash/kernel.*: No such file or directory
</output><output>
/var/jails/rest-api/tmp/*core*: No such file or directory
</output><output>
/tftpboot/corefiles/*core*: No such file or directory
</output></rpc-reply>

In these cases, only selecting the first element means that we only select

<output>
/var/crash/*core*: No such file or directory
</output>

and cli returns just

/var/crash/*core*: No such file or directory

instead of the full output.

To fix this I'm proposing to get the parent of output message (if one exists) so that the full output of the command is returned.

chidanandpujar commented 1 month ago

Hi @dkaplan1 Thanks for the pull request. Please find the review comments.

we are seeing one warning for following line of code. if rsp.tag in "output" and rsp.getparent():

can be changed to if rsp.tag in "output" and rsp.getparent() is not None:

Please check if it can be fixed, otherwise the cli response has the full response.

/root/pyez_release_272/venv/lib/python3.12/site-packages/junos_eznc-2.7.1+9.ga64698b2-py3.12.egg/jnpr/junos/device.py:738: FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead.
  if rsp.tag in "output" and rsp.getparent():

/var/crash/*core*: No such file or directory

/var/tmp/*core*: No such file or directory

/var/tmp/pics/*core*: No such file or directory

/var/crash/kernel.*: No such file or directory

/var/jails/rest-api/tmp/*core*: No such file or directory

/tftpboot/corefiles/*core*: No such file or directory

 python cli_test.py 

                     Temp  CPU Utilization (%)   CPU Utilization (%)  Memory    Utilization (%)
Slot State            (C)  Total  Interrupt      1min   5min   15min  DRAM (MB) Heap     Buffer
  0  Online           Testing   4         0        3      3      3    511        34          0
  1  Empty           
  2  Empty           
  3  Empty           
  4  Empty           
  5  Empty           
  6  Empty           
  7  Empty           
  8  Empty           
  9  Empty           
 10  Empty           
 11  Empty           

python cli_test.py 
/root/pyez_release_272/venv/lib/python3.12/site-packages/paramiko/pkey.py:100: CryptographyDeprecationWarning: TripleDES has been moved to cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and will be removed from this module in 48.0.0.
  "cipher": algorithms.TripleDES,
/root/pyez_release_272/venv/lib/python3.12/site-packages/paramiko/transport.py:271: CryptographyDeprecationWarning: TripleDES has been moved to cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and will be removed from this module in 48.0.0.
  "class": algorithms.TripleDES,

/var/crash/*core*: No such file or directory

/var/tmp/*core*: No such file or directory

/var/tmp/pics/*core*: No such file or directory

/var/crash/kernel.*: No such file or directory

/var/jails/rest-api/tmp/*core*: No such file or directory

/tftpboot/corefiles/*core*: No such file or directory

Thanks Chidanand

dkaplan1 commented 1 month ago

Thanks for the review @chidanandpujar, updated.

chidanandpujar commented 1 month ago

Hi @dkaplan1 Thanks for taking care of review comments. Fix looks to be working fine .

 python cli_test.py 
/root/pyez_release_272/venv/lib/python3.12/site-packages/paramiko/pkey.py:100: CryptographyDeprecationWarning: TripleDES has been moved to cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and will be removed from this module in 48.0.0.
  "cipher": algorithms.TripleDES,
/root/pyez_release_272/venv/lib/python3.12/site-packages/paramiko/transport.py:271: CryptographyDeprecationWarning: TripleDES has been moved to cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and will be removed from this module in 48.0.0.
  "class": algorithms.TripleDES,

/var/crash/*core*: No such file or directory

/var/tmp/*core*: No such file or directory

/var/tmp/pics/*core*: No such file or directory

/var/crash/kernel.*: No such file or directory

/var/jails/rest-api/tmp/*core*: No such file or directory

/tftpboot/corefiles/*core*: No such file or directory

                     Temp  CPU Utilization (%)   CPU Utilization (%)  Memory    Utilization (%)
Slot State            (C)  Total  Interrupt      1min   5min   15min  DRAM (MB) Heap     Buffer
  0  Online           Testing   3         0        3      3      3    511        34          0
  1  Empty           
  2  Empty           
  3  Empty           
  4  Empty           
  5  Empty           
  6  Empty           
  7  Empty           
  8  Empty           
  9  Empty           
 10  Empty           
 11  Empty           

Thanks Chidanand