php ssh2扩展操作类

WBOY
发布: 2016-06-20 12:34:37
原创
1203 人浏览过

php 的ssh2扩展可以找度娘

<?phpclass Ssh{    protected $host;    protected $user;    protected $password;    protected $connection;    protected $authMethods;    protected $data;    public function __construct($host, $user, $password)    {        $this->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.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!