ssh perl脚本没有运行

时间:2016-04-23 04:07:26

标签: bash perl shell ssh

我正在尝试编写一个ssh到perl中的远程计算机的脚本。

我不确定是什么问题但是当我运行脚本时,它会提示我输入root密码,并在我提供密码后以空白输出结束。

这是我的剧本:

#!/usr/bin/perl
use strict;
use warnings;

my @id=`ssh expert\@x.x.x.x`;
print"@id";

2 个答案:

答案 0 :(得分:2)

这就是你要求你的程序做的事情。这一行

capture
  • 启动一个运行带有给定参数的ssh的新子进程
  • 退出该子流程并返回ssh
  • 的任何文本输出

您可能需要在连接后与远程系统进行交互,因此您需要 perl 进程连接到远程系统,或者您需要能够收听并与之通信哈希在退出之前连接的子进程

第一个是迄今为止最简单的解决方案。如果您使用Net::OpenSSH模块并阅读其文档,那么您将看到可以通过创建对象来打开连接。然后,您可以使用该对象的<div class="tutorial_list"> <!-- LOAD YOUR PHP BLOG DATA --> <div class="loading"><img src="fb-load.gif"/></div> <!-- More Button here $ID values is a last post id value. --> <div id="show_more<?php echo $ID; ?>" class="morebox"> <a href="#" class="show_more" id="<?php echo $ID; ?>">more</a> </div> </div> <script> $(document).on('click', '.show_more', function() { { var ID = $(this).attr("id"); if (ID) { $('.morebox').hide(); $('.loding').show(); $.ajax({ type: "POST", url: "ajax_more.php", data: "lastpost=" + ID, cache: false, success: function(html) { $('.loading').hide(); $('.tutorial_list').append(html); $("#show_more" + ID).remove(); // removing old more button } }); } else { $(".morebox").html('The End'); // no results } return false; }); </script> 方法

发送命令并检索输出

答案 1 :(得分:1)

我建议不要在perl代码中使用system ssh命令,原因如下:

  1. 使解析输出变得困难
  2. 错误处理变得困难
  3. 编程灵活性较低
  4. 相反,使用CPAN库,例如Net :: SSH :: Perl,用于从Perl代码中激活SSH命令。<​​/ p>

    使用此模块打开shell很简单,如下所述:

    $ssh->shell
    
    Opens up an interactive shell on the remote machine and connects it to your STDIN. This is most effective when used with a pseudo tty; otherwise you won't get a command line prompt, and it won't look much like a shell. For this reason--unless you've specifically declined one--a pty will be requested from the remote machine, even if you haven't set the use_pty argument to new (described above).
    
    This is really only useful in an interactive program.
    
    In addition, you'll probably want to set your terminal to raw input before calling this method. This lets Net::SSH::Perl process each character and send it off to the remote machine, as you type it.
    
    To do so, use Term::ReadKey in your program:
    
        use Term::ReadKey;
        ReadMode('raw');
        $ssh->shell;
        ReadMode('restore');
    

    下面是一个快速示例,演示了使用相同模块触发和解析命令输出是多么容易:

      use Net::SSH::Perl;
        my $ssh = Net::SSH::Perl->new($host);
        $ssh->login($user, $pass);
        my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
    

    链接到Net :: SSH :: Perl cpan文档:http://search.cpan.org/~schwigon/Net-SSH-Perl-1.42/lib/Net/SSH/Perl.pm

    我更喜欢使用的另一个模块是Net :: OpenSSH:http://search.cpan.org/~salva/Net-OpenSSH-0.70/lib/Net/OpenSSH.pm

    use Net::OpenSSH;
    
      my $ssh = Net::OpenSSH->new($host);
      $ssh->error and
        die "Couldn't establish SSH connection: ". $ssh->error;
    
      $ssh->system("ls /tmp") or
        die "remote command failed: " . $ssh->error;
    
      my @ls = $ssh->capture("ls");
      $ssh->error and
        die "remote ls command failed: " . $ssh->error;
    
      my ($out, $err) = $ssh->capture2("find /root");
      $ssh->error and
        die "remote find command failed: " . $ssh->error;
    
      my ($rin, $pid) = $ssh->pipe_in("cat >/tmp/foo") or
        die "pipe_in method failed: " . $ssh->error;
    
      print $rin "hello\n";
      close $rin;