假设我在类文件中有以下两种方法。在我的代码中,我通过执行以下操作开始连接。
$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;
}
}