在perl php中通过ssh命令重启apache服务器

时间:2017-09-15 06:54:32

标签: apache perl ssh

我试图通过perl-php中的ssh命令重启Apache服务器。

下面是我试过的代码。它通过PuTTY工作。虽然如果我通过浏览器运行,它不会在行上运行:$output = $ssh->exec("systemctl status apache2");

use Net::SSH::Expect;
my $ssh = Net::SSH::Expect->new (
            host => "xx.xx.xx.xx", 
            password=> 'adsd#21', 
            user => 'root', 
            raw_pty => 1
        );
print("\n");
my $output = $ssh->login();
print($output);
if ($output !~ /Welcome/) {
    die "Login has failed. Login output was $login_output";
}


$output = $ssh->exec("systemctl status apache2");

错误:

  

“警告:终端功能不全”

1 个答案:

答案 0 :(得分:1)

虽然Net :: SSH是一个优雅的模块,但我建议您默认使用系统工具。

你有ssh可用并且使用ssh-keys更安全,特别是因为你当前在脚本中显示密码。如果您不确定如何设置ssh-keys,请告诉我,我会将其添加到答案中。

有效地,您的整个脚本可以纯粹是:

use strict;
use warnings;

my $apache_status = `ssh username\@servername systemctl status apache2`;
   print $apache_status;