Skip to content

Instantly share code, notes, and snippets.

@fumiyas
Last active March 4, 2021 00:59
Show Gist options
  • Save fumiyas/f9cfbaceea30b494e81a to your computer and use it in GitHub Desktop.
Save fumiyas/f9cfbaceea30b494e81a to your computer and use it in GitHub Desktop.
socat over serial port
#!/bin/sh
##
## socat over serial: getty
## Copyright (c) 2016 SATOH Fumiyasu @ OSS Technology Corp., Japan
##
## License: GNU General Public License version 3
##
set -u
run() {
echo "`date '+%Y/%m/%d %H:%M:%S'` $*" 1>&2
"$@"
}
serial_speed="115200"
connect_ip="127.0.0.1"
if [ $# -ne 2 ]; then
echo "Usage: ${0##*/} <SERIAL_PATH> <CONNECT_PORT>"
exit 1
fi
serial_path="$1"; shift
connect_port="$1"; shift
while :; do
run /sbin/agetty \
--wait-cr \
--skip-login \
--login-program /usr/bin/socat \
--login-options "-,cfmakeraw TCP:$connect_ip:$connect_port" \
"$serial_path" \
"$serial_speed" \
;
[ $? -ne 0 ] && exit 1
done
#!/bin/sh
##
## socat over serial: Listener
## Copyright (c) 2016 SATOH Fumiyasu @ OSS Technology Corp., Japan
##
## License: GNU General Public License version 3
##
set -u
socat_opts="-d -d"
listen_ip="127.0.0.1"
serial_speed="115200"
if [ $# -ne 2 ]; then
echo "Usage: ${0##*/} <LISTEN_PORT> <SERIAL_PATH>"
exit 1
fi
listen_port="$1"; shift
serial_path="$1"; shift
socat_kick_getty_sh='
[ -n "${BASH_VERSION+set}" ] && shopt -s lastpipe 2>/dev/null
{ echo; exec cat; } |exec socat $socat_opts - "$serial_path$serial_opts" |{ read x; exec cat; }
'
if [ -z "${serial_path##/dev/tty*}" ]; then
serial_opts=",b$serial_speed,cfmakeraw"
else
serial_opts=""
fi
export socat_opts serial_path serial_opts
while :; do
socat \
$socat_opts \
TCP-LISTEN:"$listen_port",bind="$listen_ip",reuseaddr \
SYSTEM:"$socat_kick_getty_sh" \
;
[ $? -ne 0 ] && exit 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment