/** * * @param string $remoteFileName * @param string $localFilename * @throws \Exception */ public function receive($remoteFileName, $localFilename) { if (empty($this->session)) { throw new \Exception("Veuillez vous connecter au serveur avant de demander un fichier"); } ssh2_scp_recv($this->session, $remoteFileName, $localFilename); }
function download_file($file_path, $new_file_path) { if ($this->sftp_connection) { return @ssh2_scp_recv($this->ssh2_connection, $file_path, $new_file_path); } return false; }
public function scp_recv($remote_path, $local_path) { if (!isset($this->socket) || !is_resource($this->socket)) { $this->connect(); } try { ssh2_scp_recv($this->socket, $remote_path, $local_path); $this->disconnect(); } catch (Exception $e) { // close the connection after scp command $this->error_handler($e); } }
public function download($remoteFilename, $localFilename, $overwrite = false) { if (!$overwrite and file_exists($localFilename)) { throw new ConnectionException('File exists and overwrite flag is not set'); } if (!is_writable(dirname($localFilename))) { throw new ConnectionException('Local directory is not writable'); } if (!is_resource($this->sftpResource)) { throw new ConnectionException('Connection is not open'); } $scpResult = @ssh2_scp_recv($this->sshResource, $this->getRemotePath($remoteFilename), $localFilename); if ($scpResult) { return $this; } else { throw new ConnectionException('Unable to download file'); } }
public function getFile($remoteFile, $localFile) { $this->logAction("Receiving file {$remoteFile} as {$localFile}"); if (ssh2_scp_recv($this->conn, $remoteFile, $localFile)) { return true; } $this->logAction("Receiving file {$remoteFile} as {$localFile} failed"); throw new Exception("Unable to get file to {$remoteFile}"); }
/** * Facade for file_put_contents over ssh2. * * @param resource $session The sftp resource to use. * @param string $remote_file The remote path to get from. * @param string $local_file The local file to write to. * * @return boolean True on success. */ public function scpRecv($session, $remote_file, $local_file) { return ssh2_scp_recv($session, $remote_file, $local_file); }
public function get($remote, $local) { return ssh2_scp_recv($this->conn_, $remote, $local); }
function get_contents($file, $type = '', $resumepos = 0) { $this->debug("get_contents();"); $tempfile = wp_tempnam($file); if (!$tempfile) { return false; } if (!ssh2_scp_recv($this->link, $file, $tempfile)) { return false; } $contents = file_get_contents($tempfile); unlink($tempfile); return $contents; }
if ($connection) { if (!@ssh2_auth_password($connection, $login, $password)) { dol_syslog("Could not authenticate with username " . $login . " . and password " . $password, LOG_ERR); exit(-5); } else { //$stream = ssh2_exec($connection, '/usr/bin/php -i'); /* print "Generate dump ".$filesys1.'.bz2'."\n"; $stream = ssh2_exec($connection, "mysqldump -u debian-sys-maint -p4k9Blxl2snq4FHXY -h 127.0.0.1 --single-transaction -K --tables -c -e --hex-blob --default-character-set=utf8 saasplex | bzip2 -1 > ".$filesys1.'.bz2'); stream_set_blocking($stream, true); // The command may not finish properly if the stream is not read to end $output = stream_get_contents($stream); */ $sftp = ssh2_sftp($connection); print 'Get file ' . $sourcefile . ' into ' . $targetdir . $targetfile . "\n"; ssh2_scp_recv($connection, $sourcefile, $targetdir . $targetfile); $fullcommand = "cat " . $targetdir . $targetfile . " | mysql -h" . $databaseserver . " -u" . $loginbase . " -p" . $passwordbase . " -D " . $database; if (preg_match('/\\.bz2$/', $targetfile)) { $fullcommand = "bzip2 -c -d " . $targetdir . $targetfile . " | mysql -h" . $databaseserver . " -u" . $loginbase . " -p" . $passwordbase . " -D " . $database; } if (preg_match('/\\.gz$/', $targetfile)) { $fullcommand = "gzip -d " . $targetdir . $targetfile . " | mysql -h" . $databaseserver . " -u" . $loginbase . " -p" . $passwordbase . " -D " . $database; } print "Load dump with " . $fullcommand . "\n"; $output = array(); $return_var = 0; print strftime("%Y%m%d-%H%M%S") . ' ' . $fullcommand . "\n"; exec($fullcommand, $output, $return_var); foreach ($output as $line) { print $line . "\n"; }
/////////////////////////////////// // Abrindo conexão novamente ssh // $connect_ssh = ssh2_connect($_SESSION['ipserver'], 22); if (ssh2_auth_password($connect_ssh, $_SESSION['login'], $_SESSION['password'])) { echo "Conectado..." . "<br>"; } ////////////////////////////////////// // Iniciando Deletagem das linha... // $remote_file = "/etc/bind/named.conf.local"; //way arq a ser copiado $local_file = "/tmp/named.conf.local.cpy"; //way arq copiado $local_file_modif = "/tmp/named.conf.local.cpy.alt"; //wau arq alterado // Copiando arquivo do server remote e testando se houver falha...returna false if (!ssh2_scp_recv($connect_ssh, $remote_file, $local_file)) { return false; } //Abrir file $file_original = fopen($local_file, 'r'); //abri arq para leitura $file_alterado = fopen($local_file_modif, 'w'); //cria um arq zerado // Var. para avisar se deve copiar ou não linha atual do laço $delete_line = false; //var. para ir increment. ate chegar a 4, q deve ser quando deve parar de exlucir as linhas // o "excluir na verdade eh não copiar a linha para o arquivo $local_file_modif $cont = 0; for ($i = 0;; $i++) { $linha = fgets($file_original); if (strstr($linha, $_GET['domain']) || $delete_line) {
/** * Copy a remote file on local filesystem. * * @param string $local_file * @param string $remote_file * @param boolean $overwrite * * @return boolean Returns TRUE on success or FALSE on error * * @throws \BackBee\Util\Transport\Exception\TransportException Occures if SSH connection is invalid */ public function get($local_file, $remote_file, $overwrite = false) { if (null === $this->_sftp_resource || null === $this->_ssh_resource) { throw new TransportException(sprintf('None SSH connection available.')); } $remote_file = $this->_getAbsoluteRemotePath($remote_file); if (true === $overwrite || false === file_exists($local_file)) { if (false === @ssh2_sftp_stat($this->_sftp_resource, $remote_file)) { return $this->_trigger_error(sprintf('Could not open remote file: %s.', $remote_file)); } if (false === @ssh2_scp_recv($this->_ssh_resource, $remote_file, $local_file)) { return $this->_trigger_error(sprintf('Could not send data from file %s to file %s.', $remote_file, $local_file)); } return true; } return $this->_trigger_error(sprintf('Local file already exists: %s.', $local_file)); }
/** * @param $remoteFile * @param $localFile * @return bool */ public function download($remoteFile, $localFile) { return ssh2_scp_recv($this->connection->getConnection(), $remoteFile, $localFile); }
public function downloadFile_Single_Stream($local_file, $remote_file) { $connection_data = $this->getSSH2Connection(); if (!$connection_data->connected) { //--------------------------------------------------------------------------------------------------------------- if (PHP_SSH2MST_DEBUG) { echo "\n\n_____________FILE TRANSFER FAILED___________\n"; } //--------------------------------------------------------------------------------------------------------------- $this->parent->transfer_status = PHP_SSH2MST_TRANSFER_STATUS_FAILED; return 0; } $res = ssh2_scp_recv($connection_data->connection_handle, $remote_file, $local_file); //release the connection - we dont need it anymore $this->ReleaseConnection($connection_data); return $res; }
/** * Facade for file_put_contents over ssh2. * * @param resource $session The sftp resource to use. * @param string $remote_file The remote path to get from. * @param string $local_file The local file to write to. * * @return boolean True on success. */ public function scpRecv($session, $remote_file, $local_file) { //@codeCoverageIgnoreStart return ssh2_scp_recv($session, $remote_file, $local_file); //@codeCoverageIgnoreEnd }
public function scpRecv($remote_file, $local_file) { if (!($inifile = ssh2_scp_recv($this->con, $remote_file, $local_file))) { throw new \Exception('Unable to retrieve file'); } }
/** * Fetch a file from the remote server via SCP. * * @param string $remote_file * @param string $local_file * * @return bool true if successful */ public function getFile($remote_file, $local_file) { return ssh2_scp_recv($this->session, $remote_file, $local_file); }
if (!($spHost = get_option('sp_host')) || !($spPort = get_option('sp_port')) || !($spUsername = get_option('sp_username')) || !($spPassword = get_option('sp_password')) || !($dFilePath = get_option('sp_dfilepath') . '/BTInbound.txt')) { throw new Exception('Missing connection information...', 0); } // create an connection resource if (!($ssh2_connect = @ssh2_connect($spHost, $spPort))) { throw new Exception('Connection to remote server failed...', 0); } // cauthenticate with the connection resource if (!@ssh2_auth_password($ssh2_connect, $spUsername, $spPassword)) { throw new Exception('Authentication with remote server failed...', 0); } // open the SSH connection $ssh2_sftp = ssh2_sftp($ssh2_connect); // if the remote file exists, copy it if (@ssh2_sftp_stat($ssh2_sftp, $dFilePath)) { if (!@ssh2_scp_recv($ssh2_connect, $dFilePath, $sFilePath)) { throw new Exception('Receiving file from remote server failed...', 0); } } /*------------------------------------------------*\ save flat file \*------------------------------------------------*/ $file = fopen($sFilePath, 'a'); foreach ($arrData as $row) { fputcsv($file, $row, '|'); } fclose($file); /*--------------------------------------------------------------------------------------------------*\ grab the contents of our flat file, replace our bogus characters, and then get outta there \*--------------------------------------------------------------------------------------------------*/ $contents = file_get_contents($sFilePath);
public function download(string $remotePath, string $localPath) : bool { if (@ssh2_scp_recv($this->connect, $remotePath, $localPath)) { return true; } else { throw new FileRemoteDownloadException($localPath); } }
protected function copyFile($local, $remote) { $path = rtrim($this->todir, "/") . "/"; if ($this->fetch) { $localEndpoint = $path . $remote; $remoteEndpoint = $local; $this->log('Will fetch ' . $remoteEndpoint . ' to ' . $localEndpoint, $this->logLevel); $ret = @ssh2_scp_recv($this->connection, $remoteEndpoint, $localEndpoint); if ($ret === false) { throw new BuildException("Could not fetch remote file '" . $remoteEndpoint . "'"); } } else { $localEndpoint = $local; $remoteEndpoint = $path . $remote; if ($this->autocreate) { ssh2_sftp_mkdir($this->sftp, dirname($remoteEndpoint), is_null($this->mode) ? 0777 : $this->mode, true); } $this->log('Will copy ' . $localEndpoint . ' to ' . $remoteEndpoint, $this->logLevel); if (!is_null($this->mode)) { $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint, $this->mode); } else { $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint); } if ($ret === false) { throw new BuildException("Could not create remote file '" . $remoteEndpoint . "'"); } } $this->counter++; }
private function copyRemoteToLocal($sourcePath, $destPath) { return @ssh2_scp_recv($this->sshSource, $sourcePath, $destPath); }
/** * Attempts to download a file using SCP protocol * * @param string $remoteFilePath Remote file path. File must exist. * @param string $localFileName Local file name / path. If null then script will try to save in the working directory with the original basename. * @return self * @throws Exception Invalid connection resource! due to use of validateSshResource. * @throws Exception File does not exist or no permissions to read! * @throws Exception Could not download the file! */ public function scpDownload($remoteFilePath, $localFileName = null) { $this->validateSshResource(); if (ssh2_sftp_stat($this->getSftpResource(), $remoteFilePath) === false) { throw new Exception("File does not exist or no permissions to read!"); } $savePath = $this->getLocalPrefix(); if (is_string($localFileName) === true) { $savePath .= $localFileName; } else { $path = pathinfo($remoteFilePath); $savePath .= $path['basename']; } $result = ssh2_scp_recv($this->getSshResource(), $remoteFilePath, $savePath); if ($result === false) { throw new Exception("Could not download the file!"); } return $this; }
/** * Request a file via SCP * * @param string $std_output The standard output of the executed command * @param string $std_error The standard error of the executed command * @param array $options Additional options for the specified method * * @return mixed The exit code of the executed command or false on error */ public function scpReceive(&$std_output, &$std_error, $options = array()) { //Check for valid options foreach ($options as $key => $value) { $this->{$key} = $value; } $exit_code = 255; try { $connection = $this->_authenticate($std_output); ssh2_scp_recv($connection, $this->remote_path, $this->local_path); $exit_code = 0; } catch (Exception $e) { $std_output = $e->getMessage(); } return $exit_code; }
/** * @param string $identifier * @param string $target * @return string */ public function downloadFile($identifier, $target) { if (ssh2_scp_recv($this->ssh, $identifier, $target)) { return $target; } throw new \RuntimeException('Copying file "' . $identifier . '" to temporary path "' . $target . '" failed.', 1447607200); }
/** * Retrieve a file from a remote system * @param string $remote_file * @param string $local_file * @return bool - true if succeeded, false if not */ public function recv($remote_file, $local_file) { return ssh2_scp_recv($this->resource, $remote_file, $local_file); }
/** * @param $local * @param $remote * * @throws BuildException */ protected function copyFile($local, $remote) { $path = rtrim($this->todir, "/") . "/"; if ($this->fetch) { $localEndpoint = $path . $remote; $remoteEndpoint = $local; $this->log('Will fetch ' . $remoteEndpoint . ' to ' . $localEndpoint, $this->logLevel); $ret = @ssh2_scp_recv($this->connection, $remoteEndpoint, $localEndpoint); if ($ret === false) { throw new BuildException("Could not fetch remote file '" . $remoteEndpoint . "'"); } } else { $localEndpoint = $local; $remoteEndpoint = $path . $remote; if ($this->autocreate) { ssh2_sftp_mkdir($this->sftp, dirname($remoteEndpoint), is_null($this->mode) ? 0777 : $this->mode, true); } $this->log('Will copy ' . $localEndpoint . ' to ' . $remoteEndpoint, $this->logLevel); $ret = false; // If more than "$this->heuristicDecision" successfully send files by "ssh2.sftp" over "ssh2_scp_send" // then ship this step (task finish ~40% faster) if ($this->heuristicScpSftp < $this->heuristicDecision) { if (null !== $this->mode) { $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint, $this->mode); } else { $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint); } } // sometimes remote server allow only create files via sftp (eg. phpcloud.com) if (false === $ret && $this->sftp) { // mark failure of "scp" --$this->heuristicScpSftp; // try create file via ssh2.sftp://file wrapper $fh = @fopen("ssh2.sftp://{$this->sftp}/{$remoteEndpoint}", 'wb'); if (is_resource($fh)) { $ret = fwrite($fh, file_get_contents($localEndpoint)); fclose($fh); // mark success of "sftp" $this->heuristicScpSftp += 2; } } if ($ret === false) { throw new BuildException("Could not create remote file '" . $remoteEndpoint . "'"); } } $this->counter++; }
public function get($local, $path) { $path = $this->path($path); // @todo implement nb? $return = @ssh2_scp_recv($this->getResource(), $path, $local); // Error if (!$return) { throw new Engine_Vfs_Adapter_Exception(sprintf('Unable to get "%s" to "%s"', $path, $local)); } return true; }
private function getDirectory($cPath, $sPath, $fName) { if (!file_exists($cPath)) { if (!mkdir($cPath)) { return false; } } if ($this->execCommand("cd " . $sPath . "/" . $fName) === false) { return false; } $raw = $this->execCommand("ls -al " . $sPath . "/" . $fName . "/"); $raw = explode("\n", $raw); $raw = array_slice($raw, 1); $files = $this->parseRawList($raw); $files = array_slice($files, 1); foreach ($files as $file) { //filter . and .. if ($file['name'] != "." && $file['name'] != "..") { if ($this->scp_isdir($sPath . "/" . $fName . "/" . $file['name'])) { if (!$this->getDirectory($cPath . "/" . $file['name'], $sPath . "/" . $fName, $file['name'])) { return false; } } else { if (!ssh2_scp_recv($this->id, $sPath . "/" . $fName . "/" . $file['name'], $cPath . "/" . $file['name'])) { return false; } } } } return true; }
/** * Download a file to the server * * @access public * @param string * @param string * @return bool */ function download($rempath, $locpath) { if (!$this->_is_conn()) { return FALSE; } $sftp = $this->conn_sftp; $stream = @fopen("ssh2.sftp://{$sftp}{$rempath}", 'r'); if ($stream === false) { $this->_error('ftp_unable_to_download'); return FALSE; } $result = ssh2_scp_recv($this->conn, $rempath, $locpath); @fclose($stream); return $result; }
function sftpServices($host = "localhost", $user = false, $pass = false, $filename = false, $singleAccount = false) { global $CONFIG, $sftpConfig; logFile('host =' . $host . 'user='******'pass='******'filename=' . $filename . 'single=' . $singleAccount); if (!$user && !$pass && !$filename) { return false; } $folderTmp = $user . "/" . $CONFIG['default']['zip_foldername'] . "/"; $pathFile = $CONFIG['default']['upload_path_temporary'] . $folderTmp . $filename; if ($singleAccount) { $user = $sftpConfig['user']; $pass = $sftpConfig['pass']; $host = $sftpConfig['host']; $folderTmp = $user . "/" . $CONFIG['default']['zip_foldername'] . "/"; $pathFile = $CONFIG['default']['upload_path_temporary'] . $folderTmp . $filename; } logFile(serialize($sftpConfig)); if ($sftpConfig['mode'] == '1') { $shellExec = "cd " . $CONFIG['default']['upload_path'] . " && sftp " . $user . "@" . $host . ":" . $pathFile; logFile($shellExec); exec($shellExec); return true; exit; } // if using single account to upload zip file $portDefine = $sftpConfig['port']; logFile("begin connection ssh2"); $connection = ssh2_connect($host, $portDefine); logFile("connect=" . $connection . " user="******" pass=" . $pass); if (ssh2_auth_password($connection, $user, $pass)) { logFile('sftp connected'); } else { logFile('sftp failed connected'); return false; } $sftp = ssh2_sftp($connection); logFile('pathfile' . $pathFile); logFile('filename' . $CONFIG['default']['upload_path'] . $filename); if (ssh2_scp_recv($connection, $pathFile, $CONFIG['default']['upload_path'] . $filename)) { logFile('sftp move file to tmp'); } else { logFile('sftp failed move file to tmp'); return false; } // ssh2_scp_recv($connection, $pathFile, $CONFIG['default']['upload_path'].$filename); logFile('source=' . $pathFile . ' target=' . $CONFIG['default']['upload_path'] . $filename); // unlink($pathFile); logFile('delete current file'); return true; }
function get_file($remoteFile, $localFile) { if (ssh2_scp_recv($this->conn, $remoteFile, $localFile)) { return true; } else { return false; } }