r0c/clients/bash.sh

60 lines
1002 B
Bash
Raw Normal View History

2018-01-27 21:36:16 +00:00
#!/bin/bash
set -e
# --------------------------------------------
# tcp plaintext client without linebuffering
# --------------------------------------------
# uses bash internals wherever possible,
# let me know if I missed some opportunities
# --------------------------------------------
debug=''
#debug=1
cat_pid=''
function ctrl_c()
{
kill $cat_pid
exit 0
}
function connect()
{
# hook ctrl-c for cleanup
trap ctrl_c INT
# open the tcp connection
exec 147<>/dev/tcp/$1/$2
# dump socket to stdout
cat <&147 & cat_pid=$!
# read keyboard and send each key to the socket
2018-02-10 19:36:32 +00:00
while IFS= read -rn1 x
2018-01-27 21:36:16 +00:00
do
2020-01-19 19:06:16 +00:00
[ "x$x" == "x" ] &&
2018-02-10 19:36:32 +00:00
x=$'\n'
2020-01-19 19:06:16 +00:00
[ $debug ] &&
2018-01-27 21:36:16 +00:00
{
printf '%s' "$x" |
tee /dev/stderr 2>&1 >&147 |
xxd -c8 -g1 >> /dev/shm/r0c-log
} ||
printf '%s' "$x" >&147
done
}
2020-01-19 19:06:16 +00:00
[ "x$2" == x ] &&
2018-01-27 21:36:16 +00:00
{
echo
echo " r0c client (bash edition)"
echo " need argument 1: r0c server ip or hostname"
echo " need argument 2: r0c server port"
echo
exit 1
}
connect $1 $2