【问题标题】:Net::SSH::Perl using forwarded SSH keyNet::SSH::Perl 使用转发的 SSH 密钥
【发布时间】:2013-08-14 03:59:38
【问题描述】:

我正在尝试使用 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/");

这可行,但唯一的问题是,我需要跳过堡垒服务器来运行命令。我将我的私钥转发到堡垒,但我坚持的一点是如何在 perl 中使用转发的密钥,而不是使用必须在服务器上的密钥。

这可能吗?

谢谢

【问题讨论】:

    标签: perl ssh


    【解决方案1】:

    连接堡垒服务器时必须启用代理转发:

    my $ssh = Net::SSH::Perl->new(
        $host,
        debug          => 1,
        identity_files => \@KEYFILE,
        options        => [
            'ForwardAgent yes',
        ],
    );
    

    有关其他 ssh Perl 模块及其优缺点,请参阅Net::OpenSSH vs Net::SSH:: modules

    【讨论】:

      【解决方案2】:

      您可以将文件内容作为标量引用传入。

      my $ssh = Net::SSH::Perl->new($host, identity_files => [ \$file_contents ]);
      

      但是,如果您要通过堡垒服务器进行 ssh,则转发 SSH 代理是执行此操作的最佳方式(如 abraxxa 上所示)。

      【讨论】:

        猜你喜欢
        • 2019-10-09
        • 2023-03-19
        • 1970-01-01
        • 2018-06-17
        • 2021-09-09
        • 1970-01-01
        • 1970-01-01
        • 2018-09-08
        • 2014-04-29
        相关资源
        最近更新 更多