溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用php實現(xiàn)遠程連接

發(fā)布時間:2021-07-19 10:08:56 來源:億速云 閱讀:140 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“如何使用php實現(xiàn)遠程連接”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何使用php實現(xiàn)遠程連接”吧!

用php實現(xiàn)遠程連接的方法:首先安裝SSH2模塊;然后通過“ssh3_connect ($host, $port = null, $methods = nullarray , $callbacks = nullarray)”方法連接即可。

本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦

怎么用php實現(xiàn)遠程連接?

php實現(xiàn)遠程操作

使用 php 進行遠程操作的時候,需要安裝SSH2模塊。關于在SSH2模塊中用到過的幾個函數(shù),做一個簡單的記錄。

常用方法

1、連接

ssh3_connect ($host, $port = null, $methods = nullarray , $callbacks = nullarray )

連接到一個 SSH 服務器

2、認證

ssh3_auth_password ($session, $username, $password)

在 SSH  上使用普通密碼進行認證

或者

ssh3_auth_pubkey_file ($session, $username, $pubkeyfile, $privkeyfile, $passphrase = null)

通過公鑰進行認證

3、文件傳送

ssh3_scp_send ( resource $session , string $local_file , string $remote_file [, int $create_mode = 0644 ] )

通過 scp 協(xié)議發(fā)送文件

ssh3_scp_recv ( resource $session , string $remote_file , string $local_file )

通過 scp 協(xié)議獲得文件

4、執(zhí)行命令

ssh3_exec ($session, $command, $pty = null, $env = nullarray , $width = null, $height = null, $width_height_type = null)

在遠程機器上執(zhí)行命令

5、其他

ssh3_fetch_stream ($channel, $streamid) {}

獲取拓展的數(shù)據(jù)流。常用的$streamid 定義有:

define ('SSH2_STREAM_STDIO', 0);
define ('SSH2_STREAM_STDERR', 1);
stream_set_blocking ( resource $stream , bool $mode )

設置流為 阻塞/非阻塞 狀態(tài)。當 $mode 為 true 時為阻塞; $mode 為 false 時,則為非阻塞狀態(tài)。

簡單應用

//建立連接
$connection = ssh3_connect($host, (int)$port);
if (!$connection) {
 
... ...
 
}
 
//進行認證
 
if (!ssh3_auth_password($connection, $user, $password)) {
 
... ...
 
}
 
//發(fā)送文件
if (!ssh3_scp_send($connection, $sourceFile, $targetFile, 0644)) {
 
... ...
 
}else{
 
$stream = ssh3_exec($connection, "stat /tmp/targetFile 2>&1");
$errorStream = ssh3_fetch_stream($stream, SSH2_STREAM_STDERR);
 
// Enable blocking for both streams
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
 
echo stream_get_contents($stream);
 
// Close the streams
fclose($errorStream);
fclose($stream);
 
}

到此,相信大家對“如何使用php實現(xiàn)遠程連接”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

php
AI