为什么我的 Net::Telnet 程序超时?
我编写了一些小代码来使用 Perl 连接到远程服务器,但观察错误消息
#!/usr/bin/perl -w
use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>60, Errmode=>'die');
$telnet->open('192.168.50.40');
$telnet->waitfor('/login:/');
$telnet->print('queen');
$telnet->waitfor('/password:/');
$telnet->print('kinG!');
$telnet->waitfor('/:/');
$telnet->print('vol >> C:\result.txt');
$telnet->waitfor('/:/');
$telnet->cmd("mkdir vol");
$telnet->print('mkdir vol234');
$telnet->cmd("mkdir vol1");
$telnet->waitfor('/\$ $/i');
$telnet->print('whoamI');
print $output;
但是在运行时我收到以下错误
C:\>perl -c E:\test\net.pl
E:\test\net.pl syntax OK
C:\>perl E:\test\net.pl
command timed-out at E:\test\net.pl line 13
C:\>
在这方面帮助我。我是 Perl 新手。
I'm written small code to connect to remote server using Perl but observing error messages
#!/usr/bin/perl -w
use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>60, Errmode=>'die');
$telnet->open('192.168.50.40');
$telnet->waitfor('/login:/');
$telnet->print('queen');
$telnet->waitfor('/password:/');
$telnet->print('kinG!');
$telnet->waitfor('/:/');
$telnet->print('vol >> C:\result.txt');
$telnet->waitfor('/:/');
$telnet->cmd("mkdir vol");
$telnet->print('mkdir vol234');
$telnet->cmd("mkdir vol1");
$telnet->waitfor('/\$ $/i');
$telnet->print('whoamI');
print $output;
But while running i'm getting following errors
C:\>perl -c E:\test\net.pl
E:\test\net.pl syntax OK
C:\>perl E:\test\net.pl
command timed-out at E:\test\net.pl line 13
C:\>
Help me in this regard. I'm new to Perl.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定 Net::Telnet,但使用“/:/”(带引号)我猜是问题所在。即 /:/ (斜杠内) - 这是正则表达式,但带有引号,它只是应该出现在终端上的字符串(即它等待字符串“/:/” - 斜杠,两个点,斜杠)。
要调试此类程序(如果 Net::Telnet 不回显与远程系统的交互),您可以简单地输入: 在 waitfor() 之前的行中打印“我正在等待登录...\n”
I'm not sure about that Net::Telnet, but using '/:/' (with quotes) I guess is the problem. I.e. /:/ (within slashes) - that's regular expression, but with quotes that's simply string which should appear on terminal (i.e. it waits for string '/:/' - slash, two dots, slash).
To debug such programs (if Net::Telnet doesn't echo interaction with remote system) you can simply put: print "I'm waiting for login...\n" at lines before waitfor()
您为什么不使用 Net::Telnet 的
登录登录方法?当你水平这么低的时候,所有的细节都得自己处理。如果您查看该方法的源代码,您会发现它做了相当多的工作,包括解决 Linux 上登录错误的拼凑。
Why aren't you using Net::Telnet's
login
method to log-in? When you're at such a low level, you have to handle all of the details yourself. If you look in the source for that method, you'll see it doing quite a bit of work, including a kludge to get around a login bug on Linux.