Automation

 View Only
last person joined: 9 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  using pyez

    Posted 5 days ago
    Edited by ROB MERRITT 5 days ago

    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
    ------------------------------



  • 2.  RE: using pyez

    Posted 4 days ago
    Edited by STUART RIDSDALE 4 days ago

    It returns a tuple and you are just printing the tuple itself rather than the item in the tuple that contains the CLI output.

    So instead print the item in the tuple you want with:

    print(version2[1])

    You also might need to set the screen length to 0 before you run the command to disable pagination. So something like this should work:

    version2 = ss.run('cli -c "set cli screen-length 0;show security nat resource-usage source-pool all"')



    As an aside, I would recommend at this point to look at using the equivalent RPC command to return the output as structured data rather than getting the raw CLI output.

    Starting a shell and running CLI commands via PyEz is useful for when there are no RPC commands for what you are trying to do, otherwise it's mostly preferable to get the structured data back. 



    ------------------------------
    STUART RIDSDALE
    ------------------------------



  • 3.  RE: using pyez
    Best Answer

     
    Posted 4 days ago
    Edited by ROB MERRITT 4 days ago

    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




  • 4.  RE: using pyez

    Posted 4 days ago

    Have you tried using pretty print?

    from pprint import pprint

    pprint(version2)

    This would just be to get a nicer output. \r\n are still being printed.



    ------------------------------
    IAN STOETTRUP
    ------------------------------



  • 5.  RE: using pyez

    Posted 4 days ago

    ey Yeah that cleaned it up for sure thanks ian



    ------------------------------
    ROB MERRITT
    ------------------------------