我正在尝试通过 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");
错误:
"WARNING: terminal is not fully functional"
最佳答案
虽然 Net::SSH 是一个优雅的模块,但我建议您使用默认可用的系统工具。
您有 ssh 可用并且使用 ssh-keys 更安全,尤其是因为您当前在脚本中显示密码。如果您不确定如何设置 ssh-keys,请告诉我,我会将其添加到答案中。
实际上,您的整个脚本可以纯粹是:
use strict;
use warnings;
my $apache_status = `ssh username\@servername systemctl status apache2`;
print $apache_status;
关于apache - 在 perl php 中通过 ssh 命令重新启动 apache 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46233482/