Filtering Operational Mode Command Output
You can redirect the output of operational mode commands into a file or into filters. When you display help about these commands, one of the options listed is
|, which is a pipe filter. For example:user@host>show configuration ?Possible completions:<[Enter]> Execute this command...| Pipe through a commanduser@host>show configuration | ?Possible completions:count Count occurrencesdisplay Show additional kinds of informationexcept Show only text that does not match a patternfind Search for first occurrence of patternhold Hold text without exiting the --More-- promptlast Display end of output onlymatch Show only text that matches a patternno-more Don't paginate outputrequest Make system-level requestsresolve Resolve IP addressessave Save output text to filetrim Trim specified number of columns from start of lineTable 7 describes the commands that can be used with the pipe ( | ) option in operational mode.
Table 7: Filtering Operational Mode Command Output
Ignores text matching the regular expression. If the regular expression contains spaces, operators, or wildcard characters, enclose it in quotation marks (" " ).
Displays output starting at the first occurrence of text matching the regular expression. If the regular expression contains spaces, operators, or wildcard characters, enclose it in quotation marks (" " ).
Displays text starting at the specified number of lines from the end of the output.
Displaying the Lines You Want to View from the End of the Output
Searches for text matching the regular expression. If the regular expression contains spaces, operators, or wildcard characters, enclose it in quotation marks.
Displays output all at once rather than one screen at a time.
Display command output on the terminal of a specific user logged onto your router, or on the terminals of all users logged onto your router.
Converts IP addresses into domain name server (DNS) names. By default, names are truncated to fit the original size. To prevent names from being truncated, use the full-names keyword.
NOTE: Additional options are available in configuration mode. For these options, see the JUNOS System Basics Configuration Guide.
For the output of a single command, you can redirect the output one or more times. For example:
user@host>command| matchregular-expression| exceptregular-expression| matchother-regular-expression| findregular-expression| holdThe following sections provide additional information for most of the pipe ( | ) filter options described in Table 7:
- Counting the Number of Lines in the Output
- Searching for a String in the Output
- Retaining the Output After the Last Screen
- Displaying the Lines You Want to View from the End of the Output
- Displaying All Output at Once
- Sending Command Output to Other Users
- Placing Command Output in a File
Counting the Number of Lines in the Output
To count the number of lines in the output, specify the
countcommand after the pipe. For example:user@host>show configuration | countCount: 269 linesuser@host>show route | countCount: 67 linesSearching for a String in the Output
You can filter the output to search for a text matching a regular expression. You can match a regular expression, match everything except a regular expression, or find the first occurrence of text matching a regular expression. All searches are not case-sensitive.
To ignore text that matches a regular expression, specify the
exceptcommand after the pipe:user@host>command| exceptregular-expressionTo find the first occurrence of a regular expression, specify the find command after the pipe:
user@host>command| findregular-expressionTo match a regular expression, specify the
matchcommand after the pipe:user@host>command| matchregular-expressionIf the
regular-expressioncontains any spaces, operators, or wildcard characters, enclose it in quotation marks.You use extended regular expressions to specify what text in the output to match. Command regular expressions implement the extended (modern) regular expressions as defined in POSIX 1003.2.
Table 8 lists common regular expression operators.
Table 8: Common Regular Expression Operators
For example, if a command produces the following output:
one twotwo twothree two onefourThe
match twocommand displays:one twotwo twothree two oneThe
except onecommand displays:two twofourList all the ATM interfaces in the configuration:
user@host>show configuration | match at-at-2/1/0 {at-2/1/1 {at-2/2/0 {at-5/2/0 {at-5/3/0 {Display a skeleton of your router configuration:
[edit]user@host# show | match {system {root-authentication {name-server {login {class superuser {user junipero {authentication {services {syslog {file messages {processes {chassis {alarm {sonet {images {scb {fpc {interfaces {at-2/1/1 {atm-options {unit 0 {at-2/2/0 {...snmp {community public {clients {routing-options {static {route 0.0.0.0/0 {route 192.168.0.0/16 {route 208.197.169.0/24 {protocols {rsvp {interface so-5/1/0 {mpls {interface so-5/1/0 {bgp {group internal {ospf {area 0.0.0.0 {interface so-5/1/0 {List all users who are logged in to the router except for the user "root":
user@host>show system users | except root8:28PM up 1 day, 13:59, 2 users, load averages: 0.01, 0.01, 0.00USER TTY FROM LOGIN@ IDLE WHATsheep p0 baa.juniper.net 7:25PM - cliSave the configuration, except for encrypted passwords, to a file:
user@host>show configuration | except SECRET-DATA | save my.output.fileDisplay the output, starting not at the beginning but rather at the first occurrence of text matching a regular expression, using the
findcommand after the pipe:user@host>command| findregular-expressionIf the regular expression contains spaces, operators, or wildcard characters, enclose the expression in quotation marks.
List the routes in the routing table starting at
208.197.169.0:user@host>show route | find 208.197.169.0208.197.169.0/24 *[Static/5] 1d 13:22:11> to 192.168.4.254 via so-3/0/0.0224.0.0.5/32 *[OSPF/10] 1d 13:22:12, metric 1iso.0: 1 destinations, 1 routes (1 active, 0 holddown, 0 hidden)+ = Active Route, - = Last Active, * = Both47.0005.80ff.f800.0000.0108.0001.1921.6800.4015.00/160*[Direct/0] 1d 13:22:12> via lo0.0Retaining the Output After the Last Screen
When you view output one screen at a time, you typically return to the CLI prompt after viewing the last screen.
To not return immediately, use the
holdcommand after the pipe. This feature is useful, for example, when you want to scroll or search through the output.user@host>command | holdDisplaying the Lines You Want to View from the End of the Output
By default, the
lastcommand displays the lines from the end of the output. You can also specify the number of lines to display from the end of the output. If the number of specified lines is less than a screen full, the command will display a screen full of output. This feature is most useful for viewing log files where the end of the files contain the most recent entries.To view the most recent entries from the end of the output, use the
| lastcommand after the pipe:user@host>showcommand|lastlines
linesspecifies the number of lines to display from the end of output.Displaying All Output at Once
To display the output all at once instead of one screen at a time, specify the
no-morecommand after the pipe. This command is equivalent to theset cli screen-length 0command, but affects the output of the one command only.user@host>command| no-moreSending Command Output to Other Users
If you are troubleshooting your router and, for example, talking with a customer service representative on the phone, you can use the
request messagecommand to send your representative the command output you are currently viewing on your terminal.The following example sends the output from the
show interfacescommand you enter on your terminal to the terminal of the userroot@ttyp1:root@ui6%whoroot ttyp0 Nov 3 12:42 (172.17.28.75)root ttyp1 Nov 30 10:32 (172.17.28.75)root@ui6% cliroot@ui6>show interfaces | request message user root@ttyp1The user
root@ttyp1sees the following output appear on his terminal screen:Message from root@ui6 on /dev/ttyp0 at 10:32 PST...Physical interface: dsc, Enabled, Physical link is UpInterface index: 5, SNMP ifIndex: 5Type: Software-Pseudo, MTU: Unlimited ...Placing Command Output in a File
When the output is very long, when you need to store or analyze the output, or when you need to send the output in e-mail, you can redirect the output to a file. Doing this is useful when the output scrolls off the screen, making it difficult to cut the output from a window and paste it into another.
To save the output to a file, specify the
savecommand after the pipe:user@host>command| savefilenameBy default, the file is placed in your home directory on the router. For information about how you can specify the filename, see Specifying Filenames and URLs.
This example stores the output of the
request support informationcommand in a file:user@host>request support information | savefilenameWrote 1143 lines of output to `filename'user@host>