怎么用php实现远程连接
用php实现远程连接的方法:首先安装SSH2模块;然后通过“ssh2_connect ($host, $port = null, $methods = nullarray , $callbacks = nullarray)”方法连接即可。
本文操作环境:windows7系统、PHP7.1版,DELL G3电脑
怎么用php实现远程连接?
php实现远程操作
使用 php 进行远程操作的时候,需要安装SSH2模块。关于在SSH2模块中用到过的几个函数,做一个简单的记录。
常用方法
1、连接
ssh2_connect ($host, $port = null, $methods = nullarray , $callbacks = nullarray )
连接到一个 SSH 服务器
2、认证
ssh2_auth_password ($session, $username, $password)
在 SSH 上使用普通密码进行认证
或者
ssh2_auth_pubkey_file ($session, $username, $pubkeyfile, $privkeyfile, $passphrase = null)
通过公钥进行认证
3、文件传送
ssh2_scp_send ( resource $session , string $local_file , string $remote_file [, int $create_mode = 0644 ] )
通过 scp 协议发送文件
ssh2_scp_recv ( resource $session , string $remote_file , string $local_file )
通过 scp 协议获得文件
4、执行命令
ssh2_exec ($session, $command, $pty = null, $env = nullarray , $width = null, $height = null, $width_height_type = null)
在远程机器上执行命令
5、其他
ssh2_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 = ssh2_connect($host, (int)$port); if (!$connection) { ... ... } //进行认证 if (!ssh2_auth_password($connection, $user, $password)) { ... ... } //发送文件 if (!ssh2_scp_send($connection, $sourceFile, $targetFile, 0644)) { ... ... }else{ $stream = ssh2_exec($connection, "stat /tmp/targetFile 2>&1"); $errorStream = ssh2_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实现远程连接的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写
