php ssh2 类,php ssh2扩展操作类

本文介绍了一段PHP代码,用于通过SSH2扩展连接并认证远程服务器,然后执行shell命令。如果认证成功,它将使用ssh2_auth_password进行密码验证,并通过ssh2_exec执行指定的shell命令,如'pwd'。最后,代码会返回执行结果的状态和内容。
摘要由CSDN通过智能技术生成

php 的ssh2扩展可以找度娘 host = $host; $this->user = $user; $this->password = $password; $this->connection = ssh2_connect ( $this->host, 22 ); if (! $this->connection) die ( 'connection to ' . $host . ':22 failed' ); $this->auth_methods = ssh2_auth_none ( $this->connection, $this->user ); } /** * 执行shell命令 * @return code 101:认证失败 * 102:通过password方式登录远程服务器失败 * 103:shell命令执行失败 * 200:操作成功 */ public function execShell($shell) { if (in_array ( 'password', $this->auth_methods )) { if (ssh2_auth_password ( $this->connection, $this->user, $this->password )) { $stream = ssh2_exec ( $this->connection, $shell ); // 执行php stream_set_blocking ( $stream, true ); // 获取执行pwd后的内容 if ($stream === FALSE) { $this->data ['msg'] = 'shell命令执行失败'; } else { $this->data ['code'] = 200; $this->data ['msg'] = stream_get_contents ( $stream ); } } else { $this->data ['code'] = 102; $this->data ['msg'] = '通过password方式登录远程服务器失败'; } } else { $this->data ['code'] = 101; $this->data ['msg'] = '认证失败'; } return $this->data; }}$host='';$user='';$passwd='';$sshObj = new Ssh($host, $user, $passwd);$rs = $sshObj->execShell('pwd');var_dump($rs);

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值