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
# 1  
Old 03-19-2009
Error 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 !
# 2  
Old 03-19-2009
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.
# 3  
Old 03-19-2009
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
# 4  
Old 03-19-2009
nmap isn't quite that useful to test connections. You could install netcat and test the connection using the -z switch, eg
Code:
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
Code:
exec 3>/dev/tcp/${MACHINE}/23

But this is really, really fugly.
# 5  
Old 03-19-2009
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!
# 6  
Old 03-19-2009
what version is your sun solaris OS?
# 7  
Old 03-19-2009
Please use [code ][/code] (sans the space) tags for source and listings, it's easier to read.
Quote:
Originally Posted by yhcheong
Code:
exec 3>/dev/tcp/${10.x.x.x}/2071
if [ $? -eq 0 ]
then
    echo "Telnet accepting connections"
else
    echo "Telnet connections not possible"
fi

bash: /dev/tcp/${10.x.x.x}/2071: bad substitution.
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

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