2012-03-21 27 views
10

我有一臺接受telnet連接進行管理的服務器。我想念命令歷史,所以我想讓我的telnet會話支持它。我的問題:如何在telnet客戶端上實現命令歷史記錄? (向上/向下箭頭)

1)我必須在服務器端實現它,所以服務器會發送過去的命令給客戶端,然後客戶端可以重新執行?

2)有沒有在遠程登錄客戶端實現此功能(而不是搞亂服務器)?

如果答案是1),那麼我需要知道如何捕獲和發送我的telnet會話上的上下箭頭鍵,而不必按Enter鍵。

回答

33

這不是服務器問題。只需使用rlwrap與您的telnet客戶端。它給你readline無需編程。

$ rlwrap telnet server port 

(我實際使用nc而不是telnet,因爲它更容易使用和更強勁)

+0

10000000謝謝! – chrisapotek 2012-03-21 17:18:04

+1

這工作就像一個魅力! – chrisapotek 2012-03-21 17:30:14

+0

這裏有一個問題:^]退出不起作用。 :( – chrisapotek 2012-09-20 15:44:52

5

使用socat:

socat readline,history=$HOME/.telnet_history TCP:host:23 
+1

注意:如果省略'history',則使用文件'$ HOME/.history' – Lekensteyn 2013-12-27 16:51:56

+1

不幸的是,在最新的Ubuntu/Debian上無法運行, [由於許可問題](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=677371)。 – 2014-07-06 17:46:50

1

我假設這是你的服務使用Perl編寫,基於您的標籤。

您可以使用CPAN中的Term :: ReadLine模塊來做你想做的事。從CPAN網站,這裏有一個基本的例子:

use Term::ReadLine; 
    my $term = Term::ReadLine->new('My Management Service'); 
    my $prompt = "Enter your management command: "; 
    my $OUT = $term->OUT || \*STDOUT; 
    while (defined ($_ = $term->readline($prompt))) { 
     my $res = eval($_); 
     warn [email protected] if [email protected]; 
     print $OUT $res, "\n" unless [email protected]; 
     $term->addhistory($_) if /\S/; 
    } 
相關問題