免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4079 | 回复: 9
打印 上一主题 下一主题

perl telnet 模块的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-31 12:03 |只看该作者 |倒序浏览
#!/opt/dc_perl

use Net::Telnet;

print "\nUsage: get_alarm.pl remote_host_ip port User_Name Password \n\n";

my $remote_host_ip = $ARGV[0];
my $port = $ARGV[1] ;
my $username = $ARGV[2] ;
my $password = $ARGV[3] ;
my $recv_file_name = $ARGV[4] ;

my $first_cmd ="cd /tellin/oams/oams_run/log";
my $second_cmd = "ps -ef|grep -v grep|grep tail|awk '{print $2}'|xargs kill -9";
my $third_cmd = "tail -f /tellin/oams/oams_run/log/oams_warninglog_usr.log";


print "ipremote_host_ip\n";
print "portport\n";
print "loginusername\n";
print "passwdpassword\n";

my $telnet_hd=new Net::Telnet (Timeout => 3600);
print "now telnet......\n";
#$telnet_hd->open(Host=>$remote_host_ip);
$telnet_hd->open(Host=>$remote_host_ip,Port=>$port);
print "now open ......\n";
$telnet_hd->login(login=>$username,Password=>$password);
print "now login......\n";

执行perl程序的时候提示如下,哪位帮忙看看是啥问题啊?
Usage: get_alarm.pl remote_host_ip port User_Name Password
ip:192.168.1.149
port:23
nameams
passwdams
now telnet......
now open ......

手工telnet服务器的时候
telnet 192.168.1.149
Trying 192.168.1.149...
Connected to 192,168.1.149.
Escape character is '^]'.
Welcome to SUSE Linux Enterprise Server 10 SP1 (x86_64) - Kernel 2.6.16.46-0.12-smp (2).

linux-461u login:

论坛徽章:
0
2 [报告]
发表于 2010-12-31 14:15 |只看该作者
你的prompt 在哪?

论坛徽章:
0
3 [报告]
发表于 2010-12-31 15:33 |只看该作者
#!c:/Perl

use Net::Telnet;

print "\nUsage: get_alarm.pl remote_host_ip port User_Name Password \n\n";

my $remote_host_ip = $ARGV[0];
my $port = $ARGV[1] ;
my $username = $ARGV[2] ;
my $password = $ARGV[3] ;
my $recv_file_name = $ARGV[4] ;

my $first_cmd ="cd /var/log/report";
my $second_cmd = "ps -ef|grep -v grep|grep tail|awk '{print $2}'|xargs kill -9";
my $third_cmd = "tail -f Alarm.log";


print "ipremote_host_ip\n";
print "portport\n";
print "nameusername\n";
print "passwdpassword\n";

my $telnet_hd=new Net::Telnet (Timeout => 10);
print "now telnet......\n";
#$telnet_hd->open("$remote_host_ip";
print "ceshi....\n";
$telnet_hd->open(Host=>$remote_host_ip,Port=>$port);
print "now open ......\n";
$telnet_hd->login(Name=>$username,PASSWORD=>$password);
print "now login......\n";

    if(!$telnet_hd)
    {
       print "Login Failed\n";
       $telnet_hd->close;
       exit 0;
     }

print "first cmd is $first_cmd\n";
$telnet_hd->print("$first_cmd";
print "send first cmd\n";
my ($cmd_result_msg,$match)= $telnet_hd->waitfor(Match=>"/report/",Timeout=>600000);

`sleep 3` ;
print "second cmd is $second_cmd\n";
$telnet_hd->print("$second_cmd";
print "send second cmd\n";
my ($cmd_result_msg,$match)= $telnet_hd->waitfor(Match=>"/report/",Timeout=>600000);

`sleep 3` ;
$telnet_hd->print("$third_cmd";
print "the third cmdthird_cmd\n";

while(my $line = $telnet_hd->getline())

{
        syswrite(stdout,$line);
}

print "begin quit\n";
$telnet_hd->close;

论坛徽章:
0
4 [报告]
发表于 2010-12-31 15:36 |只看该作者
perl -d get_alarm.pl 92.168.1.149 23  oams  oams

Loading DB routines from perl5db.pl version 1.28
Editor support available.

Enter h or `h h' for help, or `perldoc perldebug' for more help.

main:get_alarm.pl:5): print "\nUsage: get_alarm.pl remote_host_ip port User_Na
me Password \n\n";

  DB<1> n

Usage: get_alarm.pl remote_host_ip port User_Name Password

main:get_alarm.pl:7): my $remote_host_ip = $ARGV[0];

  DB<1> n
main:get_alarm.pl:: my $port = $ARGV[1] ;

  DB<1> n
main:get_alarm.pl:9): my $username = $ARGV[2] ;

  DB<1> n
main:get_alarm.pl:10):        my $password = $ARGV[3] ;

  DB<1> n
main:get_alarm.pl:11):        my $recv_file_name = $ARGV[4] ;

  DB<1> n
main:get_alarm.pl:13):        my $first_cmd ="cd /var/log/report";

  DB<1> n
main:get_alarm.pl:14):        my $second_cmd = "ps -ef|grep -v grep|grep tail|
awk '{print $2}'|xargs kill -9";

  DB<1> n
main:get_alarm.pl:15):        my $third_cmd = "tail -f Alarm.log";

  DB<1> n
main:get_alarm.pl:1:        print "ipremote_host_ip\n";

  DB<1> n
ip:192。168.1.149
main::(get_alarm.pl:19):        print "portport\n";

  DB<1> n
port:23
main::(get_alarm.pl:20):        print "nameusername\n";

  DB<1> n
nameams
main::(get_alarm.pl:21):        print "passwdpassword\n";

  DB<1> n
passwdams
main::(get_alarm.pl:23):        my $telnet_hd=new Net::Telnet (Timeout => 10);

  DB<1> n
main::(get_alarm.pl:24):        print "now telnet......\n";

  DB<1> n
now telnet......
main::(get_alarm.pl:26):        print "ceshi....\n";

  DB<1> n
ceshi....
main::(get_alarm.pl:27):        $telnet_hd->open(Host=>$remote_host_ip,Port=>$po
rt);

  DB<1> n
main::(get_alarm.pl:2:        print "now open ......\n";

  DB<1> n
now open ......
main::(get_alarm.pl:29):        $telnet_hd->login(Name=>$username,PASSWORD=>$pas
sword);

  DB<1> n
timed-out waiting for command prompt at get_alarm.pl line 29
at C:/Perl/site/lib/Net/Telnet.pm line 2036
        Net::Telnet::_croak('Net::Telnet=GLOB(0x1f57dc4)', 'timed-out waiting fo
r command prompt') called at C:/Perl/site/lib/Net/Telnet.pm line 539
        Net::Telnet::error('Net::Telnet=GLOB(0x1f57dc4)', 'timed-out waiting for
command prompt') called at C:/Perl/site/lib/Net/Telnet.pm line 1088
        Net::Telnet::__ANON__[C:/Perl/site/lib/Net/Telnet.pm:1097]('timed-out wa
iting for command prompt') called at C:/Perl/site/lib/Net/Telnet.pm line 1146
        Net::Telnet::login('Net::Telnet=GLOB(0x1f57dc4)', 'Name', 'oams', 'PAS
SWORD', 'oams') called at get_alarm.pl line 29
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.

  DB<1>   这是单步的执行结果  谁知道咋回事

论坛徽章:
0
5 [报告]
发表于 2010-12-31 15:40 |只看该作者
提示很清楚了-- timed-out waiting for command prompt'
看你的代码中,似乎并没有设置prompt的匹配,当然就会产生这样的超时了。建议你看看Net::Telnet中的示例代码。
贴代码最好禁用笑脸符号, 夹杂在中间,代码没法看啊。

论坛徽章:
0
6 [报告]
发表于 2010-12-31 15:49 |只看该作者
prompt 匹配怎么设置啊  我对perl不熟

论坛徽章:
0
7 [报告]
发表于 2010-12-31 15:53 |只看该作者

  1. use Net::Telnet ();
  2.     $t = new Net::Telnet (Timeout => 10,
  3.                           Prompt => '/bash\$ $/');

复制代码
Prompt 需要根据你远程机器上的命令提示符做相应的修改哦!

论坛徽章:
0
8 [报告]
发表于 2010-12-31 16:00 |只看该作者
能不能帮忙发一份相关的例子啊?

论坛徽章:
0
9 [报告]
发表于 2010-12-31 16:02 |只看该作者
需要例子,最好莫过于模块本身提供的了,
到这里看看去吧
http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm

论坛徽章:
0
10 [报告]
发表于 2010-12-31 16:49 |只看该作者
print "ipremote_host_ip\n";
print "portport\n";
print "nameusername\n";
print "passwdpassword\n";

my $telnet_hd=new Net::Telnet (Timeout => 10,
                                             Prompt => '/login:/');
print "now telnet......\n";
#$telnet_hd->open("$remote_host_ip";
$telnet_hd->open(Host=>$remote_host_ip,Port=>$port) or die "Can't open the connection:  $!";
print "now open ......\n";
$telnet_hd->login($username,$password);
print "now login......\n";     

现在提示main:get_alarm.pl:29):        $telnet_hd->login($username,$password);

  DB<2> n
login failed: bad name or password at get_alarm.pl line 29
at C:/Perl/site/lib/Net/Telnet.pm line 2036
        Net::Telnet::_croak('Net::Telnet=GLOB(0x1f57f84)', 'login failed: bad na
me or password') called at C:/Perl/site/lib/Net/Telnet.pm line 539
        Net::Telnet::error('Net::Telnet=GLOB(0x1f57f84)', 'login failed: bad nam
e or password') called at C:/Perl/site/lib/Net/Telnet.pm line 1150
        Net::Telnet::login('Net::Telnet=GLOB(0x1f57f84)', 'nrmdal', 'nrmdal') ca
lled at get_alarm.pl line 29
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.

  DB<2> n
Use `q' to quit or `R' to restart.  `h q' for details.

  DB<2>   谁帮忙看一下啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP