scripts/shell/dev-tcp.sh
2024-03-20 11:28:46 -05:00

36 lines
1,012 B
Bash
Executable file

#!/usr/bin/env bash
#
# bash /dev/tcp redirection to check Internet connection
#
# SPDX-License-Identifier: MIT
TCP_HOST=example.com # A given website
TCP_PORT=80 # Port 80 is http
# Try to connect. (Somewhat similar to a 'ping' . . .)
echo "HEAD / HTTP/1.0" >/dev/tcp/${TCP_HOST}/${TCP_PORT}
MYEXIT=$?
: <<EXPLANATION
If bash was compiled with --enable-net-redirections, it has the capability of
using a special character device for both TCP and UDP redirections. These
redirections are used identically as STDIN/STDOUT/STDERR. The device entries
are 30,36 for /dev/tcp:
mknod /dev/tcp c 30 36
>From the bash reference:
/dev/tcp/host/port
If host is a valid hostname or Internet address, and port is an integer
port number or service name, Bash attempts to open a TCP connection to the
corresponding socket.
EXPLANATION
if [ "X$MYEXIT" = "X0" ]; then
echo "Connection successful. Exit code: $MYEXIT"
else
echo "Connection unsuccessful. Exit code: $MYEXIT"
fi
exit $MYEXIT