I'm need to connect to another host in a script using the telnet command. How do I pass the login and password to be able to connect to the other host from within the script?
When you open a telnet session in a script you must read the session prompts and write the replies. For example, you open a telnet session in a script and read the session and when you match the word 'Login' (or whatever) you write the user id. The same for password.
In other words, you automate in the script what you manually do at the keyboard. When you do it from the keyboard, you telnet, then wait for the login prompt. When you get the login prompt, you type the login. etc.
Writing a script is the same for telnet sessions, however instead of the keyboard, you echo (or write) in the script. Instead of seeing the login problem, you match the pattern in the script (your eyes, so to speak).
I understand the concept very well. I have tried to automate it in my script but it doesn't take the login and password. Do you have an example?
I don't have an example handy, but as I recall the working construct is based on this form of code fragment:
while {
#put conditional logic here
} telnet 111.222.333.444
I once wrote a script that automatically 'telnetted' into hundreds of Cisco routers and changed the passwords when an employee was terminated. This was about 7 years ago and my memory on the actual code is foggy. I do remember that we used KSH and used a contruct like the one above.
I suggest you get out your best shell programming book and use the construct above in the correct syntax. For example, I don't recall if you use a pipe "|" or perhaps a "<" redirection tag to send the telnet output to the while logic block. You would have to get this from studying the construct and perhaps some simple trial and error. Sorry not to remember more.
When you get it right, please post.
Thanks Neo, I'm also trying to telnet into hundreds of routers, which is why I need the telnet command (because of port 23) When I get it right I will post it.
Great. When you get it working for TELNET, the same idea will work for SSH. If you are using Cisco, they now support SSH. As PxT points out, this might be a good future revision if you require a more secure solution.
Telnet is a good first step. Cisco has SSH built into the recent IOS releases.
You can use a pipe to send the output to telnet. Something like:
#!/bin/ksh
( echo open hostname
sleep 5
echo username
sleep 1
echo password
sleep 1
echo some more output, etc. ) | telnet
Combine with a while loop or however you want to do it...
HTH
Better idea would be to use Perl with its Telnet module...
It's very easy to configure, and I could post an example if you'd like.
Hezki
That would be great if you have the example handy. I would really appreciate it.
Hi,
Here's a script I wrote to collect the accounting of the top "heavy" connection within a time interval from a cisco router. (using TELNET
if you don't have the NET::TELNET perl module grab it from you nearest CPAN site. ) You'd find in it all the help about the Perl telnet object elements etc..
here it is (excluding passwords and IP addresses ) :
#!/usr/local/bin/perl
# By Hezki Englander / 22.3.2000
#
use Net::Telnet ();
if ($#ARGV < 0) {
print "Usage: $0 interval [num_of_conn]\n";
exit;
}
$machine = '111.111.111.111';
$logfile = "cisco.log";
$interval = $ARGV[0];
if ($#ARGV > 0)
{ $connum = $ARGV[1]; }
else
{ $connum = 1; }
$| =1;
$t = new Net::Telnet (Timeout => 10);
$t->input_log($logfile);
$t->open($machine);
print "Connected to $machine\n";
$t->waitfor('/Password: $/');
$t->print("passwd");
$t->prompt('/LTD-INC>/');
$t->cmd("enable");
$t->waitfor('/Password: $/');
$t->print("passwd2");
$t->prompt('/LTD-INC#/');
$t->cmd("clear ip accounting");
print "Logged in successfully.\n";
print "Collecting statistics";
$count = 0;
while ($count < $interval) {
sleep(1);
print ".";
$count++;
}
$t->cmd("show ip accounting");
@result = $t->cmd("logout");
print "done.\n";
print "Most heavy connection/s :\n" ;
print $result[0];
system("echo '@result' | sort -n -k 4 |tail -$connum");
$t->input_log("");
added code tags for readability --oombera
i tried the following.. and got my system stuck. that is after executing this everything went fine and i got a prompt.. but i could not execute anything from that.
what is going wrong?
#!/bin/csh
( echo open hostname
sleep 5
echo username
sleep 1
echo password
sleep 1
echo dtterm ) | telnet
added code tags for readability --oombera