【问题标题】:Is it possible to reuse a connection string with Phpseclib?是否可以使用 Phpseclib 重用连接字符串?
【发布时间】:2012-11-28 16:56:43
【问题描述】:

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

$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;
    }
}

【问题讨论】:

    标签: php database-connection connection-string phpseclib


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多