关于expect perl的问题
我是expect perl 编程的新手,必须编写一些脚本来自动执行某些任务。基本上我需要远程登录到 unix 机器,发出一些 unix 命令并检查一些输出。 我已经编写了一个 telnet 的小脚本,但我对如何检查 unix 命令的状态有疑问。
#!/usr/bin/perl
use Expect;
$timeout=10;
my $exp = Expect->spawn("telnet test-b -l regress")
or die "Cannot spawn telnet: $!\n";;
my $spawn_ok;
$foo1=$exp->expect($timeout, 'Password:');
print "######Received Password prompt\n";
$exp->send("MaRtInI\n");
print "######Sent password\n";
$foo1=$exp->expect($timeout, '%');
print "######Received root prompt\n";
$exp->send("cd /var/tmp");
我在这里对如何检查 cd 命令是否真正成功表示怀疑,因为如果我给出 cd /var/temp12 ,而不是 cd /var/tmp (这会-不存在),我看到了相同的结果。如何检查我通过 cd 发送的 unix 命令是否确实成功?
I am new in expect perl programming and have to write some scripts to automate some tasks. Basically I need to telnet to a unix machine, issue some unix commands and check some outputs.
I have written a small script to telnet but I have a doubt on how to check the status of the unix commands.
#!/usr/bin/perl
use Expect;
$timeout=10;
my $exp = Expect->spawn("telnet test-b -l regress")
or die "Cannot spawn telnet: $!\n";;
my $spawn_ok;
$foo1=$exp->expect($timeout, 'Password:');
print "######Received Password prompt\n";
$exp->send("MaRtInI\n");
print "######Sent password\n";
$foo1=$exp->expect($timeout, '%');
print "######Received root prompt\n";
$exp->send("cd /var/tmp");
I have doubt here on how to check whether the cd command is actually successful, because instead of cd /var/tmp
if I give cd /var/temp12
(which does-not exist), I am seeing the same result. How can I check whether to unix command which I have sent through cd is actually successful ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
echo $?
在服务器端找出最后一个命令的退出状态,然后通过qr/^\d+/m
进行处理(m 表示“字符串或换行符开头之后”)。我编写了一个示例脚本,它确实生成了预期的 shell,并尝试 chdir 到作为参数给出的路径,例如 perl myscript.pl /tmp 或 perl myscript.pl /noexist
PS 我想知道如何在 except 中获取(捕获)(来自)(我的正则表达式)。
[ qr/(\d+)/, sub { ...($1) } ]
对我不起作用。Use
echo $?
to find out last command exit status on the server-side, and then process it viaqr/^\d+/m
(m means "afrer beginning of string or newline").I've written a sample script that does spawns an expect'ed shell and tries to chdir to the path given as argument, e.g.
perl myscript.pl /tmp
orperl myscript.pl /noexist
P.S. I wonder how can I get (captures) (from) (my regexp) in Except.
[ qr/(\d+)/, sub { ...($1) } ]
didn't work for me.