我正在尝试使用Net :: SSH :: Perl编写一个perl脚本
它现在很简单,因为我只想在ssh上的目录中执行ls.
#!/usr/bin/perl
use Net::SSH::Perl;
@KEYFILE = ("/user/.ssh/id_rsa");
$ssh = Net::SSH::Perl->new($host, debug=>1, identity_files=>\@KEYFILE)
my $host = "hostname";
my $user = "user";
#-- set up a new connection
my $ssh = Net::SSH::Perl->new($host, debug=>1, identity_files=>\@KEYFILE)
#-- authenticate
$ssh->login($user);
#-- execute the command
my($stdout, $stderr, $exit) = $ssh->cmd("ls -l /home/user/");
Run Code Online (Sandbox Code Playgroud)
这是有效的,但唯一的问题是,我需要跳过堡垒服务器来运行命令.我将我的私钥转发到堡垒,但我坚持的是如何在perl中使用转发密钥,而不是使用必须在服务器上的密钥.
这可能吗?
谢谢
连接到堡垒服务器时,您必须启用代理转发:
my $ssh = Net::SSH::Perl->new(
$host,
debug => 1,
identity_files => \@KEYFILE,
options => [
'ForwardAgent yes',
],
);
Run Code Online (Sandbox Code Playgroud)
有关其他 ssh Perl 模块及其优缺点,请参阅Net::OpenSSH 与 Net::SSH:: 模块。