问题
I'm new to programming. I have written a perl script which does the following.
1) ssh into a remote machine
2) execute a sequence of commands.
The script requires user interaction in between.It needs user name and password. I'm not sure how to proceed with that.
use Net::SSH::Expect;
use strict;
use warnings;
#login to a remote host
my $ssh = Net::SSH::Expect->new (host => "ipaddr",
password=> 'pwd',
user => 'username',
raw_pty => 1);
my $login_output = $ssh->login();
The login is successful. Now, for the below scp command i'm prompted to enter the username and password of that system.The script stops here.
my $cpscp = $ssh->exec("copy scp install ip addr filename");
Enter usename for remote scp server:
Enter the password for the remote server:
Once the user name is entered I need to enter the password.This is what i did but it didn't work.
my $usr = $ssh->waitfor ("Enter usename for remote scp server:\s*\z ", 5);
if ($usr)
{
print("string found \n");
my $pwd =$ssh->send("root");
}
else
{
print("No string found\n");
}
The script is unable to identify it. "no string found"
I also tried the following
$ssh->waitfor('Enter username for remote scp server:\s*\z ', 5) or die "not found";
$ssh->send("root");
回答1:
I imagine the prompt for a username has already arrived as part of the response to
my $cpscp = $ssh->exec("copy scp install ip addr filename");
so a subsequent waitfor
will wait for ever.
You should use send
with waitfor
instead of exec
, like this
$ssh->exec('copy scp install <ip-addr> <remote-dir> <remote-filename>');
my $found = $ssh->waitfor ('Enter usename for remote scp server:\s*\z', 5);
Note also that the string you use in waitfor
is probably wrong
"Enter usename for remote scp server:\s*\z "
You have usename
when you probably need username
, and you have put a space at the end of the string, which is impossible because \z
marks the end of your input and there clearly can be nothing after the end.
It is also bad manners to post the same question to two sites simultaneously. If Perl Monks doesn't come up with a working solution for you then by all means ask the question here, but you are wasting our time if, unknown to us, your problem already has a solution elsewhere.
回答2:
I imagine the prompt for username has already arrived as part of the response to the exec
my $cpscp = $ssh->exec("copy scp install ip addr filename");
so a subsequent waitfor
will wait for ever.
You should use send
with waitfor
instead of exec
, like this
$ssh->exec('copy scp install <ip-addr> <remote-dir> <remote-filename>');
my $found = $ssh->waitfor ('Enter usename for remote scp server:\s*\z', 5);
Note also that the string you use in waitfor
is probably wrong
"Enter usename for remote scp server:\s*\z "
You have usename
when you probably need username
, and you have put a space at the end of the string, which is impossible because \z
marks the end of your input and there clearly can be nothing after the end.
It is also bad manners to post the same question to two sites simultaneously. If Perl Monks doesn't come up with a working solution for you then by all means ask the question here, but you are wasting our time if, unknown to us, your problem already has a solution elsewhere.
来源:https://stackoverflow.com/questions/24295468/perl-script-for-user-interaction