使用网::远程登录ssh到linux的(Using Net::Telnet to ssh into

2019-10-20 03:49发布

我试图使用下面的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;
}

Answer 1:

Telnet和SSH是两个不同的程序。 你不能使用telnet客户端SSH服务器。



文章来源: Using Net::Telnet to ssh into linux
标签: linux perl ssh