0
假設我在類文件中有以下兩種方法。在我的代碼中,我通過執行以下操作來啓動連接。是否可以重複使用連接字符串與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;
}
}