Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

telnet, then su - command on remote host using perl

Status
Not open for further replies.

RobJordan

MIS
Apr 3, 2001
296
US
I am able to log in and run some commands on a remote
host as follows. I am not sure how to do interactive
commands like "su -" which will prompt for user id
and password.

Thank you in advance
smiletiniest.gif
,

Robert

print "Enter USER NAME on $dst : ";
chop ($user = <STDIN>); # get user name and remove carriage return
system 'stty', '-echo'; # Turn echo off for the password
print &quot;Enter the PASSWORD ( It will not be echoed on the screen ) : &quot;;
chop ($su_pass = <STDIN>); #get user password and remove carriage return
system 'stty', 'echo'; # turn echo back on
print &quot;\n&quot;;

$t = Net::Telnet -> new (); # create handle for telnet
$t->open(&quot;$dst&quot;); # open telnet session
$t->login($user, $pass); # login with user name and password
@ls = $t->cmd(&quot;/usr/bin/ls&quot;); # run ls command on remote machine
print &quot;ls : @ls &quot;; # print results of ls command
 
I came on to ask exactly the same question. I did find exceptional tips on this forum on other issues.

I definitely remember reading this could be done in PERL - using interactive programs in scripts - but just recollect how & where.

Any help would be greatly appreciated.

Thanks
 
There is a module, somewhat similar(ish) to Telnet called Expect, which is designed to do just this sort of thing. Mike
______________________________________________________________________
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Here is a section of one of my scripts that shows how to do this sort of thing with Net-Telnet.

$t = new Net::Telnet;
$t->errmode(&quot;return&quot;); # return on errors
$t->open($host);
&WaitFor(&quot;login:&quot;);
$t->print($username);
&WaitFor(&quot;Password:&quot;);
$t->print($password);
&WaitFor(&quot;]#&quot;);
$t->print('uptime');
$t->getline;
($up) = $t->getline;
print &quot;uptime for $host is $up\n&quot;;
&Waitfor(&quot;]#&quot;);
close($t);

sub WaitFor($) {
my ($string) = &quot;@_&quot;;
($prematch, $match) = $t->waitfor(String => $string);
unless (defined $match) {
my $error = $t->errmsg;
print &quot;ERROR: $error\n&quot;;
exit(1);
}
}
 
ya, I believe the telnet module has replaced the expect module, and the expect module has become obsolete
 
interesting... where did you pick that up from? Mike
______________________________________________________________________
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Raider2001 - Your tip works wonderfully.

I was wondering why this module hasn't made it to Core Perl. So many other Net modules are in core now. I checked Perl 5.7.3 dev too and Net::Telnet isn't there in it.

Has any of you find any security hole or something in this module? My team members were concerned. It is a pretty long module and I don't thing I can proof-read it to be sure.

It would be interesting to know.
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top