【问题标题】:Using Net::Telnet to ssh into linux使用 Net::Telnet ssh 进入 linux
【发布时间】:2014-05-18 05:59:38
【问题描述】:

我正在尝试使用以下 perl 代码通过 ssh 进入 linux 机器并提取 ls -l 的输出,但它似乎不起作用。 我做错了什么?

#!/usr/bin/perl

use warnings;
use strict;

use Net::Telnet;
use IO::Pty;
use POSIX 'setsid';
use Getopt::Long;

use constant PROMPT => '/[a-z#>]/';

my $host = "192.168.1.121";
my $user = "root";

my $ssh = do_cmd( 'ssh', "-l$user", $host );

my $shell = Net::Telnet->new( Fhopen => $ssh );

$shell->binmode(1);
$shell->cmd( String => 'test', Prompt => '/[a-z]/' );
$shell->waitfor(PROMPT);

my @lines = $shell->cmd( String => 'ls -l', Prompt => '/[a-z]/' );

print @lines;
print "\n";

sub do_cmd {
    my ( $cmd, @args ) = @_;

    my $pty = IO::Pty->new;
    defined( my $child = fork );
    return $pty if $child;
    setsid();

    my $tty = $pty->slave;
    close $pty;
    STDIN->fdopen( $tty, "<" );
    STDOUT->fdopen( $tty, ">" );
    STDERR->fdopen( $tty, ">" );
    close $tty;

    $| = 1;
    exec $cmd, @args;
}

【问题讨论】:

  • 我猜这是来自林肯 D. Stein 十多年前发表的Network Programming with Perl。请检查 cpan metacpan.org/pod/Net::OpenSSH 上的最新 ssh 方法

标签: linux perl ssh


【解决方案1】:

Telnet 和 SSH 是两个不同的程序。您不能将 telnet 客户端用于 ssh 服务器。

【讨论】:

    猜你喜欢
    • 2017-01-16
    • 2021-11-20
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2011-02-23
    • 2011-11-20
    • 2023-03-08
    相关资源
    最近更新 更多