Junos Automation (Scripting)
Reply
Contributor
onedreamcloser
Posts: 16
Registered: ‎05-13-2011
0
Accepted Solution

JUNOSe | capture "item" from command output

Hello All,

 

Could you please help me with the following problem :

Given the script with the cli macros that appears below :

<# _hello #>

<# env.startCommandResults #>
show ip interface fastEthernet 6/0/0 | include Out Forwarded
<# env.stopCommandResults #>
<# line := env.getResults #>
<# B_out := env.getRegexpMatch(line, "[0-9]+", 2) #>

<# setoutput console #>
<# line $ "\n" #>
<# B_out $ "\n" #>
<# endsetoutput #>

<# endtmpl #>

 What I would like to ask you is :

0. is it possible to supress the output from what is inside the "CommandResults" control statements (in our case: show ip interface fastEthernet 6/0/0 | include Out Forwarded) ?

the output is:

  Out Forwarded Packets 0, Bytes 0

 

1. why the "line" variable even after calling the "env.getResults" method does not hold the output of the aforementioned command in question (0.) ?

 

Thank You.

Juniper Employee
pkasten
Posts: 6
Registered: ‎02-16-2011

Re: JUNOSe | capture "item" from command output

I am still looking to see if I can help you suppress the output of the "show ip" command.

However, I do have a solution for the problem that the "line" and "B_out" variables end up not having the text you expect. Simply move the assignment of those variables to within the setoutput scope; I modified your script, below, to have the lines appear AFTER the setoutput directive. This should give you the results you expect.


<# _hello #> <# env.startCommandResults #> show ip interface fastEthernet 6/0/0 | include Out Forwarded <# env.stopCommandResults #> <# setoutput console #>
<# line := env.getResults #> <# B_out := env.getRegexpMatch(line, "[0-9]+", 2) #>
<# line $ "\n" #> <# B_out $ "\n" #> <# endsetoutput #> <# endtmpl #>
Contributor
onedreamcloser
Posts: 16
Registered: ‎05-13-2011
0

Re: JUNOSe | capture "item" from command output

[ Edited ]

thank you so much pkasten, it works :smileyhappy: i am a newbie but you are a pro :smileyhappy:

Contributor
onedreamcloser
Posts: 16
Registered: ‎05-13-2011
0

Re: JUNOSe | capture "item" from command output

May I ask something else, how many bits are reserved for integer data types in the JunosE scripting language ?

It seems that there is a rollover in the following case, if I am not mistaken :

Macro '_show_interface_output_rate' in file '_show_interface_output_rate.mac'
starting execution (Id: 349)
  Out Forwarded Packets 2887681, Bytes 340008454

-1574899664
Macro '_show_interface_output_rate' in file '_show_interface_output_rate.mac'

 The actual code is :

<# _show_interface_output_rate #>

<# if env.argc=1 #>
<# env.startCommandResults #>
show ip interface <# env.argv(1); #> | include Out Forwarded
<# env.stopCommandResults #>

<# setoutput console #>
<# line := env.getResults #>
<# output_rate := env.getRegexpMatch(line, "[0-9]+", 2) #>
<# output_rate := env.atoi(output_rate) *8 #>

<# output_rate $ "\n" #>
<# endsetoutput #>
<# endif #>

<# endtmpl #>

 Thank you.

 

Juniper Employee
pkasten
Posts: 6
Registered: ‎02-16-2011

Re: JUNOSe | capture "item" from command output

[ Edited ]

Hi OneDreamCloser.  Integers in E-Series are 32 bits, and it appears that the macro language, at least in your example, is treating them as signed.

 

Here is a solution you might find handy.  I suggest that you save this as a utility macro -- you can invoke macros from other macros, even if they are in different .mac files. Please let me know if you have questions about that.

 

The following contains two macros, although one is used only for testing the other.

 

The real macro of interest is xmToString, for "multiply by m and return the string".  (We cannot return the integer value, if the value is larger than 32 bits will hold, but we can return a string that looks like the integer :smileyhappy: ).

 

I have attached the .mac file (but you'll have to remove the ".txt" extension), but its contents are listed below, too.

 

Uncomment the setoutput line below, if you want to watch the inner-workings.

 

Use this like tmpl.xmToString(3201234, 8).  Note that you really should only use positive integers for both parameters; any other use will not work as intended.

 

So, in your specific case, change this line:

<# output_rate := env.atoi(output_rate) *8 #>

to

<# output_rate := tmpl.xmToString(output_rate, 8) #>

 

Just remember that you should not use output_rate as a number after the above, because you'll be back where you started.  In your case, though, you're just looking to display it as a string, so you'll be fine.

 

<# xmToString(i, m) #>

<# n := i #>

<# r := "" >

<# #>

<# // This macro will take an integer i and return a  #>

<# // string that looks like i * m.  This             #>

<# // might be useful if i * m is a number larger     #>

<# // than 32 bits can hold.                          #>

<# #>

<# // The "trick" is to do the multiplication "by hand" #>

<# // one digit at a time, managing the carry, etc.     #>

<# #>

<# while(n > 0 || carry > 0) #>

<#    n_lsd := n%10          #>

<#    d := n_lsd * m + carry #>

<#    if (d > 9)             #>

<#         unit := d%10; carry := d - unit; carry := carry/10; d := unit #>

<#    else                   #>

<#        carry := 0         #>

<#    endif                  #>

<#   r := d $ r              #>

<#   // setoutput console; "(n, n_lsd, d, carry, r): " $ n $ " " $ n_lsd $ " " $ d $ " " $ carry $ " " $ r $ "\n"; endsetoutput #>

<#   n := n - n_lsd; n := n/10 #>

<# endwhile #>

<# #>

<# return(r) #>

<# #>

<# endtmpl #>

 

<# _xmToString(i, m) #>

<# // Tester macro #>

<# #>

<# setoutput console #>

<# r := tmpl.xmToString(i, m); r $ "\n" #>

<# endsetoutput #>

<# #>

<# endtmpl #>

 

 

Contributor
onedreamcloser
Posts: 16
Registered: ‎05-13-2011
0

Re: JUNOSe | capture "item" from command output

i am so thankful pkasten, thanx ! :smileyhappy:

Juniper Employee
pkasten
Posts: 6
Registered: ‎02-16-2011
0

Re: JUNOSe | capture "item" from command output

You're welcome, onedreamcloser. Let me know how you make out with this solution.

I'll continue to watch the forum for additional questions you might have. Feel free to create a new discussion for each new question.

Good luck!
Copyright© 1999-2013 Juniper Networks, Inc. All rights reserved.