【问题标题】:Net::SSH2 to ssh from Windows 7 to VMWare Guest CentOS 6.2 not working with Public and Private keysNet::SSH2 从 Windows 7 到 ssh 到 VMWare Guest CentOS 6.2 不使用公钥和私钥
【发布时间】:2012-11-14 01:14:24
【问题描述】:

我无法从 Windows 7 SSH 到 VMWare Guest CentOS 6.2

我的笔记本电脑上有 Windows 7 64 位操作系统。在上面安装了草莓 Perl 5.16.2。我还安装了 VMWare Player 并在其上运行 CentOS 6.2。

使用我在网上找到的文章,我已经成功地从 PUTTY SSH 到 CentOS 6.2,无需密码。

我在 CentOS 6.2 上创建了用户 perl514。使用下面的脚本,如果我使用用户名和密码登录,一切正常。 但它不适用于我使用 PUTTY 随附的 SSH KEYGEN 工具创建的公钥和私钥。

下面是脚本:

    #!C:\strawberry\perl\bin\perl.exe

    use Modern::Perl;
    use Net::SSH2;


    my $hostname = '192.168.247.128';
    my $username = 'perl514';
    my $password = 'redhat';

    my $ssh2 = Net::SSH2->new();
    say "Connecting to $hostname";

    $ssh2->connect("$hostname") || die "PROBELM - $!";

    #$ssh2->auth_password("$username","$password") || die "Username/Password not #right";#COMMENTED OUT. This Works. Stuff given below does not work.
    $ssh2->auth_publickey ("perl514", "C:\\Users\\winuser\\Documents\\perl\\work\\putty_priv.ppk",
                   "C:\\Users\\winuser\\Documents\\perl\\work\\public.pub") || die "ERROR", $ssh2->error;



    my $chan = $ssh2->channel();
    $chan->blocking(0);
    $chan->exec('ls -la');
    while (<$chan>){ print } 

我收到以下错误:

    Connecting to 192.168.247.128
    ERROR-19LIBSSH2_ERROR_PUBLICKEY_UNVERIFIEDInvalid public key at ssh2.pl line 17.

使用用户名和密码,它可以正常工作。但不是用公钥和私钥。

我很确定我在某个地方出错了。请帮助我。

【问题讨论】:

    标签: linux windows perl ssh-keys


    【解决方案1】:

    Net::SSH2 要求密钥文件采用不同于 PuTTY 使用的 OpenSSH 格式。

    您应该能够使用 PuTTY GUI 转换为 OpenSSH 格式。例如,请参阅How to convert SSH keypairs generated using PuttyGen(Windows) into key-pairs used by ssh-agent and KeyChain(Linux)

    更新

    详细的转换步骤:

    • 打开 PuTTY 密钥生成器
    • 将私钥加载到其中
    • 在转换菜单上,以 OpenSSH 格式导出私钥
    • 用鼠标从“用于粘贴到 OpenSSH 授权密钥文件的公钥”下的框中选择并复制 OpenSSH 的公钥,并将其粘贴到与您的文件同名的新文件中已给附加了“.pub”的私钥。

    【讨论】:

    • 嗨萨尔瓦,感谢您的回复。我确实浏览了您提到的文章,但它们似乎是在谈论从 linux 机器生成密钥以登录到另一个 linux 机器。在下面给出的步骤中,这是我能理解的:
    【解决方案2】:

    很抱歉回答这个问题而不是发表评论,但评论部分不允许我将步骤置于另一个之下。我检查了您的链接,这是我的理解:

        1.Open PuttyGen
        2.Click Load
        3.Load your private key
        4.Go to Conversions->Export OpenSSH and export your private key
    

    我知道上面的步骤是在 Windows Box 上运行的,但是下面的步骤呢?它们要在 CentOS Guest 上运行吗?不确定我是否必须在 CentOS 6.2 来宾操作系统中复制 putty 创建的私钥。另一个问题是 Puttygen 创建的私钥在其中包含私钥和公钥。

         5.Copy your private key to ~/.ssh/id_dsa (or id_rsa).
    
         6.Create the RFC 4716 version of the public key using ssh-keygen
    
        ssh-keygen -e -f ~/.ssh/id_dsa > ~/.ssh/id_dsa_com.pub
    
         7.Convert the RFC 4716 version of the public key to the OpenSSH format:
    
        ssh-keygen -i -f ~/.ssh/id_dsa_com.pub > ~/.ssh/id_dsa.pub
    

    我这样做的原因是,一旦这个测试设置工作,我将使用类似的方法登录到一些 NAS 阵列(基于 linux)来运行一些报告。因此提出了问题。

    【讨论】:

    • 您可以编辑您的问题,而不是创建答案。
    • 嗨萨尔瓦,对不起,我没有意识到这一点。您提供的链接还有另外两个链接,第一个正是我试图从我的 Windows 笔记本电脑(Office)进入 CentOS 来宾操作系统以无需密码登录的内容,它运行得非常好。由于某种原因,它在脚本中不起作用。我已经在我的个人笔记本电脑上安装了 Ubuntu 12.10,并将在上面进行尝试。之前在上面安装了 SL 6.3,但由于其他一些东西没有按预期工作,所以切换到 Ubuntu。我会试试这个,我相信它会起作用,只是这个 Windows 的东西不起作用。
    • 您可以使用$ssh2-&gt;debug(1)在 Net::SSH2 对象上激活调试
    【解决方案3】:

    我没有在 CentOS vm 上安装 ssh 服务器。安装 ssh 服务器后它工作了。

    yum -y install openssh-server openssh-clients
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 2013-03-24
      • 2016-03-08
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 2017-02-10
      • 2017-04-29
      相关资源
      最近更新 更多