/** * Return the current directory * * @return String * @throws CMbException */ private function _getCurrentDirectory() { if (!$this->connexion) { throw new CMbException("CSourceSFTP-connexion-failed", $this->hostname); } if (!($pwd = $this->connexion->pwd())) { throw new CMbException("CSourceSFTP-pwd-failed", $this->hostname); } return $pwd; }
/** * Returns the current directory path * * @return string */ public function pwd() { switch ($this->_connType) { case SftpHelper::TYPE_SFTP: default: $res = $this->_connection->pwd(); break; case SftpHelper::TYPE_FTP: $res = ftp_pwd($this->_connection); break; } return $res; }
/** * @param \Cogeco\Build\Entity\Host $host * @return \Net_SFTP * @throws \Cogeco\Build\Exception */ public static function connect(Host $host) { // Throw an exception if no host is provided if (empty($host)) { throw new Exception(__METHOD__ . "() No host specified"); } // If the connection doesn't already exist, create it if (empty(self::$handles[$host->hostname])) { self::log("- Starting an SSH session on {$host->hostname} for user {$host->account->username}\n\n"); self::$handles[$host->hostname] = $handle = new \Net_SFTP($host->hostname); if (!$handle->login($host->account->username, $host->account->password)) { throw new Exception(__METHOD__ . '() SSH connection failed'); } // Set the home folder, if it isn't explicitly set already $homeDirPath = $handle->pwd(); if (empty($host->homeDirPath) && !empty($homeDirPath)) { $host->homeDirPath = $homeDirPath; } } return self::$handles[$host->hostname]; }
public function cwd() { $ret = $this->ftp->pwd(); $ret = rtrim($ret, "/\\"); return $ret; }
public function uploadToFulfillment() { //test //return true; if (!$this->filePath) { return false; } $fType = $this->fulfillment_zip ? '.zip' : '.csv'; if ($this->ssl) { $sftp = new Net_SFTP($this->server); if (!$sftp->login($this->username, $this->password)) { echo 'Login Failed'; return false; } if (!$sftp->put($sftp->pwd() . $this->upload_path . $this->csvFileName . $fType, $this->filePath, NET_SFTP_LOCAL_FILE)) { // Download error echo 'error'; return false; } } else { $ftp = ftp_connect($this->server, $this->port, 20); if ($this->username && $this->password) { ftp_login($ftp, $this->username, $this->password); } ftp_pasv($ftp, true); // Passive mode if (!ftp_put($ftp, $this->upload_path . $this->csvFileName . $fType, $this->filePath, FTP_BINARY)) { // Download error echo 'error'; return false; } ftp_close($ftp); } return true; }
/** * Get current working directory * */ public function pwd() { return $this->_connection->pwd(); }
/** * This method is called immediately after the wrapper is initialized * * Connects to an SFTP server * * NOTE: This method is not get called by default for the following functions: * dir_opendir(), mkdir(), rename(), rmdir(), stream_metadata(), unlink() and url_stat() * So I implemented a call to stream_open() at the beginning of the functions and stream_close() at the end * * The wrapper will also reuse open connections * * @param String $path * @param String $mode * @param Integer $options * @param String &$opened_path * @return bool * @access public */ public function stream_open($path, $mode, $options, &$opened_path) { $url = parse_url($path); $host = $url["host"]; $port = $url["port"]; $user = $url["user"]; $pass = $url["pass"]; $this->path = $url["path"]; $connection_uuid = md5($host . $port . $user); // Generate a unique ID for the current connection if (isset(self::$instances[$connection_uuid])) { // Get previously established connection $this->sftp = self::$instances[$connection_uuid]; } else { //$context = stream_context_get_options($this->context); if (!isset($user) || !isset($pass)) { return FALSE; } // Connection $sftp = new Net_SFTP($host, isset($port) ? $port : 22); if (!$sftp->login($user, $pass)) { return FALSE; } // Store connection instance self::$instances[$connection_uuid] = $sftp; // Get current connection $this->sftp = $sftp; } $filesize = $this->sftp->size($this->path); if (isset($mode)) { $this->mode = preg_replace('#[bt]$#', '', $mode); } else { $this->mode = 'r'; } switch ($this->mode[0]) { case 'r': $this->position = 0; break; case 'w': $this->position = 0; if ($filesize === FALSE) { $this->sftp->touch($this->path); } else { $this->sftp->truncate($this->path, 0); } break; case 'a': if ($filesize === FALSE) { $this->position = 0; $this->sftp->touch($this->path); } else { $this->position = $filesize; } break; case 'c': $this->position = 0; if ($filesize === FALSE) { $this->sftp->touch($this->path); } break; default: return FALSE; } if ($options == STREAM_USE_PATH) { $opened_path = $this->sftp->pwd(); } return TRUE; }
protected function pwd() { return $this->conn->pwd(); }
if (strlen($order['expiry_date']) == 4) { $expiry_date[] = substr($order['expiry_date'], 0, 2); $expiry_date[] = '20' . substr($order['expiry_date'], 2, 2); } elseif (strlen($order['expiry_date']) == 3) { $expiry_date[] = '0' . substr($order['expiry_date'], 0, 1); $expiry_date[] = '20' . substr($order['expiry_date'], 1, 2); } else { $expiry_date[] = '00'; $expiry_date[] = '0000'; } $expiryDate = implode('/', $expiry_date); $csv[] = array($order['order_id'], date("d/m/Y"), 'HHCRBM', $order['product_name'], '', '', $order['fname'], $order['lname'], $order['address1'], $order['address2'], $order['city'], $order['country_name'], $order['zip'], $order['phone'], '', $order['email'], sprintf("%02d", substr($order['created'], 8, 2)) . '/' . sprintf("%02d", substr($order['created'], 5, 2)) . '/' . sprintf("%02d", substr($order['created'], 0, 4)), 'Credit', $arrayNote1['ccn'], $expiryDate, '', '', $arrayNote1['ccc'], 'rocket', '1'); } $name = 'lj3out_rocket_' . date('Ymdhis'); $path = array('files', 'csv', 'rockets'); $filePath = Csv::saveCsv($csv, $name, $path); $fileZipPath = AF::path($name, $path, 'zip'); $phpFilePath = AF::path('zip', $path); system('/usr/bin/php ' . $phpFilePath . ' name=' . $name . ' > /dev/null 2>&1 &'); $sftp = new Net_SFTP($option['server']); if (!$sftp->login($option['username'], $option['password'])) { exit('Login Failed'); } if (!$sftp->put($sftp->pwd() . $option['uploadPath'] . $name . '.zip', $fileZipPath, NET_SFTP_LOCAL_FILE)) { echo 'error'; die; } $sql = "UPDATE `orders` SET `rocket` = 2\n WHERE `order_id` IN (?a)"; $msql->query($sql, $orderIDs); unlink($fileZipPath); unlink($filePath);
<?php if (isset($_GET['tID'])) { //echo sftp_printer($_GET['tID'].'.xml'); if (strrpos(get_include_path(), "phpseclib0.3.0") === false) { set_include_path(get_include_path() . PATH_SEPARATOR . './phpseclib0.3.0'); // Staging requires the ../ to be ./ } include 'Net/SFTP.php'; $sftp = new Net_SFTP('ftp.tginc.com', 2222); if (!$sftp->login('flatterbox', 'T&!Fl@!!er')) { echo 'Login Failed'; } echo $sftp->pwd(); //$response = http_get("http://www.whatismyip.com/", array("timeout"=>1), $info); $externalContent = file_get_contents('http://checkip.dyndns.com/'); preg_match('/Current IP Address: \\[?([:.0-9a-fA-F]+)\\]?/', $externalContent, $m); $externalIp = $m[1]; echo '<br/>' . $externalIp; //print_r($sftp); } else { echo 'The ring... it is gone...'; } /* SFTP TO PRINTER */ function sftp_printer($xml_file) { if (strrpos(get_include_path(), "phpseclib0.3.0") === false) { set_include_path(get_include_path() . PATH_SEPARATOR . './phpseclib0.3.0'); // Staging requires the ../ to be ./ } include 'Net/SFTP.php';
/** * @return \Net_SFTP * @throws \Exception */ protected function getConnection() { if (!$this->connection) { $host = $this->config->get(FilesystemConfig::HOST); $port = $this->config->get(FilesystemConfig::PORT, 22); $username = $this->config->get(FilesystemConfig::USERNAME); $password = $this->config->get(FilesystemConfig::PASSWORD, ''); $key = $this->config->get(self::CONFIG_KEY); $keyFile = $this->config->get(self::CONFIG_KEY_FILE); $basepath = Util::normalizePath('/' . $this->config->get(FilesystemConfig::BASEPATH, '')); if ($keyFile) { $key = file_get_contents($keyFile); } if ($key) { $key = new \Crypt_RSA(); if ($password) { $key->setPassword($password); } $key->loadKey($key); $password = $key; } $connection = new \Net_SFTP($host, $port); if (!$connection->login($username, $password)) { throw new \Exception(sprintf('Could not login to %s', $host)); } if ($basepath != '/') { $connection->chdir($basepath); } $this->connection = $connection; $this->basepath = $connection->pwd() . '/'; } return $this->connection; }
function update_firmware() { $ssh = new Net_SSH2('192.168.88.1'); $current_firwmare_version = ''; if ($ssh->login('admin', '')) { $routerboard = $ssh->exec('system resource print'); preg_match_all('/([^:]*?):([^\\r\\n]*)\\r\\n?/', $routerboard, $matches); $output = array_combine(preg_replace('/\\s/', '', $matches[1]), $matches[2]); $current_firmware_version = preg_replace("/[^0-9.]/", "", $output['version']); logthis('current firmware version: ' . $current_firmware_version); $routerboard = $ssh->exec('system routerboard print'); preg_match_all('/([^:]*?):([^\\r\\n]*)\\r\\n?/', $routerboard, $matches); $output = array_combine(preg_replace('/\\s/', '', $matches[1]), $matches[2]); $model = 'RB' . preg_replace("/[^0-9]/", "", $output['model']); $ssh->disconnect(); $files = scandir($GLOBALS['firmware_directory']); $sftp = new Net_SFTP('192.168.88.1'); if (!$sftp->login('admin', '')) { exit('Login Failed'); } $routeros_file_found = 0; foreach ($files as $file) { if (strstr($file, 'routeros-' . $GLOBALS['architecture_types'][$model] . '-')) { $routeros_file_found = 1; $newfw = preg_replace("/.npk/", "", $file); $newfw = preg_replace("/[^0-9.]/", "", $newfw); if ($current_firmware_version != $newfw) { if ($current_firmware_version > $newfw) { logthis('current firmware is newer than ' . $newfw); } else { logthis('new firmware version: ' . $newfw); } } else { logthis('firmware already up to date'); } } } if ($routeros_file_found == 0) { die('no routeros for ' . $GLOBALS['architecture_types'][$model] . ' found.'); } $sftp->pwd(); foreach ($files as $file) { if ($file != '.' && $file != '..') { if (strstr($file, $GLOBALS['architecture_types'][$model]) || strstr($file, $model)) { if (!strstr($file, $current_firmware_version . '-') && !strstr($file, $current_firmware_version . '.npk')) { //append a '-' here also check for npk... logthis('sftping file (not using a password): ' . $file); $sftp->put("{$file}", file_get_contents($GLOBALS['firmware_directory'] . $file)); $ssh->exec(':beep frequency=137 length=2ms;'); } } if (strstr($file, $model)) { $configfile = $file; } } } if (!$configfile) { die('no backup for ' . $model . ' found.'); } $ssh = new Net_SSH2('192.168.88.1'); if ($ssh->login('admin', '')) { $todo = $ssh->exec('system reboot'); logthis($todo); $ssh->disconnect(); logthis('updated firmware (not using a password)'); } return $configfile; } if ($ssh->login('admin', $GLOBALS['admin_password'])) { $routerboard = $ssh->exec('system resource print'); preg_match_all('/([^:]*?):([^\\r\\n]*)\\r\\n?/', $routerboard, $matches); $output = array_combine(preg_replace('/\\s/', '', $matches[1]), $matches[2]); $current_firmware_version = preg_replace("/[^0-9.]/", "", $output['version']); logthis('current firmware version: ' . $current_firmware_version); $routerboard = $ssh->exec('system routerboard print'); preg_match_all('/([^:]*?):([^\\r\\n]*)\\r\\n?/', $routerboard, $matches); $output = array_combine(preg_replace('/\\s/', '', $matches[1]), $matches[2]); $model = 'RB' . preg_replace("/[^0-9]/", "", $output['model']); $ssh->disconnect(); $files = scandir($GLOBALS['firmware_directory']); $sftp = new Net_SFTP('192.168.88.1'); if (!$sftp->login('admin', $GLOBALS['admin_password'])) { exit('Login Failed'); } foreach ($files as $file) { if (strstr($file, 'routeros-' . $GLOBALS['architecture_types'][$model] . '-')) { $routeros_file_found = 1; $newfw = preg_replace("/.npk/", "", $file); $newfw = preg_replace("/[^0-9.]/", "", $newfw); if ($current_firmware_version != $newfw) { if ($current_firmware_version > $newfw) { logthis('current firmware is newer than ' . $newfw); } else { logthis('new firmware version: ' . $newfw); } } else { logthis('firmware already up to date'); } } } if ($routeros_file_found == 0) { die('no routeros for ' . $GLOBALS['architecture_types'][$model] . ' found.'); } $sftp->pwd(); foreach ($files as $file) { if ($file != '.' && $file != '..') { if (strstr($file, $GLOBALS['architecture_types'][$model]) || strstr($file, $model)) { if (!strstr($file, $current_firmware_version . '-') && !strstr($file, $current_firmware_version . '.npk')) { logthis('sftping file (using password): ' . $file); $sftp->put("{$file}", file_get_contents($GLOBALS['firmware_directory'] . $file)); $ssh->exec(':beep frequency=137 length=2ms;'); } } if (strstr($file, $model)) { $configfile = $file; } } } if (!$configfile) { die('no backup for ' . $model . ' found.'); } $ssh = new Net_SSH2('192.168.88.1'); if ($ssh->login('admin', $GLOBALS['admin_password'])) { $todo = $ssh->exec('system reboot'); logthis($todo); $ssh->disconnect(); logthis('updated firmware (using password)'); } else { logthis('password ' . $GLOBALS['admin_password'] . ' is incorrect. '); } return $configfile; } else { logthis('password ' . $GLOBALS['admin_password'] . ' is incorrect. '); } }