function put($remote_file, $data, $mode = self::SOURCE_STRING, $callback = null)
{
if (!isset($this->ssh)) {
return false;
}
if (!$this->ssh->exec('scp -t ' . escapeshellarg($remote_file), false)) {
// -t = to
return false;
}
$temp = $this->_receive();
if ($temp !== chr(0)) {
return false;
}
if ($this->mode == self::MODE_SSH2) {
$this->packet_size = $this->ssh->packet_size_client_to_server[SSH2::CHANNEL_EXEC] - 4;
}
$remote_file = basename($remote_file);
if ($mode == self::SOURCE_STRING) {
$size = strlen($data);
} else {
if (!is_file($data)) {
throw new FileNotFoundException("{$data} is not a valid file");
}
$fp = @fopen($data, 'rb');
if (!$fp) {
return false;
}
$size = filesize($data);
}
$this->_send('C0644 ' . $size . ' ' . $remote_file . "\n");
$temp = $this->_receive();
if ($temp !== chr(0)) {
return false;
}
$sent = 0;
while ($sent < $size) {
$temp = $mode & self::SOURCE_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size);
$this->_send($temp);
$sent += strlen($temp);
if (is_callable($callback)) {
call_user_func($callback, $sent);
}
}
$this->_close();
if ($mode != self::SOURCE_STRING) {
fclose($fp);
}
return true;
}
/** * Create backup user over ssh * @param string $addr * @param string $port * @return mixed (false or identity of the router) */ public function createBackupAccount($addr, $port) { $bcpuser = $this->config['routerboard']['backupuser']; $keyname = 'id_rsa-backup-user.pub'; if ($ssh = $this->sshConnect($addr, $port, false)) { $scp = new SCP($ssh); if (!$scp->put($keyname, $this->config['system']['ssh-dir'] . DIRECTORY_SEPARATOR . 'id_rsa.pub', SCP::SOURCE_LOCAL_FILE)) { $this->logger->log("The SSH-RSA file copy to the :'" . $addr . "' router fails!", $this->logger->setError()); return false; } $ssh->exec('user add name=' . $bcpuser . ' group=full'); sleep(1); $ssh->exec('user ssh-keys import user='******' public-key-file=' . $keyname); sleep(1); if ($ssh->exec('user comment ' . $bcpuser . ' comment="Backup User"')) { $this->logger->log("Creating of the backup account '" . $bcpuser . "' fails!", $this->logger->setError()); return false; } sleep(1); $identity = $ssh->exec('system identity print'); $identity = trim(str_replace('name:', '', trim($identity))); $this->logger->log("The backup account '" . $bcpuser . "' at '" . $identity . "'@'" . $addr . "' has been created successfully!"); $this->sshDisconnect($ssh); return $identity; } return false; }