How can I use php to simulate ssh Login and execute mysql statement output results? Php. the netSSH document first verifies SSH: {code ...} then, let's check whether several statements on the Internet are correct: 1. use ssh2_tunnel {code ...} 2. use ssh2 _...
How can I use php to simulate ssh Login and execute mysql statement output results? Similar to navicat SSH connection to the database
To query the php.net SSH document, first verify SSH:
$conn=ssh2_connect('SSH IP',22);ssh2_auth_password($conn,'SSH USER','SSH PASS');
Then, let's check whether the following statements are correct on the Internet:
1. Use 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. Use ssh2_exec
$stream=ssh2_exec($connection,'echo "select * from db.table where id=\"1\";" | mysql');
3. Use 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);
... It seems that ssh2_shell is still used. How can I write this connection without knowing that the database server can execute those scripts?
Reply content:
How can I use php to simulate ssh Login and execute mysql statement output results? Similar to navicat SSH connection to the database
To query the php.net SSH document, first verify SSH:
$conn=ssh2_connect('SSH IP',22);ssh2_auth_password($conn,'SSH USER','SSH PASS');
Then, let's check whether the following statements are correct on the Internet:
1. Use 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. Use ssh2_exec
$stream=ssh2_exec($connection,'echo "select * from db.table where id=\"1\";" | mysql');
3. Use 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);
... It seems that ssh2_shell is still used. How can I write this connection without knowing that the database server can execute those scripts?
You can refer to my notes to introduce
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...');}
Run the following command:
$ Stream = ssh2_exec ($ connection, 'mysql-uroot-p password; use Database Name; 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);