如何使用php实现远程连接

本篇内容主要讲解“如何使用php实现远程连接”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用php实现远程连接”吧!

成都创新互联公司是一家专注于成都网站设计、做网站和中国电信云锦天府的网络公司,有着丰富的建站经验和案例。

用php实现远程连接的方法:首先安装SSH2模块;然后通过“ssh3_connect ($host, $port = null, $methods = nullarray , $callbacks = nullarray)”方法连接即可。

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

怎么用php实现远程连接?

php实现远程操作

使用 php 进行远程操作的时候,需要安装SSH2模块。关于在SSH2模块中用到过的几个函数,做一个简单的记录。

常用方法

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 协议发送文件

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

通过 scp 协议获得文件

4、执行命令

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

在远程机器上执行命令

5、其他

ssh3_fetch_stream ($channel, $streamid) {}

获取拓展的数据流。常用的$streamid 定义有:

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

设置流为 阻塞/非阻塞 状态。当 $mode 为 true 时为阻塞; $mode 为 false 时,则为非阻塞状态。

简单应用

//建立连接
$connection = ssh3_connect($host, (int)$port);
if (!$connection) {
 
... ...
 
}
 
//进行认证
 
if (!ssh3_auth_password($connection, $user, $password)) {
 
... ...
 
}
 
//发送文件
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实现远程连接”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

文章名称:如何使用php实现远程连接
文章位置:https://www.cdcxhl.com/article8/johiip.html

成都网站建设公司_创新互联,为您提供网站排名移动网站建设做网站关键词优化网站营销网站内链

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都seo排名网站优化