简体   繁体   English

PHP:对远程主机执行服务器命令

[英]PHP: Execute Server Commands To Remote Host

I'm new to the PHP world but wanting to set up a relatively simple PHP script that will execute shell commands to a remote host via ssh2_exec when a user presses a button on the web page.我是 PHP 世界的新手,但想要设置一个相对简单的 PHP 脚本,当用户按下网页上的按钮时,该脚本将通过 ssh2_exec 向远程主机执行 shell 命令。 I'd like this to be processed via POST as opposed to GET.我希望通过 POST 而不是 GET 来处理它。 I want to avoid web CGI with bash scripts.我想避免使用 bash 脚本的 Web CGI。

After reviewing this documentation on ssh2_exec I was able to gather somewhat of an idea of what needs to be done, but I still am needing quite a bit of help.ssh2_exec 上查看此文档后,我能够对需要完成的工作有所了解,但我仍然需要相当多的帮助。

To help better understand what I'm trying to accomplish, let me explain.为了帮助更好地理解我要完成的任务,让我解释一下。 I'm looking to have two text fields and a submit button on a page.我希望在页面上有两个文本字段和一个提交按钮。 Let's call these text fields $var1 and $var2 .我们将这些文本字段称为$var1$var2 I want the user to fill out both text fields, hit submit, and that submit a command to a remote server that looks like:我希望用户填写两个文本字段,点击提交,然后向远程服务器提交一个命令,如下所示:

# [root@server] $var1 /home/$var2.sh

Now I'm not anywhere near where I need to be, but the following is the (non-working) code I've compiled on my own and looking for ways to make it work, or improvements.现在我离我需要去的地方不远了,但以下是我自己编译的(非工作)代码,正在寻找使其工作或改进的方法。 Also I realize connecting to a remote server and running commands as root via a PHP script is not a good idea.我也意识到连接到远程服务器并通过 PHP 脚本以 root 身份运行命令不是一个好主意。 But this is purely for development/testing and nothing production.但这纯粹是为了开发/测试而不是生产。 I'm just trying to familiarize myself with the process.我只是想熟悉这个过程。 Anyway, here is what I have:无论如何,这就是我所拥有的:

<?php
    $connection = ssh2_connect('192.168.1.1', 22);
    ssh2_auth_password($connection, 'root', 'password');

    if (isset($_POST['button']))
    {
         $stream = ssh2_exec($connection, 'touch /root/test/test.txt');
    }
?>
<html>
<body>
    <form method="post">
    <p>
        <button name="button">Touch</button>
    </p>
    </form>
</body>

Again, I'm sure the above code looks atrocious and entirely incorrect to a developer, but as I said I'm new to PHP so this was me just trying my best on my own.同样,我确信上面的代码对于开发人员来说看起来很糟糕并且完全不正确,但是正如我所说的,我是 PHP 的新手,所以这只是我自己尽力而为。

So with that said, if anyone has any type of insight, helpful links, tips, anything - it would be greatly appreciated!因此,话虽如此,如果有人有任何类型的见解、有用的链接、提示或任何内容 - 将不胜感激!

See that post : Other post看到那个帖子:其他帖子

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>

Using this library should help you accomplish what you need.使用这个库应该可以帮助你完成你所需要的。

As long as it's for testing.只要是为了测试。 Or else, I would use XML-RPC to call server side programs否则,我会使用XML-RPC来调用服务器端程序

I'll try to explain the steps that you need to go through in order to accomplish what you want.我将尝试解释您需要执行的步骤才能完成您想要的操作。

  1. You are going to want an HTML page somewhere on your site.你会想要在你的网站上的某个地方有一个 HTML 页面。 This could be an index.html that just holds:这可能是一个仅包含以下内容的index.html

    <html> <body> <form method="post"> <input type="text" id="text1"><br> <input type="text" id="text2"><br> <button name="button">Touch</button> </form> </body>

  2. You are going to have to look into a language called JavaScript.您将不得不研究一种称为 JavaScript 的语言。 JavaScript will allow you to take the values entered in the text boxes and send them to PHP through something called Ajax. JavaScript 将允许您获取在文本框中输入的值并通过称为 Ajax 的东西将它们发送到 PHP。

    http://www.w3schools.com/ajax/ajax_intro.asp http://www.w3schools.com/ajax/ajax_intro.asp

    In short, Ajax is a way for your website to communicate with the server and the client.简而言之,Ajax 是您的网站与服务器和客户端进行通信的一种方式。 You can send data from the client (your HTML page) to your server (PHP) and handle the data there.您可以将数据从客户端(您的 HTML 页面)发送到您的服务器 (PHP) 并在那里处理数据。

    There is another option however, you can use your current code and add an action attribute to your <form> tag and redirect the user to your PHP page and then get the data using $_POST as you wanted.但是,还有另一种选择,您可以使用当前代码并将action属性添加到<form>标记并将用户重定向到您的 PHP 页面,然后根据需要使用$_POST获取数据。

    Here is an example of that: http://www.w3schools.com/php/php_forms.asp这是一个例子: http : //www.w3schools.com/php/php_forms.asp

  3. Once the above steps have been accomplished, whether Ajax post or a form post, you can now handle all your values in PHP.完成上述步骤后,无论是 Ajax 帖子还是表单帖子,您现在都可以在 PHP 中处理所有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM