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

Reply

Join Date: Mar 2009
Posts: 2
Reputation: yhcheong is an unknown quantity at this point 
Solved Threads: 0
yhcheong yhcheong is offline Offline
Newbie Poster

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

 
0
  #1
Mar 19th, 2009
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 !
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

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

 
0
  #2
Mar 19th, 2009
Hey there,

You could use the output of netstat and pipe that to a line count. Something like:

netstat -an |grep "[2]3"|wc -l

althought that might give you some false positives and you should echo the statement if you want to dump the out in a variable, since wc will put leading space before the count.

Best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 2
Reputation: yhcheong is an unknown quantity at this point 
Solved Threads: 0
yhcheong yhcheong is offline Offline
Newbie Poster

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

 
0
  #3
Mar 19th, 2009
Hi mike,

Thanks for the good idea. But what I want is output which could lead me that the telnet connection is okay. For example:

10.x.x.x ok
10.x.x.x not connected

Any idea?
Thanks!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

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

 
0
  #4
Mar 19th, 2009
You could probably get away with using expect. Basically, you could have your script call the expect script in a loop (or figure out how to make expect do a loop) which will be for each machine you want to test. So, make your shell script do a loop, where the variable gets concatenated to a string that is the IP address:
  1. #!/bin/bash
  2.  
  3. base_ip="10.0.0."
  4. for (( i=1; i<=100; i++ ))
  5. do
  6. newip="${base_ip}$i";
  7. ./monkey_bars.exp $newip
  8. done
Then, you have to create the expect script, which I named monkey_bars.exp. If you change the name of the expect script, you'll need to change it in the above bash script as well...anyway, you can expect the expect script to look like this, though, you may want to modify it to behave differently, and catch other outputs that this one neglects:
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/usr/bin/expect
  2. set timeout 20
  3.  
  4. set Machine [lindex $argv 0]
  5.  
  6. spawn -noecho -console telnet $Machine
  7.  
  8. set log [open monkey.bars a]
  9.  
  10. expect {
  11. login {puts $log "$Machine UP!";}
  12. -re "Connection refused" {puts $log "$Machine DOWN";}
  13. }
Honestly, the only reason I put this as a possibility, is because you posted this in the shell scripts forum, so you were probably looking for a scripting solution.... however, this is a hack job. The proper way to do this, is with a C++ program, or even perl script, that can be used to create its own sockets. Then, you would do a loop, and iterate over a list of new sockets, and cleanly retrieve the responses into a file. This script solution is buggy, and hacky.... consider what happens when a computer (say 10.0.0.3) is not up and running.... and I don't mean just not running telnet... I mean powered off. The response is different (no route to host) than if it simply was refusing connections. There are a number of other caveats with this method, that would be better solved with an actual application, but this hack-job will get the job done if tweaked just right.

note: The expect script creates a file called "monkey.bars" in the present working directory of the expect script. That file contains the results of the scanned servers.
Last edited by Comatose; Mar 19th, 2009 at 7:27 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC