首页
新闻早报
常用网址
友链
更多
关于
推荐
运行状态
精品流量卡
Search
1
使用GitHub当博客图床提升博客访问速度
531 阅读
2
CDN缓存规则推荐
244 阅读
3
iOS巨魔永久屏蔽手机系统更新
232 阅读
4
PHP实现SSH远程连接服务器并操作服务器
194 阅读
5
野马通信号卡系统使用教程
166 阅读
个人笔记
值得一看
一些文章
教程分享
软件分享
登录
Search
标签搜索
GitHub
PHP
CDN
野马通讯
CSS
西部数码
AliYunDunMonitor
i o s
typora
安辰
累计撰写
26
篇文章
累计收到
12
条评论
首页
栏目
个人笔记
值得一看
一些文章
教程分享
软件分享
页面
新闻早报
常用网址
友链
关于
推荐
运行状态
精品流量卡
搜索到
1
篇与
的结果
2023-05-01
PHP实现SSH远程连接服务器并操作服务器
前言如何使用php执行本地命令这个肯定大部分会php的朋友都知道,比如:exec函数、shell_exec函数等,但是如果我想执行远程服务器的命令呢?代码class CustomSsh{ protected $host; protected $port = 22; protected $password; protected $publicKey; protected $privateKey; protected $session; protected $username; public function __construct(array $config,$connect_type='password'){ $this->host = $config['host']; $this->port = $config['port']??22; $this->password = $config['password']??''; $this->publicKey = $config['publicKey'] ?? ''; $this->privateKey = $config['privateKey'] ?? ''; $this->username = $config['username'] ?? ''; $this->session = ssh2_connect($this->host, $this->port); if($connect_type == 'password'){ ssh2_auth_password($this->session, $this->username, $this->password); }else{ ssh2_auth_pubkey_file($this->session,$this->username,$this->publicKey,$this->privateKey); } } public function execute($cmd){ if($cmd == ""){ return false; } $stream = ssh2_exec($this->session, $cmd); stream_set_blocking($stream, true); $content = stream_get_contents($stream); return trim($content); } //接收文件 public function recvFile($remote_file,$local_file){ return ssh2_scp_recv($this->session, $remote_file, $local_file); } //发送文件 public function sendFile($local_file,$remote_file){ return ssh2_scp_send($this->session, $local_file, $remote_file); } public function __destruct(){ ssh2_disconnect($this->session); } } $config = [ 'host' => '', 'port' => '', 'publicKey' => "", 'privateKey' => "", 'username' => "" ]; $customSsh = new CustomSsh($config,'publicKey'); echo $customSsh->execute("cd /data/web/ && ls -la"); $customSsh->recvFile("/a.txt","/a.txt");
2023年05月01日
194 阅读
0 评论
0 点赞