面对使用perl编写代码的问题

时间:2016-08-23 03:24:15

标签: linux perl

我想运行一个连接到远程主机并执行某些操作的命令,而我尝试运行下面的命令需要执行操作。

The authenticity of host 'test.example.com' can't be established.
RSA key fingerprint is 45:57:9e:d6:6f:b5:cc:c6:00:3f:90:ec:a1:9d:4e:5f.
Are you sure you want to continue connecting (yes/no)?
Please type 'yes' or 'no': 

它还要求输入密码

tes@test.example.com's password:

我写了一个perl脚本如下:

sub user_expect()
{
    print "==== Now executing the following command:\n";
    print "$cmd\n";
    $exp = new Expect();
    $exp->raw_pty(1);
    $exp->log_file("output2_user", "w");
    $exp->debug($debug);
    $exp->spawn("$cmd");
    $exp->expect($timeout,'-re', '\?\s$'); # Authenticity question (yes/no)
    $exp->send("yes\n");
    $exp->expect($timeout,'-re', ':\s$'); # In case on console am expected to type yes/no - Please type 'yes' or 'no': 
    $exp->send("yes\n");
    $exp->expect($timeout,'-re', 'word:$'); # enter password
    $exp->send("xyz\n");
    $exp->expect(500,'-re', 'mailbox deleted successfully\.$');
    $exp->hard_close();
    return 1;
}

我需要一个能够回答控制台上提出的问题的代码,如下所示,还有密码控制台

The authenticity of host 'test.example.com' can't be established.
RSA key fingerprint is 45:57:9e:d6:6f:b5:cc:c6:00:3f:90:ec:a1:9d:4e:5f.
Are you sure you want to continue connecting (yes/no)?
Please type 'yes' or 'no': 

我不确定我对脚本所犯的错误,有人可以帮忙吗?

问题是 - 我认为"是"不需要为下面的查询键入,因为查询"请键入' yes'或者'不':"在控制台上显示。然后我的脚本停在那里,不进行密码提示。

您确定要继续连接(是/否)吗?

2 个答案:

答案 0 :(得分:0)

您可以尝试使用Net::SSH::Perl而不是使用Expect模块。你需要设置strict_host_key_checking =>如果你想跳过SSH密钥检查,请“否”......

答案 1 :(得分:0)

我改变了下面的代码,它成功了:

502 Bad Gateway