1848631 Members
4739 Online
104033 Solutions
New Discussion

Re: telnet script

 
SOLVED
Go to solution
Donald C Nelson
Frequent Advisor

telnet script

Does anyone have a script that can automatically telnet to a number of servers in a loop to check if I can quickly connect and then disconnect and go on to the next server.
9 REPLIES 9
Steven E. Protter
Exalted Contributor

Re: telnet script

Telnet is probably not a good tool for that.

Thats becaue you have to provide a password which must be hardcoded into the script and is tranmitted across the network in clear text.

But:

You can do this:

Install this:
http://software.hp.com/portal/swdepot/displayProductInfo.do?productNumber=T1471AA

Set up password free public key exchange according to the word document I'm attaching:

Then run this check with ssh.

make a list of servers

serverlist

server1
server2

Then this little loop:

user=testit

while read -r server
do
ssh ${user}@${server} "exit"
rc=$?
if [ $rc -eq 0 ]
then
echo "${server} is up"
else
echo "${server} may have a problem"
#perhaps email out a notice.
fi
done < serverlist

Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Bejoy C Alias
Respected Contributor
Solution

Re: telnet script

U can also use the freely available tool 'expect' for autologin of telnet sessions . Site http://expect.nist.gov
Be Always Joy ......
Patrick Wallek
Honored Contributor

Re: telnet script

Well, expect MIGHT work for this. BUT there is a distinct flaw in the logic here, at least from my perspective.

You could easily script the telnet to other machines. No problem there. BUT the problem is what happens when you actually connect to the other machine? The machine you started the script on will lose control and your script will just sit there because it can't do anything on the remote machine.

So essentially you will successfully telnet to the first host, and then nothing will happen. It MIGHT proceed to the next host when the telnet connection times out, but no guarantee.

I would look at another way of verifying connectivity. telnet is not a good test.
harry d brown jr
Honored Contributor

Re: telnet script

install http://search.cpan.org/CPAN/authors/id/J/JR/JROGERS/Net-Telnet-3.03.tar.gz into perl.

then create this script:

#!/usr/bin/perl
use Net::Telnet ();
while () {
chop;
my $remote_host = $_;
printf("%s:",$remote_host);
$pop = new Net::Telnet (Timeout => 10);
$prev = $pop->errmode("return");
$pop->open(Host => $remote_host,
Port => 23);
$msgline = $pop->errmsg;
if ($msgline = $pop->errmsg) {
printf("%s.\n",$msgline);
} else {
printf("responded.\n");
}
}


then to use:

cat FILENAMEOFSYSTEMS | SCRIPTNAME

live free or die
harry d brown jr
Live Free or Die
harry d brown jr
Honored Contributor

Re: telnet script


telnet works fine, and you really don't need to login:

Here is some sample INPUT:

[root@vpart1 /]# cat /var/appl/perlscripts/tstports.data
vpart1
vpart2
vpart3
rndspt01
rndspt02
rndspt03
rndspt09

Here is the OUTPUT:

[root@vpart1 /]# cat /var/appl/perlscripts/tstports.data | /var/appl/perlscripts/tstports.pl
vpart1:responded.
vpart2:problem connecting to "vpart2", port 23: connect timed-out.
vpart3:responded.
rndspt01:responded.
rndspt02:responded.
rndspt03:responded.
rndspt09:problem connecting to "rndspt09", port 23: Connection refused.

SPECIAL NOTES:
vpart2 does not exist
rndspt09 is a WINDOZE box, thus has no telnetd running!

I put the output in the perl prog to use colon's (:) so you can "parse" and grep as you like!!!

live free or die
harry d brown jr
Live Free or Die
harry d brown jr
Honored Contributor

Re: telnet script

To install the module:

download it somewhere (anywhere)

gunzip Net-Telnet-3.03.tar.gz
tar -xvf Net-Telnet-3.03.tar
cd Net-Telnet-3.03
perl Makefile.PL
make
make test
make install

live free or die
harry d brown jr
Live Free or Die
Rick Garland
Honored Contributor

Re: telnet script

As mentioned in earlier post, telnet is not good idea. SSH tools are a better choice and you can execute remote scripts via a ssh session. The script will do the data collection for you and send it back to the initiating server.

You can setup ssh to login to other systems without passwds.

All of this is done encrypted. Much better security.

harry d brown jr
Honored Contributor

Re: telnet script

From what I read of the post, He ONLY wants to see if a server responds to telnet's, NOT EXECUTE ANYTHING so the use of "ssh" is meaningless!

live free or die
harry d brown jr
Live Free or Die
Donald C Nelson
Frequent Advisor

Re: telnet script

I will use some of the examples from the ones given.