Automation

last person joined: yesterday 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Get data from show command

    Posted 09-17-2013 02:27

    Hi all,

    I am on a project which want to get data from "show subscribers summary port" command. I wrote a script to do that but it getting not enough data.

    Here are the script: 

    version 1.0;
    ns Junos = "http://xml.juniper.net/junos/*/junos";
    ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
    ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
    
    match / {
    	<op-script-results> {
    	var $cmd = <command> "show subscribers summary port";
        var $results = jcs:invoke($cmd);
    	
    	for-each ( $results/counters/port-name ) {
    		<output> "The number subscribers of interface "_ . _ " is " _ ../port-count;
    		}
    	}
    }

     and this is the output when run this script:

     

    datnt@juniper> op sub  
    The number subscribers of interface xe-0/2/1 is 5916
    The number subscribers of interface xe-11/0/1 is 5916
    The number subscribers of interface xe-11/1/1 is 5916

     The output of script dont match the output of command. I am very pleasure if anyone can help me fix it .

    Here are the output of the command:

    datnt@juniper> show subscribers summary port    
    Interface           Count 
    xe-0/2/1            5916               
    xe-11/0/1           10121              
    xe-11/1/1           2544               

     

    datnt@juniper> show subscribers summary port | display xml 
    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/11.4X27/junos">
    <subscribers-summary-information xmlns="http://xml.juniper.net/junos/11.4X27/junos-subscribers">
     <counters junos:style="port-summary">
         <port-name>xe-0/2/1</port-name>
         <port-count>5916</port-count>
         <port-name>xe-11/0/1</port-name>
         <port-count>10122</port-count>
         <port-name>xe-11/1/1</port-name>
         <port-count>2544</port-count>
     </counters>
    <counters junos:style="port-summary-total">
    <port-total>18578</port-total>
    </counters>
    </subscribers-summary-information>
    <cli>
    <banner>{master}</banner>
    </cli>
    </rpc-reply>

     

     

     



  • 2.  RE: Get data from show command
    Best Answer

    Posted 09-17-2013 09:26

    It's unfortunate that the XML results are structured in that way. Ideally you'd want each matching port-name and port-count to be segmented in a separate hierarchy. But, with the hierarchy shown in your results, you'll need to make use of the following-sibling axis rather than the parent/child axes.

     

    <output> "The number subscribers of interface "_ . _ " is " _ ./following-sibling::port-count[1];

     

    Please give that a try.

     

    Thanks



  • 3.  RE: Get data from show command

    Posted 09-17-2013 10:57

    Thank ccall for your help. It's work.
    I have a another question want to ask. How can i get data from $results/counters/ and put it on a variable to use for other purposes.
    In this example, i want the result of variable x = "xe-0/2/1 5916 xe-11/0/1 10121 xe-11/1/1 2544"



  • 4.  RE: Get data from show command

    Posted 09-17-2013 11:05

    You can enclose the for-each loop within a variable and build the output (note that this will catch all output, including XML elements like <output>):

     

    var $variable = {

        for-each( ...whatever your path is... ) {

            expr ...whatever you want to write...;

        }

    }



  • 5.  RE: Get data from show command

    Posted 09-17-2013 11:37

    Hi ccall,

    Can you help me on this case?



  • 6.  RE: Get data from show command

    Posted 09-17-2013 11:44

    Try this:

    var $text = {

        for-each ( $results/counters/port-name ) {

            expr . _ " " _ ./following-sibling::port-count _ "\n";

        }

    }



  • 7.  RE: Get data from show command

    Posted 09-17-2013 12:48

    It's work great. Thank you very much!