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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Create a shell script to test the telnet connection status
# 8  
Old 03-19-2009
Quote:
Originally Posted by ghostdog74
what version is your sun solaris OS?

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

Thanks
# 9  
Old 03-19-2009
Quote:
Originally Posted by ghostdog74
what version is your sun solaris OS?
The version is

SunOS 5.8 Generic_117350-38 sun4u sparc SUNW,Sun-Fire-V890
# 10  
Old 03-19-2009
Quote:
Originally Posted by pludi
Please use [code ][/code] (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
Code:
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



Hi,
Code:
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

Code:
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
# 11  
Old 03-19-2009
not guaranteed to work, but you can try
Code:
# sleep 3 | telnet somewhere portnumer

use grep or awk to grab messages to confirm connection or refusal
# 12  
Old 03-19-2009
Quote:
Originally Posted by yhcheong
Code:
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

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.
Code:
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

# 13  
Old 03-19-2009
Quote:
Originally Posted by pludi
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.
Code:
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.
Code:
 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:
Code:
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

# 14  
Old 03-19-2009
/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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Telnet in script Connection closed by foreign host

Hello, I'm trying to learn and automate some tasks via a script, but my first ever script failed with "connection closed by foreign host" error. I checked the other discussions but it didn't help. Could you please help? #!/bin/bash ( sleep 2 echo open x.x.x.x 23 sleep 2 echo user sleep 2... (1 Reply)
Discussion started by: Myrtle
1 Replies

2. Shell Programming and Scripting

Test with non-zero status does not exit shell

Hi there, I'm very used to use set -e to break my scripts if any command exits with a non-zero status. As a policy, I'm willingly expecting echo hello | grep a to break the script. The commands test 1 -eq $1 && echo hello exits with a non-zero status if $1 is not 1. BUT... It doesn't break... (2 Replies)
Discussion started by: chebarbudo
2 Replies

3. Shell Programming and Scripting

Telnet Bash Script (Connection closed by foreign host.)

Hello Everyone, My following script is giving me problems, when the SIP trunk goes down and the telnet session is started and just when the command is about to complete the connection is closed then script restarts. I have noticed that as soon the script types in "sys re" or "sys rebo" or... (6 Replies)
Discussion started by: jeetz
6 Replies

4. UNIX for Dummies Questions & Answers

Shell Script to test telnet connection using port

Hello, I need to test telnet connections using port number for few hosts. Could you please help me? Thanks !! (1 Reply)
Discussion started by: skhichi
1 Replies

5. Shell Programming and Scripting

How to test for the ssh exit status in script?

Hello; I regularly run monitoring scripts over ssh to monitoring scripts But whenever a server is hung or in maintenance mode, my script hangs.. Are there anyways to trap exit status and be on my way ?? Looked at the ssh manpage and all I can see is a "-q" option for quiet mode .. Thank... (2 Replies)
Discussion started by: delphys
2 Replies

6. Shell Programming and Scripting

Success/failure status of telnet connection

Hi, I am running a shell script which will spawn the telnet and login. But sometimes, the telnet session itself is not getting spawned. My requirement is, if the telnet session is not spawned, the user must be notified that it failed. Is there any command to capture the status of telnet... (2 Replies)
Discussion started by: merin
2 Replies

7. Shell Programming and Scripting

shell script/telnet - Remove/Control "Connection closed by foreign host"

How do I gain control of the "Connection closed by foreign host" message telnet yields when you connect to it in a shell script? I'm using the output: #!/usr/local/bin/bash count=$(ping -c 1 $1 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') if ; then echo "$1 PING "... (2 Replies)
Discussion started by: phpfreak
2 Replies

8. Shell Programming and Scripting

bash script to test network connection - please help

I want to test if my host can connect to any of the following 10 hosts (192.168.1.0 to 192.168.1.9) I didnt know which command i should use, so i chose ping. (i wonder if i should use tracepath or sth else) my code is the following: #!/bin/bash clear firstPart="192.168.1" maxNum="9" ... (2 Replies)
Discussion started by: shadow_boi
2 Replies

9. Shell Programming and Scripting

Connection Test Script

I am writing a script to do some conditional logic based on the results of a connection test. I need to capture the output of netcat, the 3 expected conditions are as follows. I need to capture the output of this command. I could write this to a file, but I would like to do it clearer if possible.... (1 Reply)
Discussion started by: princeboot
1 Replies

10. Shell Programming and Scripting

Database Connection test in unix Script

i have a unix script that gives me the sysdate from the database EDNAMID.WORLD.What i want my script to do the following 1) Establish a database connection 2) if database connection is successfull then echo the message "Connected" 3) put the o/p of the Sql query in a spool file 4) then... (3 Replies)
Discussion started by: ali560045
3 Replies
Login or Register to Ask a Question