Thứ Hai, 21 tháng 5, 2012

Connection with computer other by shh


Connection with computer other by shh

<?php

class Application_Model_Slaves_Ssh {

    private $_connection = null;
    private $_log = '';

    /**
     * construct
     *
     * @param string $host
     * @param int $port
     */
    public function __construct($host, $port) {

        $this->_host = $host;
        $this->_port = $port;

        $this->_connection = ssh2_connect($this->_host, $this->_port);
        if (!$this->_connection) {
            $this->log .= "Connection failed !";
        }
    }

    /**
     * login server
     *
     * @param string $user
     * @param string $password
     */
    public function authPassword($user, $password) {

        $this->_username = $user;
        $this->_password = $password;

        if ($this->_connection) {
            if (!ssh2_auth_password($this->_connection, $this->_username, $this->_password)) {
                $this->_log .= "\n Authorization failed !";
            }
        }
    }

    /**
     * execute command line
     *
     * @param string $cmd
     * @return string
     */
    public function cmdExec($cmd) {

        if ($this->_connection) {
            $stream = ssh2_exec($this->_connection, $cmd);
            stream_set_blocking($stream, true);
            $stdout = stream_get_contents($stream);

            $stderr = stream_get_contents(ssh2_fetch_stream($stream, SSH2_STREAM_STDERR));
            $this->_log .= "\n " . $stderr;
            print_r($stderr);
        } else {
            $stdout = 'Connection failed !';
        }

        return $stdout;
    }

    /**
     * push file to server
     *
     * @param string $localFile
     * @param string $serverFile
     * @param int $mode
     */
    public function pushFile($localFile, $serverFile, $mode = 0644) {
        if (file_exists($localFile)) {
            $folders = explode("/", $serverFile);
            unset($folders[count($folders) - 1]);
            $this->createFolderInServer(implode('/', $folders));

            ssh2_scp_send($this->_connection, $localFile, $serverFile, $mode);
        }
    }

    /**
     * get file from server
     *
     * @param string $localFile
     * @param string $serverFile
     */
    public function getFile($localFile, $serverFile) {
        $sftp = ssh2_sftp($this->_connection);

        if (file_exists("ssh2.sftp://$sftp/" . $serverFile)) {
            $folders = explode("/", $localFile);
            unset($folders[count($folders) - 1]);
            $this->createFolderInLocal(implode('/', $folders));

            ssh2_scp_recv($this->_connection, $serverFile, $localFile);
        }
    }

    /**
     * create folder in server
     *
     * @param string $pathFolder
     * @param int $mode
     */
    public function createFolderInServer($pathFolder, $mode = 0777) {
        $folders = explode('/', $pathFolder);

        $path = '';
        $sftp = ssh2_sftp($this->_connection);
        foreach ($folders as $folder) {
            if ($folder != '') {
                $path .= '/' . $folder;
                if (!is_dir("ssh2.sftp://$sftp" . $path)) {
                    ssh2_sftp_mkdir($sftp, $path, $mode);
                }
            }
        }
    }

    /**
     * create folder in local
     *
     * @param string $pathFolder
     * @param int $mode
     */
    public function createFolderInLocal($pathFolder, $mode = 0777) {
        $folders = explode('/', $pathFolder);

        $path = '';
        foreach ($folders as $folder) {
            if ($folder != '') {
                $path .= '/' . $folder;
                if (!is_dir($path)) {
                    mkdir($path, $mode);
                }
            }
        }
    }

    /**
     * get log
     *
     * @return string
     */
    public function getLog() {
        return $this->_log;
    }

}

Không có nhận xét nào:

Đăng nhận xét