关于expect perl的问题

发布于 2024-11-30 22:07:35 字数 687 浏览 0 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2024-12-07 22:07:35

使用echo $?在服务器端找出最后一个命令的退出状态,然后通过qr/^\d+/m进行处理(m 表示“字符串或换行符开头之后”)。

我编写了一个示例脚本,它确实生成了预期的 shell,并尝试 chdir 到作为参数给出的路径,例如 perl myscript.pl /tmp 或 perl myscript.pl /noexist

#!/usr/bin/perl -w
use strict;

use Expect;

my $exp = Expect->spawn("/bin/bash")
    or die;

$exp->send ("cd ".(shift)."; echo \$?\n");
$exp->expect(2, # hardcoded timeout
    [ qr/^0/m => sub { print "chd\n" } ], # regex => sub
    [qr/^[1-9]/m => sub {print "no chd\n"}] 
);

PS 我想知道如何在 except 中获取(捕获)(来自)(我的正则表达式)。 [ qr/(\d+)/, sub { ...($1) } ] 对我不起作用。

Use echo $? to find out last command exit status on the server-side, and then process it via qr/^\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 or perl myscript.pl /noexist

#!/usr/bin/perl -w
use strict;

use Expect;

my $exp = Expect->spawn("/bin/bash")
    or die;

$exp->send ("cd ".(shift)."; echo \$?\n");
$exp->expect(2, # hardcoded timeout
    [ qr/^0/m => sub { print "chd\n" } ], # regex => sub
    [qr/^[1-9]/m => sub {print "no chd\n"}] 
);

P.S. I wonder how can I get (captures) (from) (my regexp) in Except. [ qr/(\d+)/, sub { ...($1) } ] didn't work for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文