Solved

Telnet connectivity query

Posted on 2009-05-19
3
590 Views
Last Modified: 2012-08-13
Hi All,
I want to check telnet connectivity for a series of 50 IPs.
I tried to automate the tasks using a script like the following:
a=`echo pwd | telnet IP1`
a=`echo pwd | telnet IP2`
a=`echo pwd | telnet IP3`
...
...
a=`echo pwd | telnet IP50`

When I run the above script, it usually returns the chracaters "Connection closed by foreign host" for each IP.
Sometimes it returns different messages like "Connection refused", "No route to Host" etc.

But the problem occurs when the telnet fails altogether for a IP. If the telnet fails for a particular IP, the script waits for 3 minutes and returns the characters "telnet failed" and then the script is terminated.
Is there any way I can make the script return the character "telnet failed" within, say 3 seconds, for every IP it fails to connect to.
In other words, if I just run the command "telnet IP" on UNIX prompt and the telnet fails then I should get back "telnet failed" message within 3 seconds instead of 3 mins.
0
Comment
Question by:subratoc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
lanboyo earned 500 total points
ID: 24425518
When the telnet is failing completely, it is often due to talking to a firewalled IP that does not return a port rejected icmp message or that icmp message is swallowed somewhere along the ling.

Variable assignment will not allow the script to proceed until it is complete.

To add a layer of complexity you can rewrite the script in the scripting language expect. Fairly easy to learn for automating usually interactive processes.

The script would then be something like:


set timeout 3
spawn telnet $IP
expect  -timeout {do this ; close ; wait ; exit 0} "*Password:" { do this ; send "$PASSWORD\r"}


... And so on.


You might be able to simply do this with temp files....


echo password  | telnet 192.168.1.3 >  /tmp/tmp.$$ 2>&1 &
sleep 1
A=`cat /tmp/tmp.$$`
if  ps -p $! 1> /dev/null 2>&1 ; then
   sleep 2
   A=`cat /tmp/tmp.$$ `
   kill $!  1> /dev/null 2>&1
fi
echo password | telnet 192.168.1.5 > /tmp/tmp.$$ 2>&1  &
sleep 1
B=`cat /tmp/tmp.$$`
if  ps -p $! 1> /dev/null 2>&1 ; then
   sleep 2
   B=`cat /tmp/tmp.$$`
   kill $!  1> /dev/null 2>&1
fi
#kill -9 $! 1> /dev/null 2&1
echo cisco | telnet www.google.com > /tmp/tmp.$$  2>&1  &
sleep 1
C=`cat /tmp/tmp.$$`
if  ps -p $! 1> /dev/null 2>&1 ; then
   sleep 2
   C=`cat /tmp/tmp.$$`
   kill $!  1> /dev/null 2>&1
fi
rm /tmp/tmp.$$  1> /dev/null  2>&1
echo "BEGIN A====>$A<===END A"
echo =========================================
echo "BEGIN B====>$B<===END B"
echo =========================================
echo "BEGIN C====>$C<===END C"



With these results...

BEGIN A====>Trying 192.168.1.3...
Connected to 192.168.1.3.
Escape character is '^]'.
Connection closed by foreign host.<===END A
=========================================
BEGIN B====>telnet: Unable to connect to remote host: Connection refused
Trying 192.168.1.5...<===END B
=========================================
BEGIN C====><===END C


0
 
LVL 10

Expert Comment

by:lanboyo
ID: 24425530
expect is preferable, as it would allow you to check for good and bad passwords.
0
 

Author Closing Comment

by:subratoc
ID: 31582952
Thanks, lanboyo. It worked perfectly!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Secure Shell (SSH) is a network protocol for secure data communication, mainly used to administer remote Unix / Linux servers via command line. But it also allows the user to open a secure tunnel between a client and a server where he can send any k…
Hello to you all, I hear of many people congratulate AWS (Amazon Web Services) on how easy it is to spin up and create new EC2 (Elastic Compute Cloud) instances, but then fail and struggle to connect to them using simple tools such as SSH (Secure…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question