| | |
How to Create a shell script to test the telnet connection status
![]() |
•
•
Join Date: Mar 2009
Posts: 2
Reputation:
Solved Threads: 0
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 !
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 !
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
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:
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:
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.
bash Syntax (Toggle Plain Text)
#!/bin/bash base_ip="10.0.0." for (( i=1; i<=100; i++ )) do newip="${base_ip}$i"; ./monkey_bars.exp $newip done
Shell Scripting Syntax (Toggle Plain Text)
#!/usr/bin/expect set timeout 20 set Machine [lindex $argv 0] spawn -noecho -console telnet $Machine set log [open monkey.bars a] expect { login {puts $log "$Machine UP!";} -re "Connection refused" {puts $log "$Machine DOWN";} }
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.
![]() |
Similar Threads
- php.ini confusion (PHP)
Other Threads in the Shell Scripting Forum
- Previous Thread: Bash Script to start application after two checks
- Next Thread: problem with sudo and test
| Thread Tools | Search this Thread |






