The Junos file command allows us to manipulate files on the local file system and copy files to and from remote servers using protocols such as SCP, FTP, and HTTP.
Junos Copying Files
Junos allows us to copy files on the local file system and to remote hosts. It can also copy files between routing engines.
The general format of the command is ‘file copy <source> <destination>’.
Junos Copy A File Between Directories On The Device
To copy files between directories you simply specify the source and destination.
lab@vMX-1> file list /var/home/lab
/var/home/lab:
.ssh/
config-backup1.conf
config-backup2.conf
config-backup3.conf
id_rsa.pub
new-user5-key.pub
lab@vMX-1> file copy /var/tmp/vMX-1-var-logs-yyyymmdd.tgz /var/home/lab/
lab@vMX-1> file list /var/home/lab
/var/home/lab:
.ssh/
config-backup1.conf
config-backup2.conf
config-backup3.conf
id_rsa.pub
new-user5-key.pub
vMX-1-var-logs-yyyymmdd.tgz
lab@vMX-1>
Junos Copy A File To An SCP Server
In this example, we’ll copy a file to a remote SCP server. When we specify just the source filename and not a full path, it assumes the source directory is /var/home/<user>/ which in our case here is /var/home/lab/.
lab@vMX-1> file copy config-backup1.conf scp://[email protected]/home/eve/config-backup1.conf
[email protected]'s password:
config-backup1.conf 100% 2515 159.5KB/s 00:00
Next, we’ll repeat the command, but not specify a path on the destination.
lab@vMX-1> file copy config-backup2.conf scp://[email protected]
[email protected]'s password:
config-backup2.conf 100% 2515 124.5KB/s 00:00
In both cases, it results in the file being placed in the home directory of the remote user we specified, in this case, eve. To prove this, we can check on the SCP server.
eve@linux-1:~$ ls
backup config-backup1.conf config-backup2.conf
eve@linux-1:~$
If we want the file to be placed somewhere else, we need to specify the full path.
Junos Copy A File To An SFTP Server
Unfortunately, you can’t use file copy for this. The assumption that we can just replace scp:// with sftp:// is incorrect. Junos will try to interpret that as a hostname instead.
lab@vMX-1> file copy config-backup1.conf sftp://[email protected]/home/eve/config-backup1.conf
ssh: Could not resolve hostname sftp: hostname nor servname provided, or not known
lost connection
error: put-file failed
error: could not send local copy of file
lab@vMX-1>
This was tested on a vMX running Junos 20.4R3.
Instead, you can use the sftp command.
lab@vMX-1> sftp [email protected]
[email protected]'s password:
Connected to 192.168.20.5.
sftp> put /var/tmp/vMX-1-var-logs-yyyymmdd.tgz
Uploading /var/tmp/vMX-1-var-logs-yyyymmdd.tgz to /home/eve/vMX-1-var-logs-yyyymmdd.tgz
/var/tmp/vMX-1-var-logs-yyyymmdd.tgz 100% 811KB 92.2KB/s 00:08
sftp> exit
lab@vMX-1>
You can also run sftp from the shell, assuming your user account is allowed to run ‘start shell’.
lab@vMX-1> start shell
% pwd
/var/home/lab
% sftp [email protected]
[email protected]'s password:
Connected to 192.168.20.5.
sftp> put config-backup3.conf
Uploading config-backup3.conf to /home/eve/config-backup3.conf
config-backup3.conf 100% 2515 165.6KB/s 00:00
sftp> quit
% exit
exit
lab@vMX-1>
Junos Copy A File Between Routing Engines
On devices with redundant routing engines, this can be a very helpful command. Unfortunately, I don’t have access to such a device just at the moment. If we assume we are currently logged in to re0, the format is similar to this:
file copy /var/tmp/filename re1:/var/tmp/filename
I’ve used this when transferring software images to the device, and the out-of-band network was slow. I copied the file to one routing engine over the network and then copied it directly from that routing engine to the other one using the above command.
Junos Listing Files
The commands file list and file list detail work similarly to a Unix ‘ls’ and ‘ls -l’. If you do not specify a path, it will list the home directory of the user you are logged in as.
lab@vMX-1> file list
/var/home/lab/:
.ssh/
config-backup1.conf
config-backup2.conf
config-backup3.conf
config-backup4.conf
lab@vMX-1> file list detail
/var/home/lab/:
total blocks: 56
drwx------ 2 lab 20 512 Nov 22 10:41 .ssh/
-rw-r--r-- 1 lab 20 2515 Dec 28 10:02 config-backup1.conf
-rw-r--r-- 1 lab 20 2515 Dec 28 10:02 config-backup2.conf
-rw-r--r-- 1 lab 20 2515 Dec 28 10:02 config-backup3.conf
-rw-r--r-- 1 lab 20 2515 Dec 28 10:02 config-backup4.conf
total files: 4
lab@vMX-1> file list /var/tmp detail
/var/tmp:
total blocks: 152
-rw-r--r-- 1 root wheel 0 Nov 5 05:46 LOCK_FILE
-rw-r--r-- 1 root wheel 870 Dec 28 04:47 appidd_cust_app_trace
-rw-r--r-- 1 root wheel 10712 Dec 28 04:47 appidd_trace_debug
-rw-rw-rw- 1 root wheel 57 Nov 5 05:50 bcast.bdisp.log
-rw-rw-rw- 1 root wheel 73 Nov 5 05:50 bcast.disp.log
-rw-r--r-- 1 root wheel 59 Nov 5 05:50 bcast.rstdisp.log
-rw-rw-rw- 1 root wheel 66 Nov 5 05:50 bcast.undisp.log
-rw-r--r-- 1 root wheel 1243 Nov 5 05:47 ex_autod_config
-rw-r--r-- 1 root wheel 155 Nov 5 05:46 ex_autod_rollback_cfg
-rw-r--r-- 1 root wheel 57 Dec 28 04:47 krt_rpf_filter.txt
frw------- 1 root wheel 0 Nov 5 05:48 mmcq_authd
frw------- 1 root wheel 0 Nov 5 05:48 mmcq_bbeStatsdGetCollector
frw------- 1 root wheel 0 Nov 5 05:46 mmcq_mmdb_rep_mmcq
frw------- 1 root wheel 0 Nov 5 05:47 mmcq_sdb_bbe_mmcq
-rw-r--r-- 1 root wheel 111 Nov 5 05:45 pfe_debug_commands
drwxrwxrwx 2 root wheel 512 Nov 5 05:29 pics/
drwxr-xr-x 2 root wheel 512 Nov 5 05:46 rtsdb/
drwxr-xr-x 2 root wheel 512 Nov 5 05:43 sd-upgrade/
-rw-r--r-- 1 root wheel 202 Nov 5 06:16 test.lic
drwxrwxrwt 2 root wheel 512 Nov 5 06:16 vi.recover/
total files: 16
lab@vMX-1>
Junos Renaming And Deleting Files
The file rename and file delete commands behave as you would expect them to.
lab@vMX-1> file list
/var/home/lab/:
.ssh/
config-backup1.conf
config-backup2.conf
config-backup3.conf
config-backup4.conf
lab@vMX-1> file rename config-backup4.conf config-newbackup.conf
lab@vMX-1> file list
/var/home/lab/:
.ssh/
config-backup1.conf
config-backup2.conf
config-backup3.conf
config-newbackup.conf
lab@vMX-1> file delete config-newbackup.conf
lab@vMX-1> file list
/var/home/lab/:
.ssh/
config-backup1.conf
config-backup2.conf
config-backup3.conf
lab@vMX-1>
Junos Creating And Compressing File Archives
These two commands allow us to create a file archive and compress it as a tarball, similar to a zip file.
I’ve often used this to collect all of the log files together and compress them before copying them off of the device.
lab@vMX-1> file archive compress source /var/log/* destination /var/tmp/vMX-1-var-logs-yyyymmdd
tar: Removing leading '/' from member names
lab@vMX-1> file list /var/tmp/vMX-1* detail
-rw------- 1 lab wheel 830472 Dec 28 10:49 /var/tmp/vMX-1-var-logs-yyyymmdd.tgz
total files: 1
lab@vMX-1>
I want to highlight the destination filename. I like to put these archived files in /var/tmp so they are out of the way. The filename can be whatever you want, but I find it useful to use a format of <hostname>-<directory>-<datestamp>.
If you are a support engineer, this is helpful because it identifies what host it came from, what the contents are, and when it was made. This may not sound important, but if you deal with a large network and many support cases, having a file named ‘logs.tgz’ doesn’t tell you much. Using the yyyymmdd datestamp also allows for easy sorting of the files, especially if you have to get the log files over several consecutive days.
Junos Showing A File
The ‘file show’ command will display the contents of a file and paginate the output.
lab@vMX-1> file show /var/home/lab/config-backup3.conf
## Last changed: 2023-12-28 05:19:19 UTC
version 20.4R3-S2.6;
system {
host-name vMX-1;
root-authentication {
encrypted-password "REMOVED"; ## SECRET-DATA
}
login {
class my-class {
permissions all;
allow-commands "(commit confirmed *)|(commit check)";
deny-commands "commit *";
}
user lab {
uid 2000;
class super-user;
authentication {
encrypted-password "REMOVED"; ## SECRET-DATA
}
}
user user1 {
---(more)---
Junos Calculate Checksum For A File
This can be used to calculate the checksum for a file. One example is using it to confirm the checksum of a software image after transferring it to the device, to ensure it is the same as the source file.
The command is ‘file checksum <type> <file>’ where the type is md5, sha1, or sha256.
lab@vMX-1> file checksum md5 config-backup3.conf
MD5 (/var/home/lab/config-backup3.conf) = a2f1f2fae0926c9abcc497066b79649a
lab@vMX-1>