Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dudeitssm/ad3fce6db4d45740752c321e9f5c3aef to your computer and use it in GitHub Desktop.
Save dudeitssm/ad3fce6db4d45740752c321e9f5c3aef to your computer and use it in GitHub Desktop.
File Transfer Over Serial Console With GNU Screen And lrzsz / rz / sz

File Transfer Over Serial Console With GNU Screen And lrzsz / rz / sz

Description

It is possible to transfer files over serial console when access through the network is unavailable, using the lrzsz suite.

For example, we can upload or download firmware, backups and configuration files to an OpenWRT device over a serial console.

Pre-requisites

In this guide, the host device is defined as your computer; the guest device is defined as the device being accessed via serial console.

Install required packages:

# HOST SIDE
dnf install screen lrzsz -y

# GUEST SIDE (BETTER TO COMPILE THIS PACKAGE INTO THE FIRMWARE; IT DOES NOT TAKE MUCH SPACE AT ALL)
# THIS CAN BE EXTREMELY HELPFUL FOR BACKUP AND RECOVERY PURPOSES AS WELL AS DEVELOPMENT PURPOSES
opkg install lrzsz

On your host, set the screen configuration file to automatically detect zmodem protocol transfers and prompt for uploads/downloads:

cat >> ~/.screenrc << EOF

# AUTO DETECT AND PROMPT SZ AND RZ FILE TRANSFERS OVER SERIAL CONSOLE
zmodem catch

# ALLOW SCROLLING SCREEN BUFFER USING MOUSE WHEEL
termcapinfo xterm* ti@:te@

EOF

Send File To Guest

# ON THE HOST, CREATE A TEST FILE
$ echo "hello world" > ~/hello_world

# OPEN SCREEN SESSION TO GUEST
$ screen /dev/ttyUSB00 115200n8

Please press Enter to activate this console.

WDR3600-AP login: root
Password:

root@WDR3600-AP:~#

# ISSUE THE `rz` COMMAND TO START RECEIVING
# YOU WILL SEE AN OUTPUT SIMILAR TO THE FOLLOWING

root@WDR3600-AP:~# rz
rz waiting to receive.

# A NEW PROMPT APPEARS ON THE `screen` WINDOW
:!!! sz -vv -b /home/$USER/hello_world

# ^^ PASTE IN THE PATH TO THE FILE YOU WANT TO UPLOAD AFTER IT
# YOU SHOULD SEE THE TRANSFER WITH AN ETA TIME

root@WDR3600-AP:~# rz
rz waiting to receive.
Sending: hello_world
Bytes Sent:     12   BPS:410

Transfer complete

root@WDR3600-AP:~# cat hello_world
hello world

Receive File From Guest

# OPEN SCREEN SESSION TO GUEST
$ screen /dev/ttyUSB00 115200n8

Please press Enter to activate this console.

WDR3600-AP login: root
Password:

root@WDR3600-AP:~#

# CREATE A TEST FILE
root@WDR3600-AP:~# echo "hello there" > hello_there

# SEND THE FILE TO THE HOST
root@WDR3600-AP:~# sz hello_there
rz

# A NEW PROMPT APPEARS ON THE `screen` WINDOW
:!!! rz -vv -b -E

# ^^ PRESS ENTER, WHICH WILL SAVE THE FILE IN YOUR PRESENT WORKING DIRECTORY (WHERE YOU OPENED `screen` FROM)

root@WDR3600-AP:~# sz hello_there
rz
Receiving: hello_there
Bytes received:      12/     12   BPS:1436

Transfer complete

# DETACH SCREEN SESSION
<CTRL+A> D

$ cat ~/hello_there
hello there

Helpful References

https://gangmax.me/blog/2016/12/08/using-rz-slash-sz-to-transfer-files/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment