简体   繁体   中英

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. I'd like this to be processed via POST as opposed to GET. I want to avoid web CGI with bash scripts.

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.

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

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

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. This could be an index.html that just holds:

    <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 will allow you to take the values entered in the text boxes and send them to PHP through something called Ajax.

    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. You can send data from the client (your HTML page) to your server (PHP) and handle the data there.

    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.

    Here is an example of that: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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