How to Create a shell script to test the telnet connection status

Hi friends,

I'm newbie to shell script. I wanted to create a shell script which able to write a result for all the telnet connection status. For example, from this machine I want to test the telnet connection (total 100+ servers) with this machine.

Any idea how to write this shell script?
Appreciate if you guys could help on this.
Thanks !

if you have Python, you can use the script here. the Python script tests for ports. For telnet, just substitute with 23. other solutions include using nmap.

Thanks for the feedback.
The current machine is running on sun solaris OS, it does not has python. The script I want to test is test the telnet connection with other 100+ machines. Is there any shell script?
The nmap seems like I need to install on the server.
Thanks in advance

nmap isn't quite that useful to test connections. You could install netcat and test the connection using the -z switch, eg

netcat -z ${MACHINE} 23
if [ $? -eq 0 ]
then
    echo "Telnet accepting connections"
else
    echo "Telnet connections not possible"
fi

Or, if you have bash available and don't want to install anything else you could try replacing the netcat call with

exec 3>/dev/tcp/${MACHINE}/23

But this is really, really fugly.

Hi Pludi,

Thanks for the suggestion, if running bash. I think it is same by executing below command
for one by one(since I have hundred over telnet connection). The telnet connection port is
2071

% telnet 10.x.x.x 2071

for the bash command
exec 3>/dev/tcp/${10.x.x.x}/2071
if [ $? -eq 0 ]
then
echo "Telnet accepting connections"
else
echo "Telnet connections not possible"
fi

It seems like not working.

bash: /dev/tcp/${10.x.x.x}/2071: bad substitution.

Do you have any idea which can lead me to run the script in one shot, and the output is the hundred over telnet connection status. THanks in advance!

what version is your sun solaris OS?

Please use (sans the space) tags for source and listings, it's easier to read.

The ${MACHINE} that I used was in preparation for the loop that you'll need for that many machines (it's substituted by the value of the variable MACHINE)

Change it like this and try again

MACHINE=10.0.0.1 # Change to what ever you need
exec 3>/dev/tcp/${MACHINE}/2071
if [ $? -eq 0 ]
then
    echo "Telnet accepting connections"
else
    echo "Telnet connections not possible"
fi

SunOS 5.8 Generic_117350-38 sun4u sparc SUNW,Sun-Fire-V890

Thanks

The version is

SunOS 5.8 Generic_117350-38 sun4u sparc SUNW,Sun-Fire-V890

Hi,

MACHINE=10.2.191.100 
MACHINE=10.1.101.100 
MACHINE=10.3.181.100 
exec 3>/dev/tcp/${MACHINE}:2061
if [ $? -eq 0 ]
then
    echo "Telnet accepting connections"
else
    echo "Telnet connections not possible"
fi

and the result is below

bash-2.03$ MACHINE=10.2.191.100
bash-2.03$ MACHINE=10.1.101.100
bash-2.03$ MACHINE=10.3.181.100
bash-2.03$ exec 3>/dev/tcp/${MACHINE}:2061
bash: /dev/tcp/10.3.181.100:2061: Not a directory
bash-2.03$ if [ $? -eq 0 ]
> then
>     echo "Telnet accepting connections"
> else
>     echo "Telnet connections not possible"
> fi
Telnet connections not possible

Sorry, please guide me.
Many Thanks

not guaranteed to work, but you can try

# sleep 3 | telnet somewhere portnumer

use grep or awk to grab messages to confirm connection or refusal

If you copy and paste, do so correctly. It's /dev/tcp/<machine>/<port>. The seperator is always the forward slash.
And you might want to start with some basic texts about shell programming, because that is not how you construct a loop (except maybe if you can create a DWIMTD function). There's a good introduction available here. It's written for Linux, but there are very few specific things used, most applies to any platform that bash runs on.

MACHINES="10.2.191.100 10.1.101.100 10.3.181.100"
for MACHINE in ${MACHINES}
do
    exec 3>/dev/tcp/${MACHINE}/2061
    if [ $? -eq 0 ]
    then
        echo "${MACHINE}: Telnet accepting connections"
    else
        echo "${MACHINE}: Telnet connections not possible"
    fi
done

Thanks for the guide. But it seems like this line of the code not functioning.

 exec 3>/dev/tcp/${MACHINE}/2061

When I check the dev/TCP, it isn't a directory. I've execute the shell code, but the error prompt as below:

bash-2.03$
bash-2.03$ MACHINES="10.97.124.100 10.12.44.10"
bash-2.03$ for MACHINE in ${MACHINES}
> do
>     exec 3>/dev/tcp/${MACHINE}/2061
>     if [ $? -eq 0 ]
>     then
>         echo "${MACHINE}: Telnet accepting connections"
>     else
>         echo "${MACHINE}: Telnet connections not possible"
>     fi
> done
bash: /dev/tcp/10.97.124.100/2061: Not a directory
10.67.170.100: Telnet connections not possible
bash: /dev/tcp/10.12.44.10/2061: Not a directory

/dev/tcp/... isn't a directory on any UNIX system that I know of, but a hint for bash to open a TCP connection and use it as a file handle. If bash complains about it that probably means that this functionality isn't supported.

I have check the the directory under dev. The TCP is exist. Hmm, any idea other than bash?
THanks!

Which one is it? Does it exist in the /dev branch or doesn't it?
For an alternative, see one of my older posts in this thread: http://www.unix.com/shell-programming-scripting/105041-how-create-shell-script-test-telnet-connection-status.html\#post302298999
Either way you'll have to install something (newer bash or netcat, and I'd suggest netcat) or you'll have to write a small program to do what you want.

Yes, it does exist, but I guess is not supported. The only solution is to install the netcat on the sun solaris?
Thanks for the suggestion, but I hope there will be an alternate solution for me. :slight_smile: