2010-08-27 62 views
1

有沒有辦法允許perl發起telnet會話並以編程方式向該telnet會話發出命令?telnet內聯perl?

我最初嘗試一個笨方法:

commands.pl:

sleep(1); 
print $command1; 
sleep(1); 
print $command2; 

然後

> perl commands.pl | telnet www.host.com port 

這是行不通的。

回答

8

有一個Net::Telnet模塊。

use Net::Telnet(); 
$t = new Net::Telnet (Timeout => 10, 
         Prompt => '/bash\$ $/'); 
$t->open("sparky"); 
$t->login($username, $passwd); 
@lines = $t->cmd("who"); 
print @lines; 

(從該網頁拍攝的實施例。)