What You’ll Learn

What You’ll Need

Sometimes in the course of managing the network, you want to save the output of your terminal session. Here are a couple of scenarios that may warrant this:

Some terminal applications provide a logging feature, but it is always good to have options to capture a record of everything that was done while interacting with the network infrastructure, especially in production. It may be hours later when you need to fill out a change request form and dump your output, or it may be weeks or months later when a latent issue pops up and you need to refer to your output to help solve a new problem. Having a record of your session is super helpful and a good practice.

tee Command

The tee command solves this problem by writing the output of your session into a specified file in the background while also displaying it to the screen as normal. The tee command is available in both Linux jump hosts (some networks have security in place that requires you to use a jump host) and within Cisco IOS as well. This means that you have little downside for a huge upside. You get a permanent file copy of your command output while also getting to view the output as normal.

The tee command is used with a pipe (|) in both Linux and Cisco devices. The syntax is simple: Type the command you want and then | tee FILENAME to save the output to the file, such as ls -l | tee ls_output.txt. If you want to append to an existing file, you can use the -a flag on the tee command, such as ls -l | tee -a ls_output.txt.

Try logging into the Linux DevBox in the Sandbox or a Linux terminal that you have access to and test the following commands, verifying the file output.

(py3venv) [developer@devbox ~]$ ls -l | tee ls_output.txt
total 4
drwxr-xr-x 2 developer root   145 Feb 11  2020 Desktop
drwxr-xr-x 2 developer docker  54 Apr 13  2020 Downloads
drwxr-xr-x 7 developer docker  98 Apr 13  2020 py3venv
-rw-rw-r-- 1 developer docker  17 Sep 14  2019 shutdown.sh
(py3venv) [developer@devbox ~]$
(py3venv) [developer@devbox ~]$
(py3venv) [developer@devbox ~]$ more ls_output.txt
total 4
drwxr-xr-x 2 developer root   145 Feb 11  2020 Desktop
drwxr-xr-x 2 developer docker  54 Apr 13  2020 Downloads
drwxr-xr-x 7 developer docker  98 Apr 13  2020 py3venv
-rw-rw-r-- 1 developer docker  17 Sep 14  2019 shutdown.sh
(py3venv) [developer@devbox ~]$ ls -l | tee -a ls_output.txt
total 8
drwxr-xr-x 2 developer root   145 Feb 11  2020 Desktop
drwxr-xr-x 2 developer docker  54 Apr 13  2020 Downloads
-rw-r--r-- 1 developer docker 234 Oct 14 14:45 ls_output.txt
drwxr-xr-x 7 developer docker  98 Apr 13  2020 py3venv
-rw-rw-r-- 1 developer docker  17 Sep 14  2019 shutdown.sh
(py3venv) [developer@devbox ~]$
(py3venv) [developer@devbox ~]$ more ls_output.txt
total 4
drwxr-xr-x 2 developer root   145 Feb 11  2020 Desktop
drwxr-xr-x 2 developer docker  54 Apr 13  2020 Downloads
drwxr-xr-x 7 developer docker  98 Apr 13  2020 py3venv
-rw-rw-r-- 1 developer docker  17 Sep 14  2019 shutdown.sh
total 8
drwxr-xr-x 2 developer root   145 Feb 11  2020 Desktop
drwxr-xr-x 2 developer docker  54 Apr 13  2020 Downloads
-rw-r--r-- 1 developer docker 234 Oct 14 14:45 ls_output.txt
drwxr-xr-x 7 developer docker  98 Apr 13  2020 py3venv
-rw-rw-r-- 1 developer docker  17 Sep 14  2019 shutdown.sh
(py3venv) [developer@devbox ~]$

You can also capture your entire session to a device, whether that is Telnet or SSH, using tee as well. Try connecting to dist-rtr01, which would be telnet 10.10.20.175 | tee dist-rtr01-telnet.txt (credentials are cisco / cisco):

(py3venv) [developer@devbox ~]$ telnet 10.10.20.175 | tee dist-rtr01-telnet.txt
Trying 10.10.20.175...
Connected to 10.10.20.175.
Escape character is '^]'.


User Access Verification

Username: cisco
Password:
dist-rtr01#

After logging into the device, now we can issue a simple show command and see the tee option from the |, with available storage locations.

dist-rtr01#show ip interface brief | tee ?
  /append     Copy and append output to URL (URLs supporting append operation only)
  bootflash:  Uniform Resource Locator
  flash:      Uniform Resource Locator
  ftp:        Uniform Resource Locator
  http:       Uniform Resource Locator
  https:      Uniform Resource Locator
  nvram:      Uniform Resource Locator
  pram:       Uniform Resource Locator
  tftp:       Uniform Resource Locator

dist-rtr01#show ip interface brief | tee boo
dist-rtr01#show ip interface brief | tee bootflash:sh-ip-int-output.txt
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       10.10.20.175    YES TFTP   up                    up
GigabitEthernet2       172.16.252.21   YES TFTP   up                    up
GigabitEthernet3       172.16.252.25   YES TFTP   up                    up
GigabitEthernet4       172.16.252.2    YES TFTP   up                    up
GigabitEthernet5       172.16.252.10   YES TFTP   up                    up
GigabitEthernet6       172.16.252.17   YES TFTP   up                    up
Loopback0              unassigned      YES unset  administratively down down

dist-rtr01#

After saving it to the bootflash:, we can then verify that the file is present, copy it if we want to, or simply log out and view the contents of the tee output on the Linux host.

dist-rtr01#dir bootflash:
Directory of bootflash:/

22      -rw-              649  Oct 14 2022 21:49:17 +00:00  sh-ip-int-output.txt
314497  drwx             8192  Oct 14 2022 20:19:47 +00:00  tracelogs
120961  drwx             4096  Oct 14 2022 20:11:31 +00:00  .dbpersist
18      -rw-             5199  Oct 14 2022 20:11:24 +00:00  cvac.log
40321   drwx             4096  Oct 14 2022 20:11:21 +00:00  .installer
354817  drwx             4096  Oct 14 2022 20:11:14 +00:00  iox_host_data_share
104833  drwx             4096  Oct 14 2022 20:11:13 +00:00  guest-share
370945  drwx             4096  Oct 14 2022 20:10:53 +00:00  onep
96769   drwx             4096  Oct 14 2022 20:10:39 +00:00  license_evlog
21      -rw-              209  Oct 14 2022 20:10:38 +00:00  csrlxc-cfg.log
20      -rw-               16  Oct 14 2022 20:10:36 +00:00  iosxe_config.txt.md5
19      -rw-                1  Oct 14 2022 20:10:36 +00:00  .cvac_version
169345  drwx             4096  Oct 14 2022 20:10:34 +00:00  pnp-info
112897  drwx             4096  Oct 14 2022 20:10:16 +00:00  .prst_sync
17      -rw-               30  Oct 14 2022 20:10:12 +00:00  throughput_monitor_params
298369  drwx             4096  Oct 14 2022 20:09:48 +00:00  virtual-instance
16      -rwx             1314  Oct 14 2022 20:09:26 +00:00  trustidrootx3_ca.ca
15      -rw-            20109  Oct 14 2022 20:09:26 +00:00  ios_core.p7b
161281  drwx             4096  Oct 14 2022 20:09:23 +00:00  gs_script
14      -rw-              419  Oct 14 2022 20:09:23 +00:00  mode_event_log
177409  drwx             4096  Oct 14 2022 20:09:22 +00:00  .rollback_timer
282241  drwx             4096  Oct 14 2022 20:09:22 +00:00  core
322561  drwx             4096  Oct 14 2022 20:09:18 +00:00  bootlog_history
12      -rw-        470791260   Nov 1 2020 15:06:02 +00:00  csr1000v-mono-universalk9.17.03.02.SPA.pkg
13      -rw-         48320460   Nov 1 2020 15:06:02 +00:00  csr1000v-rpboot.17.03.02.SPA.pkg
11      -rw-             1092   Nov 1 2020 15:06:01 +00:00  packages.conf

6286540800 bytes total (5435645952 bytes free)
dist-rtr01#exit
Connection closed by foreign host.
(py3venv) [developer@devbox ~]$
(py3venv) [developer@devbox ~]$ more dist-rtr01-telnet.txt
Trying 10.10.20.175...
Connected to 10.10.20.175.
Escape character is '^]'.


User Access Verification

Username: cisco

You can see from the ? output on the IOS device that the tee command uses /append instead of -a and provides options for storing the output, either locally on bootflash or even on remote servers. You can then use SCP or TFTP to copy the output files back to your device for local consumption. Alternatively, you also will have a copy of all those command outputs from your tee output file of the Telnet session as well.

Learn More