dobroho_vechora/dobroho_vechora.bash

60 lines
844 B
Bash
Raw Normal View History

2024-12-16 17:42:18 +00:00
#!/bin/bash
set -e
ip="${1}"
port="${2}"
2024-12-16 17:46:50 +00:00
2024-12-16 17:42:18 +00:00
print_usage() {
echo "${1}"
echo "${0} <IP> <PORT>"
exit
}
2024-12-16 17:46:50 +00:00
[ -z "${ip}" ] && print_usage 'No IP given.'
[ -z "${port}" ] && print_usage 'No port given.'
[ -z "${3}" ] || print_usage 'Too many arguments.'
2024-12-16 17:42:18 +00:00
send_message() {
exec 3<> "/dev/tcp/${ip}/${port}"
echo "${1}" >&3
exec 3>&-
}
get_message() {
exec 3<> "/dev/tcp/${ip}/${port}"
echo "${1}" >&3
timeout 1s cat <&3 || true
exec 3>&-
}
while :; do
read cmd
case "${cmd}" in
i)
b=""
while :; do
read c
[[ "${c}" != "." ]] || break
if [ -z ${b} ]; then
b="${c}"
else
b="${b}"$'\n'"${c}"
fi
done
send_message 0"${b}"
;;
I)
read a
send_message 0"${a}"
;;
p)
send_message 1
2024-12-20 14:07:54 +00:00
get_message 2 | sed -e 's/\x1B/ESC/g'
2024-12-16 17:42:18 +00:00
;;
q)
exit
;;
*)
echo ?
;;
esac
done