Net::SSH::Perl 使用转发的 SSH 密钥 [英] Net::SSH::Perl using forwarded SSH key

查看:61
本文介绍了Net::SSH::Perl 使用转发的 SSH 密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Net::SSH::Perl 编写一个 perl 脚本

I am trying to write a perl script using Net::SSH::Perl

目前很简单,因为我只想通过 ssh 在目录中执行 ls.

Its quite simple at the moment as I just want to perform an ls in a directory over ssh.

#!/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 中使用转发的密钥,而不是使用必须在服务器上的密钥.

This works, but the only problem is, I need to jump through a bastion server to run the commands. I forward my private key to the bastion, but the bit I am stuck on is how to use the forwarded key in perl, rather than using a key which would have to be on the server.

这可能吗?

谢谢

推荐答案

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

You have to enable agent forwarding when connecting to the bastion server:

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

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

For other ssh Perl modules and their pros and cons see Net::OpenSSH vs Net::SSH:: modules.

这篇关于Net::SSH::Perl 使用转发的 SSH 密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆