def open(self): assert not self.connected no_prompt = self.get_option("no_prompt", True) store_password = self.get_option("store_password", False) if self.timeout: self.ftp.connect(self.host, self.port, self.timeout) else: # Py2.7 uses -999 as default for `timeout`, Py3 uses None self.ftp.connect(self.host, self.port) self.ftp_socket_connected = True if self.username is None or self.password is None: creds = get_credentials_for_url(self.host, allow_prompt=not no_prompt) if creds: self.username, self.password = creds try: # Login (as 'anonymous' if self.username is undefined): self.ftp.login(self.username, self.password) except error_perm as e: # If credentials were passed, but authentication fails, prompt # for new password if not e.args[0].startswith("530"): raise # error other then '530 Login incorrect' print(e) if not no_prompt: self.user, self.password = prompt_for_password( self.host, self.username) self.ftp.login(self.username, self.password) if self.tls: # Upgrade data connection to TLS. self.ftp.prot_p() try: self.ftp.cwd(self.root_dir) except error_perm as e: # If credentials were passed, but authentication fails, prompt # for new password if not e.args[0].startswith("550"): raise # error other then 550 No such directory' print( "Could not change directory to %s (%s): missing permissions?" % (self.root_dir, e)) pwd = self.ftp.pwd() if pwd != self.root_dir: raise RuntimeError( "Unable to navigate to working directory %r (now at %r)" % (self.root_dir, pwd)) self.cur_dir = pwd self.connected = True # Successfully authenticated: store password if store_password: save_password(self.host, self.username, self.password) # TODO: case sensitivity? # resp = self.ftp.sendcmd("system") # self.is_unix = "unix" in resp.lower() # not necessarily true, better check with read/write tests self._lock() return
def open(self): assert not self.ftp_socket_connected super(FtpTarget, self).open() options = self.get_options_dict() no_prompt = self.get_option("no_prompt", True) store_password = self.get_option("store_password", False) self.ftp.set_debuglevel(self.get_option("ftp_debug", 0)) # Optionally use FTP active mode (default: PASV) (issue #21) force_active = self.get_option("ftp_active", False) self.ftp.set_pasv(not force_active) if self.timeout: self.ftp.connect(self.host, self.port, self.timeout) else: # Py2.7 uses -999 as default for `timeout`, Py3 uses None self.ftp.connect(self.host, self.port) self.ftp_socket_connected = True if self.username is None or self.password is None: creds = get_credentials_for_url(self.host, options, force_user=self.username) if creds: self.username, self.password = creds while True: try: # Login (as 'anonymous' if self.username is undefined): self.ftp.login(self.username, self.password) if self.get_option("verbose", 3) >= 4: write("Login as '{}'.".format( self.username if self.username else "anonymous")) break except ftplib.error_perm as e: # If credentials were passed, but authentication fails, prompt # for new password if not e.args[0].startswith("530"): raise # error other then '530 Login incorrect' write_error("Could not login to {}@{}: {}".format( self.username, self.host, e)) if no_prompt or not self.username: raise creds = prompt_for_password(self.host, self.username) self.username, self.password = creds # Continue while-loop if self.tls: # Upgrade data connection to TLS. self.ftp.prot_p() try: self.ftp.cwd(self.root_dir) except ftplib.error_perm as e: # If credentials were passed, but authentication fails, prompt # for new password if not e.args[0].startswith("550"): raise # error other then 550 No such directory' write_error( "Could not change directory to {} ({}): missing permissions?". format(self.root_dir, e)) pwd = self.ftp.pwd() if pwd != self.root_dir: raise RuntimeError( "Unable to navigate to working directory {!r} (now at {!r})". format(self.root_dir, pwd)) self.cur_dir = pwd # self.ftp_initialized = True # Successfully authenticated: store password if store_password: save_password(self.host, self.username, self.password) # TODO: case sensitivity? # resp = self.ftp.sendcmd("system") # self.is_unix = "unix" in resp.lower() # not necessarily true, better check with r/w tests self._lock() return
def open(self): assert not self.ftp_socket_connected super(FtpTarget, self).open() options = self.get_options_dict() no_prompt = self.get_option("no_prompt", True) store_password = self.get_option("store_password", False) verbose = self.get_option("verbose", 3) self.ftp.set_debuglevel(self.get_option("ftp_debug", 0)) # Optionally use FTP active mode (default: PASV) (issue #21) force_active = self.get_option("ftp_active", False) self.ftp.set_pasv(not force_active) if self.timeout: self.ftp.connect(self.host, self.port, self.timeout) else: # Py2.7 uses -999 as default for `timeout`, Py3 uses None self.ftp.connect(self.host, self.port) self.ftp_socket_connected = True if self.username is None or self.password is None: creds = get_credentials_for_url( self.host, options, force_user=self.username ) if creds: self.username, self.password = creds while True: try: # Login (as 'anonymous' if self.username is undefined): self.ftp.login(self.username, self.password) if verbose >= 4: write( "Login as '{}'.".format( self.username if self.username else "anonymous" ) ) break except ftplib.error_perm as e: # If credentials were passed, but authentication fails, prompt # for new password if not e.args[0].startswith("530"): raise # error other then '530 Login incorrect' write_error( "Could not login to {}@{}: {}".format(self.username, self.host, e) ) if no_prompt or not self.username: raise creds = prompt_for_password(self.host, self.username) self.username, self.password = creds # Continue while-loop if self.tls: # Upgrade data connection to TLS. self.ftp.prot_p() try: self.syst_response = self.ftp.sendcmd("SYST") if verbose >= 5: write("SYST: '{}'.".format(self.syst_response.replace("\n", " "))) # self.is_unix = "unix" in resp.lower() # not necessarily true, better check with r/w tests # TODO: case sensitivity? except Exception as e: write("SYST command failed: '{}'".format(e)) try: self.feat_response = self.ftp.sendcmd("FEAT") self.support_utf8 = "UTF8" in self.feat_response if verbose >= 5: write("FEAT: '{}'.".format(self.feat_response.replace("\n", " "))) except Exception as e: write("FEAT command failed: '{}'".format(e)) if self.encoding == "utf-8": if not self.support_utf8 and verbose >= 4: write( "Server does not list utf-8 as supported feature (using it anyway).", warning=True, ) try: # Announce our wish to use UTF-8 to the server as proposed here: # See https://tools.ietf.org/html/draft-ietf-ftpext-utf-8-option-00 # Note: this RFC is inactive, expired, and failed on Strato self.ftp.sendcmd("OPTS UTF-8") if verbose >= 4: write("Sent 'OPTS UTF-8'.") except Exception as e: if verbose >= 4: write("Could not send 'OPTS UTF-8': '{}'".format(e), warning=True) try: # Announce our wish to use UTF-8 to the server as proposed here: # See https://tools.ietf.org/html/rfc2389 # https://www.cerberusftp.com/phpBB3/viewtopic.php?t=2608 # Note: this was accepted on Strato self.ftp.sendcmd("OPTS UTF8 ON") if verbose >= 4: write("Sent 'OPTS UTF8 ON'.") except Exception as e: write("Could not send 'OPTS UTF8 ON': '{}'".format(e), warning=True) if hasattr(self.ftp, "encoding"): # Python 3 encodes using latin-1 by default(!) # (In Python 2 ftp.encoding does not exist, but ascii is used) if self.encoding != codecs.lookup(self.ftp.encoding).name: write( "Setting FTP encoding to {} (was {}).".format( self.encoding, self.ftp.encoding ) ) self.ftp.encoding = self.encoding try: self.ftp.cwd(self.root_dir) except ftplib.error_perm as e: if not e.args[0].startswith("550"): raise # error other then 550 No such directory' write_error( "Could not change directory to {} ({}): missing permissions?".format( self.root_dir, e ) ) pwd = self.pwd() # pwd = self.to_unicode(pwd) if pwd != self.root_dir: raise RuntimeError( "Unable to navigate to working directory {!r} (now at {!r})".format( self.root_dir, pwd ) ) self.cur_dir = pwd # self.ftp_initialized = True # Successfully authenticated: store password if store_password: save_password(self.host, self.username, self.password) self._lock() return
def open(self): assert not self.ftp_socket_connected super(FTPTarget, self).open() options = self.get_options_dict() no_prompt = self.get_option("no_prompt", True) store_password = self.get_option("store_password", False) verbose = self.get_option("verbose", 3) self.ftp.set_debuglevel(self.get_option("ftp_debug", 0)) # Optionally use FTP active mode (default: PASV) (issue #21) force_active = self.get_option("ftp_active", False) self.ftp.set_pasv(not force_active) self.ftp.connect(self.host, self.port, self.timeout) # if self.timeout: # self.ftp.connect(self.host, self.port, self.timeout) # else: # # Py2.7 uses -999 as default for `timeout`, Py3 uses None # self.ftp.connect(self.host, self.port) self.ftp_socket_connected = True if self.username is None or self.password is None: creds = get_credentials_for_url(self.host, options, force_user=self.username) if creds: self.username, self.password = creds while True: try: # Login (as 'anonymous' if self.username is undefined): self.ftp.login(self.username, self.password) if verbose >= 4: write("Login as '{}'.".format( self.username if self.username else "anonymous")) break except ftplib.error_perm as e: # If credentials were passed, but authentication fails, prompt # for new password if not e.args[0].startswith("530"): raise # error other then '530 Login incorrect' write_error("Could not login to {}@{}: {}".format( self.username, self.host, e)) if no_prompt or not self.username: raise creds = prompt_for_password(self.host, self.username) self.username, self.password = creds # Continue while-loop if self.tls: # Upgrade data connection to TLS. self.ftp.prot_p() try: self.syst_response = self.ftp.sendcmd("SYST") if verbose >= 5: write("SYST: '{}'.".format( self.syst_response.replace("\n", " "))) # self.is_unix = "unix" in resp.lower() # not necessarily true, better check with r/w tests # TODO: case sensitivity? except Exception as e: write("SYST command failed: '{}'".format(e)) try: self.feat_response = self.ftp.sendcmd("FEAT") self.support_utf8 = "UTF8" in self.feat_response if verbose >= 5: write("FEAT: '{}'.".format( self.feat_response.replace("\n", " "))) except Exception as e: write("FEAT command failed: '{}'".format(e)) if self.encoding == "utf-8": if not self.support_utf8 and verbose >= 4: write( "Server does not list utf-8 as supported feature (using it anyway).", warning=True, ) try: # Announce our wish to use UTF-8 to the server as proposed here: # See https://tools.ietf.org/html/draft-ietf-ftpext-utf-8-option-00 # Note: this RFC is inactive, expired, and failed on Strato self.ftp.sendcmd("OPTS UTF-8") if verbose >= 4: write("Sent 'OPTS UTF-8'.") except Exception as e: if verbose >= 4: write("Could not send 'OPTS UTF-8': '{}'".format(e), warning=True) try: # Announce our wish to use UTF-8 to the server as proposed here: # See https://tools.ietf.org/html/rfc2389 # https://www.cerberusftp.com/phpBB3/viewtopic.php?t=2608 # Note: this was accepted on Strato self.ftp.sendcmd("OPTS UTF8 ON") if verbose >= 4: write("Sent 'OPTS UTF8 ON'.") except Exception as e: write("Could not send 'OPTS UTF8 ON': '{}'".format(e), warning=True) if hasattr(self.ftp, "encoding"): # Python 3 encodes using latin-1 by default(!) # (In Python 2 ftp.encoding does not exist, but ascii is used) if self.encoding != codecs.lookup(self.ftp.encoding).name: write("Setting FTP encoding to {} (was {}).".format( self.encoding, self.ftp.encoding)) self.ftp.encoding = self.encoding try: self.ftp.cwd(self.root_dir) except ftplib.error_perm as e: if not e.args[0].startswith("550"): raise # error other then 550 No such directory' write_error( "Could not change directory to {} ({}): missing permissions?". format(self.root_dir, e)) pwd = self.pwd() # pwd = self.to_unicode(pwd) if pwd != self.root_dir: raise RuntimeError( "Unable to navigate to working directory {!r} (now at {!r})". format(self.root_dir, pwd)) self.cur_dir = pwd # self.ftp_initialized = True # Successfully authenticated: store password if store_password: save_password(self.host, self.username, self.password) self._lock() return
def open(self): assert not self.ftp_socket_connected super(SFTPTarget, self).open() options = self.get_options_dict() no_prompt = self.get_option("no_prompt", True) store_password = self.get_option("store_password", False) verbose = self.get_option("verbose", 3) verify_host_keys = not self.get_option("no_verify_host_keys", False) if self.get_option("ftp_active", False): raise RuntimeError("SFTP does not have active/passive mode.") if verbose <= 3: logging.getLogger("paramiko.transport").setLevel(logging.WARNING) write("Connecting {}:*** to sftp://{}".format(self.username, self.host)) cnopts = pysftp.CnOpts() cnopts.log = self.get_option("ftp_debug", False) if not verify_host_keys: cnopts.hostkeys = None if self.username is None or self.password is None: creds = get_credentials_for_url( self.host, options, force_user=self.username ) if creds: self.username, self.password = creds assert self.sftp is None while True: try: self.sftp = pysftp.Connection( self.host, username=self.username, password=self.password, port=self.port, cnopts=cnopts, ) break except paramiko.ssh_exception.AuthenticationException as e: write_error( "Could not login to {}@{}: {}".format(self.username, self.host, e) ) if no_prompt or not self.username: raise creds = prompt_for_password(self.host, self.username) self.username, self.password = creds # Continue while-loop except paramiko.ssh_exception.SSHException as e: write_error( "{exc}: Try `ssh-keyscan HOST` to add it " "(or pass `--no-verify-host-keys` if you don't care about security).".format( exc=e ) ) raise if verbose >= 4: write( "Login as '{}'.".format(self.username if self.username else "anonymous") ) if self.sftp.logfile: write("Logging to {}".format(self.sftp.logfile)) self.sftp.timeout = self.timeout self.ftp_socket_connected = True try: self.sftp.cwd(self.root_dir) except IOError as e: # if not e.args[0].startswith("550"): # raise # error other then 550 No such directory' write_error( "Could not change directory to {} ({}): missing permissions?".format( self.root_dir, e ) ) pwd = self.pwd() # pwd = self.to_unicode(pwd) if pwd != self.root_dir: raise RuntimeError( "Unable to navigate to working directory {!r} (now at {!r})".format( self.root_dir, pwd ) ) self.cur_dir = pwd # self.ftp_initialized = True # Successfully authenticated: store password if store_password: save_password(self.host, self.username, self.password) self._lock() return