Operating System - HP-UX
1834374 Members
2597 Online
110066 Solutions
New Discussion

telnet to a remote server (router/switch) in a script

 
Ravi S. Banda
Regular Advisor

telnet to a remote server (router/switch) in a script

I need to telnet to a remote server in a script. The remote server is a multiprotocol router and there is no 'ls' or 'cp' 'cat .rhosts' capability on it.
I need to execute 3 commands on the MPR periodically.

I tried using ssh but it keeps asking for the password of admin.
ssh admin@hostname

I tried telnet -inv telnet.sh, where telnet.sh contains
open hostname
user username password

The above telnet is similar to ftp -inv ftp.sh with the values inside ftp.sh file. That didn't work, keeps asking for the username and password.

I tried telnet -l, but that asks for username/password as well.

Is there anyway I could accomplish this - I don't know if we could use 'expect' kind of tools.

Please help.
Thanks!
Ravi.
10 REPLIES 10
Ivan Ferreira
Honored Contributor

Re: telnet to a remote server (router/switch) in a script

You can use expect scrips or you can use perl scripts, like the attached.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ravi S. Banda
Regular Advisor

Re: telnet to a remote server (router/switch) in a script

I'm not an expert in perl (I see it as a substitute to unix shell script, I may be wrong!), but how do I execute a perl script:

/usr/contrib/bin/perl telnet_in_script.pl

I'm getting the following error message:

Can't locate strict.pm in @INC (@INC contains: /opt/perl5/lib/5.00502/PA-RISC1.1
/opt/perl5/lib/5.00502 /opt/perl5/lib/site_perl/5.005/PA-RISC1.1 /opt/perl5/lib
/site_perl/5.005 .) at telnet_in_script.pl line 3.
BEGIN failed--compilation aborted at telnet_in_script.pl line 3.

Thanks!
Ravi.
A. Clay Stephenson
Acclaimed Contributor

Re: telnet to a remote server (router/switch) in a script

The perl is /usr/contrib/bin is very, very old and obsolete. Look through your Applications CD's for at least Perl 5.6.x; 5.8.x is better. If you don't have your Applications CD's then install from here:
http://hpux.its.tudelft.nl/hppd/hpux/Languages/perl-5.8.8/

Executing a Perl script is similar to executing a shell script.

If the first line of a Perl script looks something like:
#!/usr/bin/perl

Then all your have to do is "myperl.pl" otherwise "perl myperl.pl" assumming that perl is in your PATH.

In any event, Perl's Net::Telnet module makes this simple and is easier than expect.
If it ain't broke, I can fix that.
Ravi S. Banda
Regular Advisor

Re: telnet to a remote server (router/switch) in a script

I changed the path like below:
$ export PATH=/opt/perl/bin:$PATH

$ cd /opt/perl/bin
$ ./perl -version

This is perl, v5.8.3

cd /home/
$ which perl
/opt/perl/bin/perl

perl telnet_in_script.pl

results in the following error:

Can't locate Net/Telnet.pm in @INC (@INC contains: /opt/perl/lib/5.8.3/PA-RISC1.
1-thread-multi /opt/perl/lib/5.8.3 /opt/perl/lib/site_perl/5.8.3/PA-RISC1.1-thre
ad-multi /opt/perl/lib/site_perl/5.8.3 /opt/perl/lib/site_perl .) at telnet_in_s
cript.pl line 4.
BEGIN failed--compilation aborted at telnet_in_script.pl line 4.
A. Clay Stephenson
Acclaimed Contributor

Re: telnet to a remote server (router/switch) in a script

The Net::Telnet module is not included in most standand Perl distribution but, being Perl, it is very easy to install it on your local host.

http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm

Download the gzipped file and gunzip zip then untar it. There will then be a README file that will take you through the rest of the installation.
If it ain't broke, I can fix that.
Rasheed Tamton
Honored Contributor

Re: telnet to a remote server (router/switch) in a script

Hi,

expect [http://expect.nist.gov/] is the correct method. Ot else you can do with netcat (nc).

Here is the telnet way. You have to use sleep in between your commands.

#!/usr/bin/ksh
(
sleep 4
echo open hostname
sleep 3
echo username
sleep 2
echo passwd
sleep 2
echo ls /tmp
sleep 2
sleep 1
echo who am i
sleep 1
echo exit

)|telnet
Maxim Yakimenko
Super Advisor

Re: telnet to a remote server (router/switch) in a script

For ssh to allow login without prompting for password, you must generate a key with empty password phrase and put it on server where you want to log in.
Ravi S. Banda
Regular Advisor

Re: telnet to a remote server (router/switch) in a script

Generating a key and placing it on the the remote server would NOT work as it is a multi protocol router and I cannot place any files there.

telnet using the sleep given by Rasheed gives the following message and doesn't execute anything (immediately exits):

Telnet TERMINAL-SPEED option ON
Connection closed by foreign host.
telnet> Trying...
Connected to 172.28.196.123.
Escape character is '^]'.
Local flow control on

perl suggestions given by Clay helped me install perl and use perl 5.8.8 without the Telnet.pm error, but the telnet.pl script just sits there without executing any commands after I provided the required arguments it needed.

ps: points have been submitted accordingly.

Thanks!
Ravi.
Rasheed Tamton
Honored Contributor

Re: telnet to a remote server (router/switch) in a script

Hi Ravi,

When you manually telnet to the said router how much time it takes. If it takes longer than usual, then increase the first sleep value to a higher value according to the connection speed. Try increasing to 8, 9, etc. It has to work.

Regards,
Rasheed Tamton.
Maxim Yakimenko
Super Advisor

Re: telnet to a remote server (router/switch) in a script

Ravi, Rasheed advised you to use Expect - this solution is the best for your situation, I used it in conjunction with telnet to gather stats from FC switches. Dont waste time :)