1752806 Members
5586 Online
108789 Solutions
New Discussion юеВ

Checking if telnet works

 
SOLVED
Go to solution
dictum9
Super Advisor

Checking if telnet works


I need to check if telnet works

i.e.

telnet box1 # this works and gives meaningful output

telnet box2 # error, doesn't work, record different output

Problem is, once I get into the telnet program, it's interactive, and I would like to do it with a script.
11 REPLIES 11
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Checking if telnet works

Bear in mind there could many many reasons that telnet is not working: wrong hostname, unreachable, bad user name, bad passwd, no telnetd daemon listening, ...

Probably the easist method would be to download and install the Perl module, Net::Telnet. You get all the error checking for free without having to slug through stderr yourself.

Another approach would be to use expect but Net::Telnet is easier.
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Checking if telnet works

download expect from http://expect.nist.gov/ and use it for automating telnet.
A. Clay Stephenson
Acclaimed Contributor

Re: Checking if telnet works

Okay, I spent about 5 minutes throwing together a complete working Perl script, checktelnet.pl. All it really does is check to see if the telnet daemon is listening on a given host. It returns a zero status on success and non-zero on failure and prints any error messages on stdder --- just like a real UNIX command. It doesn't try to login so usernames and passwords are not checked although that could be easily added.

Use it like this (and note that I am discarding stderr):
---------------------------------
#!/usr/bin/sh

typeset STAT -i STAT=0
typeset REMHOST="mickey"


checktelnet.pl ${REMHOST} 2>/dev/null
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Ok"
else
echo "Cannot connect to ${REHHOST}; status ${STAT}"
fi
exit ${STAT}
-------------------------------
You can treat the Perl script as a blackbox since all you really want to know is telnetd running on a given host.

You will probably need to install the Net::Telnet module. Get it from:
http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm

and the attached perl script, checktelnet.pl:
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Checking if telnet works

Hi:

Here's one approach:

# cat ./probe
#!/usr/bin/sh
typeset HOST=${1}
telnet ${HOST} 21 |&
sleep 3
read -p REPLY
read -p REPLY
print -p quit
ACTION=`echo ${REPLY}|cut -d" " -f1`
if [ "${ACTION}" != "Connected" ]; then
echo "${HOST} FAILED to connect"
else
echo "${HOST} reponded_ok!"
fi
exit 0

...Run as:

# ./probe somehostname

...OR: Add a driver loop to query multiples hosts. A simple file of hostnames (or IP addresses) serves as input in that case.

...The exact message(s) returned may vary.

...for a good, working host:

# ./probe goodhost
Connection closed by foreign host.
goodhost reponded_ok!

...for a non-working host:

# ./probe badhost
telnet: connect: A remote host refused an attempted connect operation.
./probe[7]: print: 0403-039 No query process for pipe.

Regards!

...JRF...
dictum9
Super Advisor

Re: Checking if telnet works

Thanks for the ideas. The problem is complicated by the fact that I am telneting to remote console servers and checking which ports are active and which aren't. So the first time I am login in to the console server, which doesn't mean anything. The second time I should get a prompt for the actual console of the machine. It's this 2nd time that I am interested in.

There doesn't appear to be an easy way of scripting this, short of using expect. I got expect installed and am contemplating scripting it, but from my experience with it, it's very tedious (much more than Perl or ksh) and you have to take care of all input possibilities to make the right decision in the 'if' statements.

Steven E. Protter
Exalted Contributor

Re: Checking if telnet works

Shalom etc,

Could be that telnet or inetd is nor running on the second box.

Some consoles don't accept telnet at all. The new ilo consoles I'm receiving now only work with ssh.

If you have physical access to the second box:

Make sure telnetd is enabled in inetd.conf
make sure inetd itself is running

tail -f /var/adm/syslog/syslog.log

Try your telnet, then try ssh.

SEP
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
dictum9
Super Advisor

Re: Checking if telnet works

I will keep in mind the suggestion about inetd

Are you sure you can ssh to a remote console server? I've never seen it done, and every time I tried, it has failed while telnet always worked. I've always assumed it was a built-in feature. If this is configurable, where does it get set?

inventsekar_1
Respected Contributor

Re: Checking if telnet works

the answer is /etc/inetd.conf

and there should be an entry like(not commented):
telnet stream tcp6 nowait root /usr/lbin/telnetd telnetd

the master daemon inetd consults this file for many network services. do a more /etc/inetd.conf

Be Tomorrow, Today.
Sandman!
Honored Contributor

Re: Checking if telnet works

Suggest you run autoexpect. This utility starts a script and records all the commands that are typed by the user into a file "script.exp". After you are done telnet'ing into eachj of the remote server's GSP you can exit out of the script and examine it contents. It will have all that you need in order to automate the process of checking the LAN console of each of the remote GSP's.

hope it helps!