gpt4 book ai didi

php - 通过PHP启动远程服务

转载 作者:行者123 更新时间:2023-12-04 18:40:16 25 4
gpt4 key购买 nike

基本上我想通过 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');

最佳答案

有几件事:

  • 您的代码尚未完成,它缺少 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 ?>

    关于php - 通过PHP启动远程服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50085326/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com