Find out whether remote port is open using Bourne Again SHell network redirections.
Examine whether port 22
/TCP
is open on 127.0.0.1
.
$ PROTO="tcp";DST_ADDR="127.0.0.1";DST_PORT=22;(</dev/${PROTO}/${DST_ADDR}/${DST_PORT} && STATUS="OPEN" || STATUS="CLOSED OR UNAVAILABLE"; echo Port ${DST_PORT}/${PROTO^^} on ${DST_ADDR} is ${STATUS} ) 2>/dev/null
Port 22/TCP on 127.0.0.1 is OPEN
It is open, so read from the TCP socket using Bourne Again SHell.
$ timeout 1 cat </dev/tcp/127.0.0.1/22
SSH-2.0-OpenSSH_7.9p1 Debian-10+deb10u2
Find out whether port 53
/UDP
is open on 10.0.0.1
.
$ PROTO="udp";DST_ADDR="10.1.0.1";DST_PORT=53;(</dev/${PROTO}/${DST_ADDR}/${DST_PORT} && STATUS="OPEN" || STATUS="CLOSED OR UNAVAILABLE"; echo Port ${DST_PORT}/${PROTO^^} on ${DST_ADDR} is ${STATUS} ) 2>/dev/null
Port 53/UDP on 10.1.0.1 is CLOSED OR UNAVAILABLE