【问题标题】:Intermittently First Iteration fail but second iteration pass间歇性第一次迭代失败但第二次迭代通过
【发布时间】:2023-04-02 05:59:02
【问题描述】:

相同的命令在第一次迭代中失败,但在第二次迭代中通过。有时它会在两个迭代中传递。 $sshNet::SSH::Expect 的对象。 知道这种奇怪的行为吗?

$ssh = Net::SSH::Expect->new(
                    host => "$host",
                    password => "$pass",
                    user => "$id",
                    raw_pty => 1,
                    timeout => 50
                    );

代码片段:

&Test("Create exports on Sonas------ No Kerberos");
$no_of_exports=2;
my $fs = "/ibm/gpfs0/";
while($no_of_exports)
{
    my $share_name = &get_share_name();
    my $path="$fs" . "$share_name";

    $cmd="ssh mgmt001st001 mkexport $share_name $path --nfs 'client002\\\\(rw,no_root_squash\\)'";
    print "CMD: $cmd";
    $out=$ssh->exec("$cmd");
    print $out;
    --$no_of_exports;
}

输出:

Test 02 : Create exports on *Sonas*------ No Kerberos

CMD: ssh mgmt001st001 mkexport kas41535108 /ibm/gpfs0/kas41535108 --nfs 'client002\\     (rw,no_root_squash\)'

--nfs 'client002\\(xport kas41535108 /ibm/gpfs0/kas41535108 
> rw,no_root_squash\)'
 bash: -c: line 0: syntax error near unexpected token `('
 bash: -c: line 0: `mkexport kas41535108 /ibm/gpfs0/kas41535108 --nfs client002\\('
 [root@ganesha36 ~]# 
 [root@ganesha36 ~]# 
CMD: ssh mgmt001st001 mkexport kas219760489 /ibm/gpfs0/kas219760489 --nfs 'client002\\  (rw,no_root_squash\)'
89 --nfs 'client002\xport kas219760489 /ibm/gpfs0/kas2197604 
> \(rw,no_root_squash\)'
EFSSG0019I The export kas219760489 has been successfully created.
EFSSG1000I The command completed successfully.

【问题讨论】:

  • 您是否在 raw_pty 模式下实例化您的 Net::SSH::Expect 对象?
  • 是的,现在我的问题中也添加了该部分。
  • 为什么括号前有\\\\ ?除非我遗漏了什么,否则应该是 \\ (没有空格)。
  • 需要多层转义...查看stackoverflow.com/questions/11616454/…了解详情
  • 我见过 Net::SSH::Expect 没有完全关闭以前使用的会话,然后在下次使用时神秘地失败了。您必须记住在退出脚本之前完全关闭它们。如果您在字符转义/翻译方面遇到问题,那么我建议您将您的命令放入远端的脚本中,然后期望运行该脚本的更小/更简单的脚本。

标签: perl bash ssh


【解决方案1】:

错误信息就在那里。 Bash 令人窒息,因为( 令牌出乎意料。您需要在有四个反斜杠的地方再添加两个反斜杠

$cmd="ssh mgmt001st001 mkexport $share_name $path --nfs 'client002\\\\(rw,no_root_squash\\)'";

变成

$cmd="ssh mgmt001st001 mkexport $share_name $path --nfs 'client002\\\\\\(rw,no_root_squash\\)'";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2016-04-13
    • 1970-01-01
    相关资源
    最近更新 更多