通过PHP启动远程服务 [英] Start remote service through PHP

查看:75
本文介绍了通过PHP启动远程服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想通过PHP ssh2_connect函数将不同的命令传递给服务器.但是我需要通过对不同的命令使用不同的按钮来传递它,但是却无法做到.

Basically i want to pass different commands to the server through PHP ssh2_connect function. But i need to pass it through using different buttons for different commands, But unable to do it.

下面是带有按钮的代码:

Below is my code with button:

<?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

无需按钮即可工作的代码:

Code working without button:

<?php
$connection = ssh2_connect('ipaddress', 22);
ssh2_auth_password($connection, 'root', 'password');
$stream = ssh2_exec($connection, 'systemctl stop apache2');

推荐答案

以下几点:

  • 您的代码尚未完成,缺少了HTML的其余部分.
  • 您的嵌套ssh2_exec($stream = ssh2_exec(,很可能是拼写错误.
  • 您停止使用的Apache无法启动.
  • Your codes not finished, it missing the rest of the HTML.
  • Your nesting ssh2_exec($stream = ssh2_exec(, most likely a typo.
  • Your stopping apache not starting it.

以下内容将帮助您入门,希望对您有所帮助.

The following will get you started, hope it helps.

<?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 ?>

这篇关于通过PHP启动远程服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆