0

クラス ファイル内に次の 2 つのメソッドがあるとします。コード内で、次のようにして接続を開始します。

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

1 に答える 1

0

これが機能しない理由はありますか?:

$ret = $this->conn->put($remote_file, $local_file,NET_SFTP_LOCAL_FILE);
于 2012-11-28T20:35:41.860 に答える