最小的 Perl/Gtk2 远程登录客户端

Posted

技术标签:

【中文标题】最小的 Perl/Gtk2 远程登录客户端【英文标题】:Minimal Perl/Gtk2 telnet client 【发布时间】:2018-09-17 06:33:53 【问题描述】:

这是一个简单的 telnet 客户端,使用 Gtk2 和 Perl 的 Net::Telnet。

显示传入的文本,但键入的命令无法发送到服务器。服务器没有收到键入的命令,或者我们没有收到服务器的响应。

处理键入命令的代码非常简单:

$cmd = $entry->get_text();       # Gtk2::Entry

$telnetObj->put(                 # Net::Telnet
    String => $cmd,
    Telnetmode => 0,        
);

不过,这个脚本完全不起作用。任何想法为什么?

#!/usr/bin/perl
# Minimal telnet client

use strict;
use diagnostics;
use warnings;

use Glib qw(TRUE FALSE);
use Gtk2 '-init';
use Net::Telnet;

# Connect to any old MUD
my $host = 'dead-souls.net';
my $port = 8000;
# The Net::Telnet object
my $telnetObj;

# Open a Gtk2 window, with a Gtk2::TextView to display incoming text, and a Gtk2::Entry for sending
#   commands    
my $window = Gtk2::Window->new('toplevel');
$window->set_title('Minimal telnet client');
$window->set_position('center');
$window->set_default_size(600, 400);
$window->signal_connect('delete-event' => sub 

    Gtk2->main_quit();
    exit;
);

my $vPaned = Gtk2::VPaned->new();
$window->add($vPaned);
$vPaned->set_position(350);

my $scrollWin = Gtk2::ScrolledWindow->new(undef, undef);
$vPaned->add1($scrollWin);
$scrollWin->set_policy('automatic', 'automatic');     
$scrollWin->set_border_width(0);

my $textView = Gtk2::TextView->new;
$scrollWin->add_with_viewport($textView);
$textView->can_focus(FALSE);
$textView->set_wrap_mode('word-char');
$textView->set_justification('left');
my $buffer = $textView->get_buffer();

my $entry = Gtk2::Entry->new();
$vPaned->add2($entry);
$entry->signal_connect(activate => sub 

    # When the user enters a command, empty the entry box and (if the connection is open), send the 
    #   command to the host, and display the command in the textview
    my $cmd = $entry->get_text();
    $entry->set_text('');

    if ($telnetObj) 

        $telnetObj->put(
            String => $cmd,
            Telnetmode => 0,        
        );

        my $iter = $buffer->get_end_iter();
        $textView->get_buffer->insert_with_tags_by_name($iter, $cmd . "\n");
        Gtk2->main_iteration() while Gtk2->events_pending();
    
);  

$window->show_all();   

# Set up a main loop
my $id = Glib::Timeout->add(100, sub &mainLoop($telnetObj, $textView, $buffer) );
if (! $id) 

    exit;


# Open the connection
$telnetObj = Net::Telnet->new(
    Timeout => 15,
);

$telnetObj->open(
    Host        => $host,
    Port        => $port,
    Errmode     => sub  &disconnected(); ,
);

# Start Gtk2's main loop
Gtk2->main();

# ##################################################################################################

sub mainLoop 

    my ($telnetObj, $textView, $buffer) = @_;

    my $text = $telnetObj->get(
        Errmode => sub  ,
        Timeout => 0,
    );

    if ($text) 

        # We've received some text from the host. Display it in the textview
        my $iter = $buffer->get_end_iter();
        $buffer->insert_with_tags_by_name($iter, $text);
        Gtk2->main_iteration() while Gtk2->events_pending();
    

    return 1;


sub disconnected 
    print "Disconnected\.\n";
    exit;


sub connectError 
    print "Connection error\.\n";
    exit;

【问题讨论】:

你试过其他主机吗? 是的,我已经尝试了至少十几个主机,并且我还尝试了将这些主机与其他 telnet 客户端一起使用,以确保这些主机正常运行。 我用 cmd(String => $cmd); 替换了方法 put(...) 并进行了对话,创建了用户名和密码等。但是有一个严重的“故障”:我发送的每个命令都先超时,并且直到那时我才看到回应。一切都显示在窗口中。我不知道为什么它会超时——也许你应该等待它们返回的某个特定序列? (这可能是put 失败的原因?) 【参考方案1】:

根据上述建议,至少有三种可行的解决方案来更换故障:

    $telnetObj->put(
        String => $cmd,
        Telnetmode => 0,        
    );

他们是:

# This works:
$telnetObj->put(
    String => $cmd . "\n",
    Telnetmode => 1,        
);

# This also works:
$telnetObj->cmd(
    String => $cmd,
    Timeout => 0,
);

# This also works:
$telnetObj->print($cmd);

【讨论】:

以上是关于最小的 Perl/Gtk2 远程登录客户端的主要内容,如果未能解决你的问题,请参考以下文章

windows远程登录Linux

Linux高级命令10:远程登录远程拷贝命令

使用 SSH-Key 登录远程服务器

SSH 之远程登录

如何允许root用户远程登录?

如何允许root用户远程登录?