tags:

views:

79

answers:

2

I have the following script,

#!/usr/bin/perl

use strict;
use warnings;
use Net::SSH::Perl;
use Expect;

my $logs = "logs";
open(LOG,'>>',"$logs") or die "can't logs $!\n";

my $domain = 'domain.com';
my @host = qw/host/;

foreach my $host (@host) {
   my $cmd = "passwd user1";
   my $sshost = join('.', $host, $domain);
   my $ssh = Net::SSH::Perl->new("$sshost");
   $ssh->login('root');
   $ssh->debug();
   my ($stdout, $stderr, $exit) = $ssh->cmd($cmd);
   print LOG $stdout,"\n";
}

Now my problem is I don't know how to use Expect to send the password after the $cmd is executed and it's time to key in the password. $stdin won't work in this case since we're using HPUX.

Appreciate any guidance and sample, reading the Expect docs don't result something for me.

+3  A: 

I don't think that's possible unfortunately. However, Net::SSH::Expect seems to be able to do what you want.

Mikael S
Yeah, seems on Synopsis of it, it's indeed what I need unfortunately it's not possible to install it on this server.
lupin
Compile errors or something else?
Mikael S
+1  A: 

I summarize: you need Expect, and the ssh module has no use.

I'll be more precise: if I understand your source code, your requirement, in human terms, is something like this: log in to a collection of Unix hosts and use passwd(1) to update root's password on each. Do I have that right?

I expect there's frustration in all directions, because variations of this question have been answered authoritatively for at least two decades. That's no reflection on you, because I recognize how difficult it is to find the correct answer.

While Net::SSH is a fine and valuable module, it contributes nothing to the solution of what I understand to be your requirements. You need Expect.

As it turns out, the standard distribution of the Tcl-based Expect includes an example which addresses your situation. Look in http://www.ibm.com/developerworks/aix/library/au-expect/ > for the description of passmass.

Identical functionality can be coded in Expect.pm, of course. Before I exhibit that, though, I ask that original questioner lupin confirm I'm on track in addressing his true requirements.

Cameron Laird
Yes, my requirement is:
lupin
login to server list, thus I use Net::SSH::Perl then update the password of the users(not root).
lupin
The passmass to which I refer above comes close. Net::SSH truly is not part of the solution. Do you, lupin, want more details with something like passmass (based on Tcl) or Expect.pm (based on Perl)? Of which users on the collection of hosts are you updating the password?
Cameron Laird
Things have become a bit confused--that is, *I* am confused. I agree that **Net::SSH::Expect** has sufficient functionality to implement **passmass** -like functionality. I don't understand lupin's apparent constraint that **Net::SSH::Perl** and **Expect.pm** can be installed on his server, but **Net::SSH::Expect** cannot. I have no idea at this point what combination of tools (Perl, and some modules, but not others? Tcl?) is available to lupin.
Cameron Laird
Thus 2 modules(Net::SSH::Expect/Expect.pm) were already installed, the constraint is I can't install another module due to:
lupin
*) Not allowed, I don't own this server *) So as much as possible I need to use Net::SSH::Perl and Expect.pm
lupin
A solution is feasible given the elements you've described so far. Of which users on the collection of hosts are you updating the password?
Cameron Laird
Actually the script above is a test script I don't added the list of users yet nor the list of servers, though I did put there a single server as the value of $sshost and for the user on value of $cmd which is 'user1' on this case, my problem is that I don't know how to use expect.pm to achieve my goal which is to key-in/supply the new password once the $cmd command is executed and the prompt for new password comes up.
lupin
'Might be as late as Tuesday before I provide a working example.
Cameron Laird
thanks, I'll try do change the passmass too(ssh), see if that will do.
lupin