通过PHP启动远程服务
基本上,我想通过PHP通过PHP启动远程服务,php,ubuntu,button,lamp,Php,Ubuntu,Button,Lamp,基本上,我想通过PHPssh2\u connect函数将不同的命令传递给服务器。但我需要使用不同的按钮来传递不同的命令,但无法完成 下面是我的按钮代码: <?php $connection = ssh2_connect('ipaddress', 22); ssh2_auth_password($connection, 'root', 'password'); if (isset($_POST['button'])) ssh2_exec($stream = ssh2_exec($conne
ssh2\u connect
函数将不同的命令传递给服务器。但我需要使用不同的按钮来传递不同的命令,但无法完成
下面是我的按钮代码:
<?php
$connection = ssh2_connect('ipaddress', 22);
ssh2_auth_password($connection, 'root', 'password');
if (isset($_POST['button'])) ssh2_exec($stream = ssh2_exec($connection, 'systemctl stop apache2'));
?>
<form action="" method="post">
<button type="submit" name="button">Apache2</button
Apache2有几件事:
- 您的代码没有完成,它缺少HTML的其余部分
- 您的嵌套
ssh2\u exec($stream=ssh2\u exec(
),很可能是输入错误
- 你停止的apache没有启动它
以下内容将帮助您开始,希望对您有所帮助
<?php
class ssh {
public function __construct($ip, $user, $pass, $port = 22)
{
// connect
$this->connection = ssh2_connect($ip, $port);
ssh2_auth_password($this->connection, $user, $pass);
}
public function exec($cmd = null)
{
// define streams
$this->stream = ssh2_exec($this->connection, $cmd);
$this->err_stream = ssh2_fetch_stream($this->stream, SSH2_STREAM_STDERR);
// get streams
$this->output = stream_get_contents($this->stream);
$this->error = stream_get_contents($this->err_stream);
return $this;
}
}
// define cmds
$commands = [
'stop_apache' => [
'description' => 'Stop Apache2',
'cmd' => 'systemctl stop apache2'
],
'restart_apache' => [
'description' => 'Restart Apache2',
'cmd' => 'systemctl restart apache2'
],
'start_apache' => [
'description' => 'Start Apache2',
'cmd' => 'systemctl start apache2'
]
];
// handle post
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$error = [];
$result = '';
// validate input
if (empty($_POST['service'])) {
$error = [
'service' => 'Service type required!'
];
} elseif (!array_key_exists($_POST['service'], $commands)) {
$error = [
'service' => 'Invalid Service!'
];
}
// run cmd - $result->output will have stdout, $result->error will have stderr
if (empty($error)) {
$ssh = new ssh('127.0.0.1', 'root', 'password');
$result = $ssh->exec($commands[$_POST['service']]['cmd']);
}
}
?>
<form action="" method="post">
<?php if (!empty($error)): ?>
<h3>Error</h3>
<pre><?= print_r($error, true) ?></pre>
<?php endif ?>
<?php foreach ($commands as $key => $command): ?>
<button type="submit" name="service" value="<?= $key ?>"><?= $command['description'] ?></button>
<?php endforeach ?>
</form>
<?php if (!empty($result)): ?>
<pre><?= print_r($result, true) ?></pre>
<?php endif ?>
错误
完美,它正在工作…我已经使用上面的code.np添加了更多的服务,很高兴它起到了作用和帮助。感谢接受。如果我想显示通过该代码执行的命令的输出,我该怎么办?正如代码中所述,$result->output
将有stdout,$result->error
将有stderr,不要混淆它虽然您需要使用管道和WebSocket来实现这一点。对。实际上,它仍然没有显示输出。我使用的是PHP5.6,这不是问题吗?但是当我在PHP7上测试时,它显示了输出。
<?php
class ssh {
public function __construct($ip, $user, $pass, $port = 22)
{
// connect
$this->connection = ssh2_connect($ip, $port);
ssh2_auth_password($this->connection, $user, $pass);
}
public function exec($cmd = null)
{
// define streams
$this->stream = ssh2_exec($this->connection, $cmd);
$this->err_stream = ssh2_fetch_stream($this->stream, SSH2_STREAM_STDERR);
// get streams
$this->output = stream_get_contents($this->stream);
$this->error = stream_get_contents($this->err_stream);
return $this;
}
}
// define cmds
$commands = [
'stop_apache' => [
'description' => 'Stop Apache2',
'cmd' => 'systemctl stop apache2'
],
'restart_apache' => [
'description' => 'Restart Apache2',
'cmd' => 'systemctl restart apache2'
],
'start_apache' => [
'description' => 'Start Apache2',
'cmd' => 'systemctl start apache2'
]
];
// handle post
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$error = [];
$result = '';
// validate input
if (empty($_POST['service'])) {
$error = [
'service' => 'Service type required!'
];
} elseif (!array_key_exists($_POST['service'], $commands)) {
$error = [
'service' => 'Invalid Service!'
];
}
// run cmd - $result->output will have stdout, $result->error will have stderr
if (empty($error)) {
$ssh = new ssh('127.0.0.1', 'root', 'password');
$result = $ssh->exec($commands[$_POST['service']]['cmd']);
}
}
?>
<form action="" method="post">
<?php if (!empty($error)): ?>
<h3>Error</h3>
<pre><?= print_r($error, true) ?></pre>
<?php endif ?>
<?php foreach ($commands as $key => $command): ?>
<button type="submit" name="service" value="<?= $key ?>"><?= $command['description'] ?></button>
<?php endforeach ?>
</form>
<?php if (!empty($result)): ?>
<pre><?= print_r($result, true) ?></pre>
<?php endif ?>