4

我正在编写一个需要使用 scp 在计算机之间传输文件的 Perl 脚本。我知道公钥身份验证,但我需要脚本完全自动化,所以在脚本运行之前我无法访问机器来设置密钥。

有没有办法从 Perl 脚本将密码传递给 scp 或从 perl 脚本中设置密钥?

该脚本将作为构建脚本的一部分运行,该脚本还会重新映像运行脚本所需的硬盘驱动器。所以我不能在每次构建项目时都在那里设置密钥。

4

6 回答 6

3

您可以使用Net::SSH::Perl。以下是您可以使用的示例代码。

#!/usr/bin/perl -w
use strict;
use Net::SSH::Perl
my $cmd = 'command';
my $ssh = Net::SSH::Perl->new("hostname", debug=>0);
$ssh->login("username","password");
my ($stdout,$stderr,$exit) = $ssh->cmd("$cmd");
print $stdout;

此代码将简单地在远程机器上运行给定的“命令”,并在本地系统上为您提供输出。因此,您可以使用此脚本代替 scp,使用命令 'cat' 来捕获本地系统上的 'cat filename' 的输出,并将输出重定向到本地系统上的文件中。

希望这可以帮助。

于 2010-09-30T05:39:38.280 回答
2

使用 ssh-agent。如果你使用 Gnome,Gnome Keyring SSH Agent 非常棒。

于 2010-09-29T20:28:34.470 回答
1

您可以使用 Perl Expect模块,请参阅Well House 顾问论坛上的示例

它的文档有一个telnet 示例,可以轻松更改为 SSH。

Net::SSH::Expect是另一个 Perl 模块,可以完全满足您的需求。不过,我以前没有用过这个。

于 2010-09-29T19:04:09.397 回答
0

只需创建没有密码的密钥。

于 2010-09-29T18:55:10.190 回答
0

您可以使用SSH 密钥(无需密码)。

于 2010-09-30T05:42:57.473 回答
0
#!/usr/bin/perl -w
######################################################
#                                                    #
#                                #
#       Script to send files to server    #
#               Author: Jurison Murati               #
#                                   #
######################################################

use strict;
use Net::SCP::Expect;
use File::Copy;
use IO::Compress::Gzip qw(gzip $GzipError);
my $host = "192.168.0.1";
my $user = "user";
my $pwd = "password";
my $RemoteDir = </nodes>;
my $file;
my $displaydate= `date +'%Y%m%d%H%M%S'`;
print "Filloi dergimi date $displaydate\n";
my $scp = Net::SCP::Expect->new(host=>$host,user=>$user,password=>$pwd,recursive=>1);
my $dir = '/arch';
        opendir(DIR, $dir) or die $!;
while (my $file = readdir(DIR)) {
next if ($file =~ m/^\./);
        $scp->scp("$dir/$file","$RemoteDir") or die $scp->{errstr};
print "file $dir/$file moved on date $displaydate\n";
}
exit 0;
于 2012-09-27T09:28:02.437 回答