/** * */ public function connect() { $this->ssh = ssh2_connect($this->configuration['hostname'], $this->configuration['port']); $username = $this->configuration['username']; switch ($this->configuration[SftpDriver::CONFIG_AUTHENTICATION_METHOD]) { case static::AUTHENTICATION_PASSWORD: ssh2_auth_password($this->ssh, $username, $this->configuration['password']); break; case static::AUTHENTICATION_PUBKEY: $publicKey = $this->configuration['publicKey']; $privateKey = $this->configuration['privateKey']; if (!file_exists($publicKey) || !file_exists($privateKey)) { return; } $password = $this->configuration['privateKeyPassword']; if (empty($password)) { $password = null; } ssh2_auth_pubkey_file($this->ssh, $username, $publicKey, $privateKey, $password); break; default: } $this->sftp = ssh2_sftp($this->ssh); $this->sftpWrapper = 'ssh2.sftp://' . $this->sftp; $this->sftpWrapperLength = strlen($this->sftpWrapper); $this->iteratorFlags = \FilesystemIterator::UNIX_PATHS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::FOLLOW_SYMLINKS; return true; }
/** * Connect to host * * Connects to the host. Throws exception if the host is unable to be connected to. Will automatically * verify the host fingerprint, if one was provided, and throw an exception if the fingerprint is not * verified. * */ public function connect() { //Attempt to connect to host $link = ssh2_connect($this->_config['host'], $this->_config['port']); //If host connection fails, throw exception if (!$link) { throw new Kohana_Exception('Unable to connect to :host on port :port', array(':host' => $host, ':port' => $port)); } else { //Assign the connection link to the class property $this->_conn_link = $link; //If host fingerprint is not NULL, attempt to verify fingerprint if (!is_null($this->_config['host_fingerprint'])) { $verify = $this->verify_host_fingerprint(); //If the fingerprint is not verified, throw exception if (!$verify) { throw new Kohana_Exception('Unable to verify host fingerprint'); } } } //Attempt to login user if ($this->_config['authentication_method'] == 'KEY') { $this->_connected = $this->login_key(); } else { $this->_connected = $this->login_password(); } $this->_connected && ($this->_sftp = ssh2_sftp($link)); }
/** * Connect operation */ public function connect() { if ($this->_ssh2 != null) { // Already connected return; } // Connect to server $host = isset($this->_config['hostname']) ? $this->_config['hostname'] : 'localhost'; $port = isset($this->_config['port']) ? $this->_config['port'] : 22; $username = isset($this->_config['username']) ? $this->_config['username'] : ''; $password = isset($this->_config['password']) ? $this->_config['password'] : null; $this->_ssh2 = ssh2_connect($host, $port); if ($this->_ssh2 === FALSE) { throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_open_connection'), array(':host' => $host, 'port' => $port)); } // Check fingerprint if it is specified if (isset($this->_config['fingerprint'])) { if (strtolower(ssh2_fingerprint($this->_ssh2)) != strtolower($this->_config['fingerprint'])) { throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_fingerprint_validation'), array(':key' => ssh2_fingerprint($this->_ssh2))); } } // Connect with certificate if it is specified if (isset($this->_config['pubkeyfile']) and isset($this->_config['privkeyfile'])) { if (!@ssh2_auth_pubkey_file($this->_ssh2, $username, $this->_config['pubkeyfile'], $this->_config['privkeyfile'], $password)) { throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_authentication')); } } else { if (!@ssh2_auth_password($this->_ssh2, $username, $password)) { throw new Kohana_Transfer_Exception(Kohana::message('transfer', 'fail_authentication')); } } // Enable SFTP mode $this->_sftp = ssh2_sftp($this->_ssh2); }
function getSFTPList() { $ftp_info = Context::getRequestVars(); if (!$ftp_info->ftp_host) { $ftp_info->ftp_host = "127.0.0.1"; } $connection = ssh2_connect($ftp_info->ftp_host, $ftp_info->ftp_port); if (!ssh2_auth_password($connection, $ftp_info->ftp_user, $ftp_info->ftp_password)) { return new Object(-1, 'msg_ftp_invalid_auth_info'); } $sftp = ssh2_sftp($connection); $curpwd = "ssh2.sftp://{$sftp}" . $this->pwd; $dh = @opendir($curpwd); if (!$dh) { return new Object(-1, 'msg_ftp_invalid_path'); } $list = array(); while (($file = readdir($dh)) !== false) { if (!is_dir($curpwd . $file)) { continue; } $list[] = $file . "/"; } closedir($dh); $this->add('list', $list); }
function __construct($cfg) { // set default options if missing static $defaults = array('port' => 22, 'user' => 'root'); if (is_object($cfg)) { $cfg = json_decode(json_encode($cfg), 1); } foreach ($defaults as $k => $v) { if (!isset($cfg[$k])) { $cfg[$k] = $v; } } // connect ssh2 $this->ssh2 = ssh2_connect($cfg['host'], $cfg['port']); if (!$this->ssh2) { throw new \Exception("can't connect trough ssh2\n"); } // authorize if (isset($cfg['key'])) { // private/public key authentication requested if (!ssh2_auth_pubkey_file($this->ssh2, $cfg['user'], $cfg['key']['pub'], $cfg['key']['pvt'], isset($cfg['key']['pass']) ? $cfg['key']['pass'] : NULL)) { throw new \Exception("can't authorize via key"); } } elseif (isset($cfg['pass'])) { // username & password authentication if (!ssh2_auth_password($this->ssh2, $cfg['user'], $cfg['pass'])) { throw new \Exception("can't authorize via user & pass"); } } else { throw new \Exception("not enough authentication information provided"); } $this->sftp = ssh2_sftp($this->ssh2); }
/** * @inheritdoc */ public function send($remoteFile, $localFile, $contentType = null) { $sftp = ssh2_sftp($this->getFTP()); if (false === copy($localFile, "ssh2.sftp://{$sftp}" . $remoteFile)) { throw new \Exception("Cant upload file on FTP server"); } return true; }
/** * Make SFTP connection over SSH2 * * @return boolean */ protected function connectSftp() { if (!$this->connected) { return false; } $this->ftpConnection = ssh2_sftp($this->connection); return true; }
/** * Saves the image to the remote server. If the folder structure doesn't exist, create it. * * @param string $relFilename path (with filename) from the CDN root * @param string $tempfile temp file name to upload * @return bool */ protected function _save($relFilename, $tempfile) { $base = Mage::getStoreConfig('imagecdn/ftp/base'); $remotePath = str_replace('\\', '/', str_replace('//', '/', '/' . $base . '/' . $relFilename)); ssh2_sftp_mkdir(ssh2_sftp($this->auth()), substr($remotePath, 0, strrpos($remotePath, '/')), 0777, true); $result = ssh2_scp_send($this->auth(), $tempfile, $remotePath, 0644); return $result ? true : false; }
function sftp($local_file, $remote_file) { $this->log->verbose("sftp'ing {$local_file} to {$remote_file}"); $sftp = ssh2_sftp($this->conn); $remote = fopen("ssh2.sftp://{$sftp}{$remote_file}", 'w'); $local = fopen($local_file, "r"); $ret = stream_copy_to_stream($local, $remote); return $ret; }
/** * Constructor * * @param SessionInterface * @param OutputInterface * @throws RuntimeException */ public function __construct(SessionInterface $session, OutputInterface $output) { // Set the base object properties parent::__construct($session, $output); if (!$session->valid()) { throw new RuntimeException('SSH connection failed.'); } $this->sftp = ssh2_sftp($session->getConnection()); }
public function login($username, $password) { if (!ssh2_auth_password($this->connection, $username, $password)) { throw new \Exception("Could not authenticate with username {$username} " . "and password {$password}."); } $this->sftp = ssh2_sftp($this->connection); if (!$this->sftp) { throw new \Exception("Could not initialize SFTP subsystem."); } }
protected function doLoginPubKey($user, $pubKeyFile, $privKeyFile, $passphrase = null) { // try to login if (ssh2_auth_pubkey_file($this->getSsh2Connection(), $user, $pubKeyFile, $privKeyFile, $passphrase)) { $this->sftp_id = ssh2_sftp($this->getSsh2Connection()); return $this->sftp_id != false && $this->sftp_id != null; } else { return false; } }
function ssh($domain, $username, $password) { $conn = ssh2_connect($domain, 22); if (conn === false) { $this->sftp = false; } else { ssh2_auth_password($conn, $username, $password); $this->sftp = ssh2_sftp($conn); } }
/** * Stablish a connection to sftp server. * * @param String $url * @param String $username * @param String $password * * @throws SftpNetworkException * @throws SftpAuthenticationException * * @return ssh2_sftp $connection */ protected function connection($url, $username, $password) { if (!($connection = @ssh2_connect($url, "22"))) { throw new SftpNetworkException("Could not connect to sftp server."); } if (@ssh2_auth_password($connection, $username, $password) === false) { throw new SftpAuthenticationException("Invalid username or password for sftp."); } return ssh2_sftp($connection); }
public function getSftpResource() { if (null === $this->_sftpResource) { $this->_sftpResource = @ssh2_sftp($this->getResource()); if (null === $this->_sftpResource) { throw new Engine_Vfs_Adapter_Exception('Unable to get sftp resource'); } } return $this->_sftpResource; }
/** * 远程 SSH 登录。 * * @throws PermissionException * @throws NetworkException */ function login() { $ok = ssh2_auth_password($this->sess, $this->_user, $this->_pass); if (!$ok) { throw new PermissionException('SSH 登录验证失败。'); } $this->sftp = ssh2_sftp($this->sess); if (!$this->sftp) { throw new NetworkException('初始化 SFTP 对象实例失败。'); } }
public function connect() { $this->connection = @ssh2_connect($this->hostname, $this->port, array('hostkey' => 'ssh-rsa')); $fingerprint = ssh2_fingerprint($this->connection, SSH2_FINGERPRINT_SHA1 | SSH2_FINGERPRINT_HEX); if (!@ssh2_auth_pubkey_file($this->connection, $this->username, $this->pubKeyFile, $this->privKeyFile)) { throw new Exception("Authentification Failed"); } $this->sftpSession = @ssh2_sftp($this->connection); if (!$this->sftpSession) { throw new Exception("Could not initialize SFTP subsystem."); } }
function __construct($server_ip, $username, $password = false) { debug("attempting to connect to {$server_ip} with {$username}::*****"); $this->ssh2_connection = @ssh2_connect($server_ip, 22); if ($this->ssh2_connection) { debug("Connected to {$server_ip}"); $logged_in = @ssh2_auth_password($this->ssh2_connection, $username, $password); if ($logged_in) { debug("Created SFTP connection to {$server_ip}"); $this->sftp_connection = ssh2_sftp($this->ssh2_connection); } } }
function _startup() { $this->_link = @ssh2_connect($this->hostname, $this->port ? $this->port : 22); if (!is_null($this->username)) { $login = @ssh2_auth_password($this->_link, $this->username, $this->password); } else { $login = @ssh2_auth_none($this->_link, "anonymous"); } if (!$this->_link || is_array($this->_link) || !$login) { internal_error(!$login ? "remote_authentication_failed" : "remote_connection_failed"); } $this->_sftp = @ssh2_sftp($this->_link, true); }
/** * Connects to FTP server. * @return void */ public function connect() { $this->protect(function () { $parts = parse_url($this->url); $this->connection = ssh2_connect($parts['host'], empty($parts['port']) ? 22 : (int) $parts['port']); if (isset($parts['pass'])) { ssh2_auth_password($this->connection, $parts['user'], $parts['pass']); } else { ssh2_auth_agent($this->connection, $parts['user']); } $this->sftp = ssh2_sftp($this->connection); }); }
/** * Connect to FTP/S * @param String $type * @param Object $this->ftps_login */ public function connect($type = 'SSL') { if ($type == 'SSL') { //$this->conn_id = ftp_ssl_connect($this->ftps_server) or die("Couldn't connect to ".$this->ftps_server); $this->conn_id = ssh2_connect($this->ftps_server, 22) or die("Couldn't connect to " . $this->ftps_server); $login = ssh2_auth_password($this->conn_id, $this->ftps_user, $this->ftps_password); $this->sftp = ssh2_sftp($this->conn_id); } else { $this->conn_id = ftp_connect($this->ftps_server) or die("Couldn't connect to " . $this->ftps_server); $login = ftp_login($this->conn_id, $this->ftps_user, $this->ftps_password); } $this->ftps_login = $login; }
function login($user, $pass, $host, $port = 22) { if ($this->connect($host, $port)) { if ($this->auth_pwd($user, $pass)) { $this->sftp = ssh2_sftp($this->conn); //echo('login ok'); return true; } else { return false; } } else { return false; } }
protected function conn_open($url) { $oldconn = $this->conn; if (!parent::conn_open($url)) { return false; } if ($this->conn !== $oldconn) { // Start SFTP subsystem on the new connection if (($this->sftp = ssh2_sftp($this->conn)) === false) { return false; } } return true; }
function login($username, $password) { $auth_methods = ssh2_auth_none($this->connection, $username); if (!in_array('password', $auth_methods)) { return $this->raiseError("The SSH Server does not support password based authentication"); } if (!@ssh2_auth_password($this->connection, $username, $password)) { return $this->raiseError("Could not authenticate with username {$username} " . "(using a password)."); } $this->sftp = @ssh2_sftp($this->connection); if (!$this->sftp) { return $this->raiseError("Could not initialize SFTP subsystem."); } }
public function connect() { $this->ssh2 = ssh2_connect($this->server); if ($this->ssh2 === false) { throw new UserException('sftp: connection failed (a)'); } if (@ssh2_auth_password($this->ssh2, $this->user, $this->pass) === false) { throw new UserException('sftp: authentication failed'); } $this->ftp = @ssh2_sftp($this->ssh2); if ($this->ftp === false) { throw new UserException('sftp: connection failed (b)'); } }
/** * Check solr ping url. * @return metric 0 KO, 1 OK */ public function getMetric() { $connection = ssh2_connect($this->addr, $this->port); if ($connection === false) { return 0; } if (ssh2_auth_password($connection, $this->login, $this->password) == false) { return 0; } $sftp = ssh2_sftp($connection); if ($sftp === false) { return 0; } return 1; }
public function __construct($ip, $port, $username, $password) { $this->_username = $username; if (Utils::checkPingToIPAndPort($ip, $port)) { $this->_con = ssh2_connect($ip, $port); if (!ssh2_auth_password($this->_con, $username, $password)) { die("Ungültige SSH-Login Daten."); } else { $this->_serverWriteFile = "/home/" . Core::GetConfig("sshUserName") . "/PanelLog"; $this->_sftp = @ssh2_sftp($this->_con); } } else { die("Konnte Server nicht erreichen."); } }
/** * Connects to FTP server. * @return void */ public function connect() { $this->protect(function () { $parts = parse_url($this->url); $this->connection = ssh2_connect($parts['host'], empty($parts['port']) ? 22 : (int) $parts['port']); if (isset($parts['pass'])) { ssh2_auth_password($this->connection, $parts['user'], $parts['pass']); } elseif ($this->publicKey != '') { // intentionally != ssh2_auth_pubkey_file($this->connection, $parts['user'], $this->publicKey, $this->privateKey); } else { ssh2_auth_agent($this->connection, urldecode($parts['user'])); } $this->sftp = ssh2_sftp($this->connection); }); }
function upload($file = '', $path = '', $new_name = '') { $server = CDN_FTP; $port = CDN_PORT; $username = CDN_USERNAME; $passwd = CDN_PASSWPORD; $connection = ssh2_connect($server, $port); if (ssh2_auth_password($connection, $username, $passwd)) { $sftp = ssh2_sftp($connection); //echo "Connection status: OK. Uploaded file!"; $contents = file_get_contents($file); file_put_contents("ssh2.sftp://{$sftp}/public_html/travel/upload/{$path}/{$new_name}", $contents); } else { echo "Nope! Can not connect to server!"; } }
/** * Initiates the ssh session * * @param string $server Server adress e.g. 'www.example.com' * @param string $username SSH Username * @param string $password SSH Password */ public function __construct($server, $username, $password) { if (!function_exists('ssh2_connect')) { throw new \RuntimeException("Function ssh2_connect not found"); } if (!($con = ssh2_connect($server))) { throw new \RuntimeException("Could not connect to {$server}"); } if (!ssh2_auth_password($con, $username, $password)) { throw new \RuntimeException("Could not log in to {$server} as {$username}"); } if (!($sftp = ssh2_sftp($con))) { throw new \RuntimeException("Could not initialize SFTP connection to {$server}"); } $this->connection = $con; $this->sftp = $sftp; }