是否可以重用Phpseclib的连接字符串?

时间:2012-11-28 16:56:44

标签: php database-connection connection-string phpseclib

假设我在类文件中有以下两种方法。在我的代码中,我通过执行以下操作来启动连接。

$host = new Host($ip, $targ, $this->log, $pwd);

然后在我的代码中,我调用scp方法。

$host->scp("$dir.tar.gz", "{$targ['deploy_to']}/releases/$dir.tar.gz");

它按原样工作,但是当我第一次连接时,我不想将连接字符串传递给scp方法。

class Host {
    private $conn = NULL;

    function __construct($host, $target, $log, $pwd=NULL) {
        $this->log = $log;
        $this->loud = $target['verbose'];
        $this->quiet = $target['quiet'];
        $this->user = $target['user'];
        $this->rsa_key = $target['private_key_file'];
        $this->conn = $this->connect($host, $target, $pwd);
    }


    private function connect($host, $target, $pwd = NULL) {
        $key = new Crypt_RSA();
        $key->loadKey(file_get_contents($target['private_key_file']));
        $this->log->verbose("Connecting to $host on port {$target['ssh_port']}");
        $conn = new Net_SSH2('10.199.1.70');
        $this->log->verbose("Authenticating as {$target['deploy_user']} using {$target['public_key_file']}");

        if (!$conn->login($target['deploy_user'], $key)) {
            $this->log->error("Unable to authenticate");
        }

        $this->log->ssh[$host] = $this;
        return $conn;
    }

    function scp($local_file, $remote_file, $mode = 0644) {
        $sftp = new Net_SFTP('10.199.1.70');
        $key = new Crypt_RSA();

        $key->loadKey(file_get_contents($this->rsa_key));
        if (!$sftp->login($this->user, $key)) {
            exit('Login Failed');
        }

        $this->log->verbose("scp'ing $local_file to $remote_file mode ".decoct($mode));
        $ret = $sftp->put($remote_file, $local_file,NET_SFTP_LOCAL_FILE);
        return $ret;
    }
}

1 个答案:

答案 0 :(得分:0)

这有什么原因不起作用吗?:

$ret = $this->conn->put($remote_file, $local_file,NET_SFTP_LOCAL_FILE);