shell-php analog SSH login execution MySQL statement

Source: Internet
Author: User
Keywords Php mysql shell

How to use PHP simulation to implement SSH login to execute the MySQL statement output results? SSH connection database similar to Navicat


Querying the Php.net SSH document first validates SSH:

$conn=ssh2_connect('SSH IP',22);ssh2_auth_password($conn,'SSH USER','SSH PASS');

Then look at the online several do not know whether the correct wording:
1. Using Ssh2_tunnel

$tunnel = ssh2_tunnel($conn, 'REMOTE MYSQL IP', 3307);$mysqli=new mysqli('127.0.0.1','MYSQL USER','MYSQL PASS','MYSQL DB',3307,$tunnel);$result=$mysqli->query($SQL);

2. Using Ssh2_exec

$stream=ssh2_exec($connection,'echo "select * from db.table where id=\"1\";"  | mysql');

3. Using Shell_exec

shell_exec("ssh -f -L 127.0.0.1:3307:127.0.0.1:3306 user@remote.rjmetrics.com sleep 60 >> logfile");  $db = mysqli_connect('127.0.0.1', 'MYSQL USER','MYSQL PASS','MYSQL DB', 3307);

... As if there is a use of Ssh2_shell, how to write this connection without knowing that the database server can execute those scripts?

Reply content:

How to use PHP simulation to implement SSH login to execute the MySQL statement output results? SSH connection database similar to Navicat


Querying the Php.net SSH document first validates SSH:

$conn=ssh2_connect('SSH IP',22);ssh2_auth_password($conn,'SSH USER','SSH PASS');

Then look at the online several do not know whether the correct wording:
1. Using Ssh2_tunnel

$tunnel = ssh2_tunnel($conn, 'REMOTE MYSQL IP', 3307);$mysqli=new mysqli('127.0.0.1','MYSQL USER','MYSQL PASS','MYSQL DB',3307,$tunnel);$result=$mysqli->query($SQL);

2. Using Ssh2_exec

$stream=ssh2_exec($connection,'echo "select * from db.table where id=\"1\";"  | mysql');

3. Using Shell_exec

shell_exec("ssh -f -L 127.0.0.1:3307:127.0.0.1:3306 user@remote.rjmetrics.com sleep 60 >> logfile");  $db = mysqli_connect('127.0.0.1', 'MYSQL USER','MYSQL PASS','MYSQL DB', 3307);

... As if there is a use of Ssh2_shell, how to write this connection without knowing that the database server can execute those scripts?

You can refer to my notes for the introduction of SSH2.

To connect to a remote shell:

$connection = ssh2_connect('example.com', 22);if (ssh2_auth_password($connection, 'root', 'password')) {    echo "Authentication Successful!\n";} else {    die('Authentication Failed...');}

Execute command:

$stream = ssh2_exec($connection, 'mysql -uroot -p密码;use 数据库名;select * from table_name');

Get returned results:

stream_set_blocking($stream, true);$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);echo stream_get_contents($stream_out);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.