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 !

Recommended Answers

All 3 Replies

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

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!

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:

#!/bin/bash

base_ip="10.0.0."
for (( i=1; i<=100; i++ ))
do
     newip="${base_ip}$i";
     ./monkey_bars.exp $newip
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:

#!/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";}
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.