php ssh 管理服务器,php 利用ssh执行远程或本地liunx服务器命令
/**
* 利用ssh執行 遠程或本地liunx服務器命令
* 雖然可以用 shee_exec來執行本地機命令 但卻無法選擇用哪個用戶來執行 此函數可解決此類問題
* $host ssh 主機名 可以為ip 或 域名
* $port ssh 端口
* $ssh_username ssh 登錄用戶名
* $ssh_password ssh 登錄密碼
* $command 要執行的liunx命令
* 反回值行結果和錯誤信息
*/
function ssh_exec($host, $port = 22, $ssh_username, $ssh_password, $command) {
$con = ssh2_connect($host, $port);
$auth_methods = ssh2_auth_none($con, $ssh_username);
if (in_array('password', $auth_methods)) {//是否允許使用密碼登陸
$auth_methods = ssh2_auth_password($con, $ssh_username, $ssh_password);
}
$stdout_stream = ssh2_exec($con, $command);
$err_stream = ssh2_fetch_stream($stdout_stream, SSH2_STREAM_STDERR);
stream_set_blocking($stdout_stream, true); //阻塞執行
stream_set_blocking($err_stream, true);
$result_stdout = stream_get_contents($stdout_stream);
$result_error = stream_get_contents($err_stream);
fclose($stdout_stream);
fclose($err_stream);
return array('result' => $result_stdout, 'error' => $result_error);
}
總結
以上是生活随笔為你收集整理的php ssh 管理服务器,php 利用ssh执行远程或本地liunx服务器命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求告知,目前市场知名度较高的实木定制地板
- 下一篇: 简燚大理石瓷砖在广东属于知名品牌吗?