SRX

 View Only
last person joined: 3 days ago 

Ask questions and share experiences about the SRX Series, vSRX, and cSRX.
  • 1.  using pyez

    Posted 10 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?


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


  • 2.  RE: using pyez

     
    Posted 9 days ago

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




  • 3.  RE: using pyez

    Posted 9 days ago

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



  • 4.  RE: using pyez

     
    Posted 9 days ago

    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




  • 5.  RE: using pyez

    Posted 9 days ago

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



  • 6.  RE: using pyez
    Best Answer

     
    Posted 9 days ago

    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




  • 7.  RE: using pyez

    Posted 9 days ago

    Ha! that is perfect thanks again! I will take your advice on using json/xml and finish this off



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