Unix & Linux: Testing remote TCP port using telnet by running a one-line command (8 Solutions!!)
Channel: Roel Van de Paar
Date: 19/08/2020

Unix & Linux: Testing remote TCP port using telnet by running a one-line command


The Question: I have numerous linux boxes with a very limited set of commands and disk space.
But it has the telnet command on it.
I remotely connect to each of these probes (programmatically) and issue one
line linux command through SSH.
I need to run a single command to connect to a specific machine, using telnet,
and then disconnect right away.
I can do all that, but the disconnection right away part. Telnet opens some
sort of a console, or terminal and I can't figure out a one-line command to run
the telnet command and then disconnect right away.
If I do that, I can easily parse the textual output for error messages for not
being able to connect to the machine on the specified port and that's exactly
what I am looking for.
So how can I run a one-line command to connect to a machine using telnet and
disconnect afterwards ?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 3 people ==
I think better tool for sending commands directly and just getting output would
be netcat. It just simple, but powerful tool for putting commands through
ports. You could see usage example in this superuser question: https://
superuser.com/questions/261900/how-can-i-pipe-commands-to-a-netcat-that-will-
stay-alive - asker gives working example in which connection closes after few
seconds.
And if you want just to test connectivity use this: http://
terminalinflection.com/use-netcat-not-telnet-to-test-network-connectivity/

== This solution helped 30 people ==
You ought to be able to pipe the exit command into STDIN in telnet. Try:
echo 'exit' | telnet {site} {port}
and see if that works. (it seems to work on my web server, but YMMV).

== This solution helped 10 people ==
The simplest and easiest method is given below.
 sleep ＜n＞ | telnet ＜server＞ ＜port＞
n - The wait time in seconds before auto exit. It could be fractional like 0.5.
Note that some required output may not be returned in the specified wait time.
So we may need to increase accordingly.
server - The target server IP or hostname.
port - Target service port number.
You can also redirect the output to file like this,
sleep 1 | telnet ＜server＞ ＜port＞ ＞ output.log

== This solution helped 5 people ==
In my case this works. (CentOS 7):
while read host port; do
r=$(bash -c 'exec 3＜＞ /dev/tcp/'$host'/'$port';echo $?' 2＞/dev/null)
if [ "$r" = "0" ]; then
echo "$host $port is open"
else
echo "$host $port is closed"
exit 1 # To force fail result in ShellScript
fi
done

== This solution helped 2 people ==
This is another version of the https://unix.stackexchange.com/questions/86556/
testing-remote-tcp-port-using-telnet-by-running-a-one-line-command/
339643#answer-339643 that makes it act a little more like "normal" telnet
syntax. If you like my answer, please give an upvote not to this, but to the
original.
#!/bin/bash
if [ "$2" == "" ]; then
echo "Syntax: $0 ＜host＞ ＜port＞"
exit;
fi

host=$1
port=$2

r=$(bash -c 'exec 3＜＞ /dev/tcp/'$host'/'$port';echo $?' 2＞/dev/null)
if [ "$r" = "0" ]; then
echo "$host $port is open"
else
echo "$host $port is closed"
exit 1 # To force fail result in ShellScript
fi

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user Vladimir (https://unix.stackexchange.com/users/317550), user theglossy1 (https://unix.stackexchange.com/users/262156), user Seff (https://unix.stackexchange.com/users/72411), user Paulo Tome (https://unix.stackexchange.com/users/40484), user Muhammad Gelbana (https://unix.stackexchange.com/users/25989), user legatoproteus (https://unix.stackexchange.com/users/145697), user IBr (https://unix.stackexchange.com/users/37559), user fpmurphy (https://unix.stackexchange.com/users/7919), user Chuss (https://unix.stackexchange.com/users/212033), user Charles Newey (https://unix.stackexchange.com/users/45035), user Alexander Mills (https://unix.stackexchange.com/users/113238), user Abhishek Kumar (https://unix.stackexchange.com/users/306669), and the Stack Exchange Network (http://unix.stackexchange.com/questions/86556). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.