944,008 Members | Top Members by Rank

Ad:
Mar 19th, 2009
0

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

Expand Post »
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 !
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yhcheong is offline Offline
2 posts
since Mar 2009
Mar 19th, 2009
0

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

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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Mar 19th, 2009
0

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

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yhcheong is offline Offline
2 posts
since Mar 2009
Mar 19th, 2009
0

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

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:
bash Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Bash Script to start application after two checks
Next Thread in Shell Scripting Forum Timeline: problem with sudo and test





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC