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

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

成都創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站和中國(guó)電信云錦天府的網(wǎng)絡(luò)公司,有著豐富的建站經(jīng)驗(yàn)和案例。

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

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

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

php實(shí)現(xiàn)遠(yuǎn)程操作

使用 php 進(jìn)行遠(yuǎn)程操作的時(shí)候,需要安裝SSH2模塊。關(guān)于在SSH2模塊中用到過(guò)的幾個(gè)函數(shù),做一個(gè)簡(jiǎn)單的記錄。

常用方法

1、連接

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

連接到一個(gè) SSH 服務(wù)器

2、認(rèn)證

ssh3_auth_password ($session, $username, $password)

在 SSH  上使用普通密碼進(jìn)行認(rèn)證

或者

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

通過(guò)公鑰進(jìn)行認(rèn)證

3、文件傳送

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

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

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

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

4、執(zhí)行命令

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

在遠(yuǎn)程機(jī)器上執(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 )

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

簡(jiǎn)單應(yīng)用

//建立連接
$connection = ssh3_connect($host, (int)$port);
if (!$connection) {
 
... ...
 
}
 
//進(jìn)行認(rèn)證
 
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);
 
}

到此,相信大家對(duì)“如何使用php實(shí)現(xiàn)遠(yuǎn)程連接”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

文章名稱:如何使用php實(shí)現(xiàn)遠(yuǎn)程連接
文章位置:http://muchs.cn/article8/johiip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、移動(dòng)網(wǎng)站建設(shè)做網(wǎng)站、關(guān)鍵詞優(yōu)化網(wǎng)站營(yíng)銷、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化