【问题标题】:Start remote service through PHP通过PHP启动远程服务
【发布时间】:2018-10-09 15:39:30
【问题描述】:

基本上我想通过 PHP ssh2_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

没有按钮的代码:

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

【问题讨论】:

    标签: php ubuntu button lamp


    【解决方案1】:

    有几点:

    • 您的代码未完成,它缺少 HTML 的其余部分。
    • 您的嵌套ssh2_exec($stream = ssh2_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 ?>
    

    【讨论】:

    • 完美,它正在工作......我已经使用上面的代码添加了更多服务。
    • np,很高兴它的工作和帮助。感谢您的接受。
    • 如果我想显示通过该代码执行的命令的输出,我该怎么办?
    • 如代码中所述,$result-&gt;output 将具有标准输出,$result-&gt;error 将具有标准错误,尽管您需要为此使用管道和 websocket,但不要将其与 tty 混淆。
    • 对。实际上,它仍然没有显示输出。我正在使用 php 5.6 不是问题吗?但是当我在 php 7 上测试时,它会显示输出。
    猜你喜欢
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2010-12-24
    • 2011-06-18
    • 2017-05-22
    • 1970-01-01
    相关资源
    最近更新 更多