Home > Backend Development > PHP Tutorial > shell - php模拟ssh方式登录执行mysql语句

shell - php模拟ssh方式登录执行mysql语句

WBOY
Release: 2016-06-06 20:09:52
Original
1612 people have browsed it

如何使用php模拟实现ssh方式登录执行mysql语句输出结果?类似于navicat的SSH连接数据库
shell - php模拟ssh方式登录执行mysql语句
shell - php模拟ssh方式登录执行mysql语句
查询php.net SSH文档先验证SSH:

<code>$conn=ssh2_connect('SSH IP',22);
ssh2_auth_password($conn,'SSH USER','SSH PASS');
</code>
Copy after login
Copy after login

然后看网上几种不知道是否正确的写法:
1.使用ssh2_tunnel

<code>$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);
</code>
Copy after login
Copy after login

2.使用ssh2_exec

<code>$stream=ssh2_exec($connection,'echo "select * from db.table where id=\"1\";"  | mysql');
</code>
Copy after login
Copy after login

3.使用shell_exec

<code>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);
</code>
Copy after login
Copy after login

...好像还有使用ssh2_shell的,在不知道数据库服务器可以执行那些脚本的情况下怎么写这个连接?

回复内容:

如何使用php模拟实现ssh方式登录执行mysql语句输出结果?类似于navicat的SSH连接数据库
shell - php模拟ssh方式登录执行mysql语句
shell - php模拟ssh方式登录执行mysql语句
查询php.net SSH文档先验证SSH:

<code>$conn=ssh2_connect('SSH IP',22);
ssh2_auth_password($conn,'SSH USER','SSH PASS');
</code>
Copy after login
Copy after login

然后看网上几种不知道是否正确的写法:
1.使用ssh2_tunnel

<code>$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);
</code>
Copy after login
Copy after login

2.使用ssh2_exec

<code>$stream=ssh2_exec($connection,'echo "select * from db.table where id=\"1\";"  | mysql');
</code>
Copy after login
Copy after login

3.使用shell_exec

<code>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);
</code>
Copy after login
Copy after login

...好像还有使用ssh2_shell的,在不知道数据库服务器可以执行那些脚本的情况下怎么写这个连接?

可以参看我的笔记介绍SSH2的

连接到远程shell:

<code>$connection = ssh2_connect('example.com', 22);

if (ssh2_auth_password($connection, 'root', 'password')) {
    echo "Authentication Successful!\n";
} else {
    die('Authentication Failed...');
}
</code>
Copy after login

执行命令:

<code>$stream = ssh2_exec($connection, 'mysql -uroot -p密码;use 数据库名;select * from table_name');
</code>
Copy after login

获取返回结果:

<code>stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out);
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template