+ -
当前位置:首页 → 问答吧 → perl expect远程执行不了命令,请帮忙看看,谢谢了

perl expect远程执行不了命令,请帮忙看看,谢谢了

时间:2011-01-14

来源:互联网

  1. foreach $remoteip(@ip)
  2. {
  3.         print @ip;
  4.         chomp;
  5.         $exp = Expect->spawn($cmd,$remoteip) or die "Can't spawn $cmd! $!";
  6.         $exp->log_file("output.log","w");
  7.         $exp->send($usrname."\n") if $exp->expect($timeout,-re=>'[Ll]ogin');
  8.         $exp->send("1111111\n") if $exp->expect($timeout,-re=>'word');
  9.         if ($exp->expect($timeout,-re=>'$usrname>'))
  10.         {
  11.                 $exp->send("/bin/date\r\n");
  12.                 print "hello \n";
  13.         }
  14. }
复制代码
*****@S7-E-JK-SZX:/home/*****/perl> more output.log
Trying 00.000.000.00...
Connected to 00.000.000.00.
Escape character is '^]'.
Welcome to SUSE Linux Enterprise Server 10 SP2 (i586) - Kernel 2.6.16.60-0.21-bigsmp (4).

linux-bmlp login: ****
Password:
Last login: Fri Jan 14 18:00:14 CST 2011 from 00.000.000.00 on pts/4
hwoyh@linux-bmlp:/home/*****>

已经登陆成功了,可以打印出hello了,这何还不可以执行命令打印日期的。登陆成功后该如何执行命令?
请帮忙看看,谢谢

作者: oyhuan   发布时间: 2011-01-14

我想应该在
  1. $exp->send("/bin/date\r\n");
复制代码
中不需要 “\r\n"吧,

作者: climby   发布时间: 2011-01-14



QUOTE:
我想应该在中不需要 “\r\n"吧,
climby 发表于 2011-01-14 18:10



"\r\n","\r","\n",和不用这二个都试过都不行。

作者: oyhuan   发布时间: 2011-01-14

回复 oyhuan


那你吧Expect的debug 打开,
  1. $exp->debug(2);
复制代码
看看debug提示什么信息。

作者: climby   发布时间: 2011-01-14

本帖最后由 oyhuan 于 2011-01-14 18:47 编辑


QUOTE:
回复  oyhuan


那你吧Expect的debug 打开,看看debug提示什么信息。
climby 发表于 2011-01-14 18:36

  1.         if ($exp->expect($timeout,-re=>'*****>'))
  2.         {
  3.                 $exp->debug(2);
  4.                 $exp->send("/bin/date\n");
  5.                 $exp->debug(2);
  6.                 print "hello \n";
  7.         }
复制代码
DEBUG如下,不知道DEBUG放在哪里,我放了二个地方了
  1.     Closing spawn id(6).
  2.         Expect::hard_close('Expect=GLOB(0x837841c)') called at /usr/lib/perl5/vendor_perl/5.8.3/Expect.pm line 1575
  3.         Expect::DESTROY('Expect=GLOB(0x837841c)') called at ./expect.pl line 0
  4.         eval {...} called at ./expect.pl line 0
  5. spawn id(6) closed.
  6. Pid 13844 of spawn id(6) terminated, Status: 0x01
复制代码

作者: oyhuan   发布时间: 2011-01-14

热门下载

更多