Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def create_ssh_tunnel(self, tunnel_password):
- """
- This method is used to create ssh tunnel and update the IP Address and
- IP Address and port to localhost and the local bind port return by the
- SSHTunnelForwarder class.
- :return: True if tunnel is successfully created else error message.
- """
- # Fetch Logged in User Details.
- user = User.query.filter_by(id=current_user.id).first()
- if user is None:
- return False, gettext("Unauthorized request.")
- try:
- tunnel_password = decrypt(tunnel_password, user.password)
- # Handling of non ascii password (Python2)
- if hasattr(str, 'decode'):
- tunnel_password =
- tunnel_password.decode('utf-8').encode('utf-8')
- # password is in bytes, for python3 we need it in string
- elif isinstance(tunnel_password, bytes):
- tunnel_password = tunnel_password.decode()
- except Exception as e:
- current_app.logger.exception(e)
- return False, "Failed to decrypt the SSH tunnel "
- "password.nError: {0}".format(str(e))
- try:
- # If authentication method is 1 then it uses identity file
- # and password
- if self.tunnel_authentication == 1:
- self.tunnel_object = SSHTunnelForwarder(
- self.tunnel_host,
- ssh_username=self.tunnel_username,
- ssh_pkey=get_complete_file_path(self.tunnel_identity_file),
- ssh_private_key_password=tunnel_password,
- remote_bind_address=(self.host, self.port)
- )
- else:
- self.tunnel_object = SSHTunnelForwarder(
- self.tunnel_host,
- ssh_username=self.tunnel_username,
- ssh_password=tunnel_password,
- remote_bind_address=(self.host, self.port)
- )
- self.tunnel_object.start()
- except BaseSSHTunnelForwarderError as e:
- current_app.logger.exception(e)
- return False, "Failed to create the SSH tunnel."
- "nError: {0}".format(str(e))
- # Update the port to communicate locally
- self.local_bind_port = self.tunnel_object.local_bind_port
- return True, None
- if self.tunnel_authentication == 1:
- self.tunnel_object = SSHTunnelForwarder(
- (self.tunnel_host, int(self.tunnel_port)),
- ssh_username=self.tunnel_username,
- ssh_pkey=get_complete_file_path(self.tunnel_identity_file),
- ssh_private_key_password=tunnel_password,
- remote_bind_address=(self.host, self.port)
- )
- else:
- self.tunnel_object = SSHTunnelForwarder(
- (self.tunnel_host, int(self.tunnel_port)),
- ssh_username=self.tunnel_username,
- ssh_password=tunnel_password,
- remote_bind_address=(self.host, self.port)
- )
Add Comment
Please, Sign In to add comment