当前位置: 首页>>代码示例>>Python>>正文


Python SSHTunnelForwarder.stop方法代码示例

本文整理汇总了Python中sshtunnel.SSHTunnelForwarder.stop方法的典型用法代码示例。如果您正苦于以下问题:Python SSHTunnelForwarder.stop方法的具体用法?Python SSHTunnelForwarder.stop怎么用?Python SSHTunnelForwarder.stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sshtunnel.SSHTunnelForwarder的用法示例。


在下文中一共展示了SSHTunnelForwarder.stop方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SSHTunnel

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]
class SSHTunnel(object):
    class TunnelException(Exception):
        pass

    def __init__(self, host, username, key_file, remote_port, host_port=nat_ssh_port_forwarding):
        """
        Returns tuple consisting of local port and sshtunnel SSHTunnelForwarder object.
        Caller must call stop() on object when finished
        """
        logger = logging.getLogger('sshtunnel')
        logger.setLevel(logging.ERROR)

        try:
            self._server = SSHTunnelForwarder((host, host_port),
                    ssh_username=username, ssh_private_key=key_file,
                    remote_bind_address=('127.0.0.1', remote_port), logger=logger)
        except sshtunnel.BaseSSHTunnelForwarderError as e:
            raise self.TunnelException(e)


    def connect(self):
        self._server.start()
        self.local_port = self._server.local_bind_port

    def close(self):
        self._server.stop()

    def __enter__(self):
        self.connect()
        return self

    def __exit__(self, type, value, traceback):
        self.close()
开发者ID:balramr,项目名称:clusterous,代码行数:35,代码来源:helpers.py

示例2: TunelSSH

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]
class TunelSSH():
    def __init__(self, ssh_address, ssh_port, ssh_username, ssh_password, remote_bind_address, remote_bind_port):
        self.server = SSHTunnelForwarder(ssh_address=(ssh_address, ssh_port), ssh_username=ssh_username, 
            ssh_password=ssh_password, remote_bind_address=(remote_bind_address, remote_bind_port))

    def Iniciar(self):
        self.server.start()
        return self.server.local_bind_port

    def Cerrar(self):
        self.server.stop()
开发者ID:procamora,项目名称:Librerias-Varias,代码行数:13,代码来源:ssh_forward.py

示例3: DatabaseWrapper

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]
class DatabaseWrapper(MysqlDatabaseWrapper):

    def __init__(self, *args, **kwargs):
        super(DatabaseWrapper, self).__init__(*args, **kwargs)
        self.tunnel = None

    def get_connection_params(self):
        kwargs = super(DatabaseWrapper, self).get_connection_params()
        host = kwargs['host']
        port = kwargs['port']
        config = self.settings_dict["TUNNEL_CONFIG"]
        config['remote_bind_address'] = (host, port)
        self.tunnel = SSHTunnelForwarder(**config)
        self.tunnel.daemon_forward_servers = True
        self.tunnel.daemon_transport = True
        self.tunnel.start()
        kwargs["host"] = '127.0.0.1'
        kwargs['port'] = self.tunnel.local_bind_port
        return kwargs

    def _close(self):
        super(DatabaseWrapper, self)._close()
        if self.tunnel is not None:
            self.tunnel.stop()
开发者ID:DrWrong,项目名称:django_extra,代码行数:26,代码来源:base.py

示例4: print

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]
        current_userID = participants_list[i]
        print("current userID - ",current_userID)
        df_temp_locations = df_user_location_list.loc[df_user_location_list['userID'] == current_userID] # location list of a particular user
        df_temp_locations = df_temp_locations.sort_values(['visit_times','spent_time'],ascending=[False,False])
        print(df_temp_locations)
        user_total_visits = df_temp_locations['visit_times'].sum()
        user_total_locations = len(df_temp_locations)
        print("user_total_visits - {0}, user_total_locations - {1}".format(user_total_visits,user_total_locations))

        var_D = 0
        for j in range(0,len(df_temp_locations)):
            var_p_j = (df_temp_locations.iloc[j]['visit_times'])/user_total_visits
            var_D = var_D - var_p_j * log(var_p_j,user_total_locations)
            print("var_D = ",var_D)

        var_L = 0
        k = user_total_locations//3
        for j in range(0,k):
            var_p_j = (df_temp_locations.iloc[j]['visit_times'])/user_total_visits
            var_L = var_L + var_p_j
            print("val_L = ",var_L)

        sql = "INSERT INTO user_location_diversity (userID,total_locations,location_diversity,location_loyalty) VALUES (" + str(
                current_userID) + "," + str(user_total_locations) + "," + str(
                var_D) + "," + str(var_L) + ");"
        print(sql)
        cursor.execute(sql)

    server.stop()

    print("End")
开发者ID:DonghoChoi,项目名称:ISB_Project,代码行数:33,代码来源:calculate_location_diversity.py

示例5: DB

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]
class DB(object):
    """
    Read: https://colinnewell.wordpress.com/2016/01/21/hand-coding-sql-with-psycopg2-for-odoo/
    """
    _db_connection = None
    _db_cur = None
    _server = None

    def __init__(self):
        db_config = Configuration.get_vicnode_db()
        ssh_intermediate = Configuration.get_ssh_tunnel_info()

        self._server = SSHTunnelForwarder(
            ((ssh_intermediate['host']), (int(ssh_intermediate['port']))),
            ssh_password=ssh_intermediate['ssh_password'],
            ssh_username=(ssh_intermediate['username']),
            ssh_pkey=(ssh_intermediate['private_key_file']),
            remote_bind_address=(db_config['host'], 5432),
            allow_agent=False
        )
        self._server.start()
        # we are about to bind to a 'local' server by means of an ssh tunnel
        # ssh tunnel: which will be seen as a local server...
        # so replace the loaded config host
        db_config['host'] = 'localhost'
        db_config['port'] = self._server.local_bind_port
        self._db_connection = psycopg2.connect(**db_config)
        self._db_cur = self._db_connection.cursor(
            cursor_factory=psycopg2.extras.RealDictCursor)
        self.test_connection()

    def __del__(self):
        self.close_connection()

    def close_connection(self):
        if self._server:
            logging.info("Closing the VicNode DB connection")
            # if the connection was not established, there will be no close()
            # attribute...
            self._db_connection.close()
            logging.info("Stopping the ssh tunnel")
            self._server.stop()
            logging.info("The VicNode DB connection is closed")
            self._server = None

    @staticmethod
    def get_product_code(product):
        products = settings.STORAGE_PRODUCT_CODES
        if product == COMPUTATIONAL:
            products = settings.COMPUTE_PRODUCT_CODES
        elif product == MARKET:
            products = settings.MARKET_PRODUCT_CODES
        elif product == VAULT:
            products = settings.VAULT_MARKET_CODES
        return products

    @connection_required
    def test_connection(self):
        self._db_cur.execute("SELECT * FROM applications_suborganization;")
        rows = self._db_cur.fetchall()
        # print(rows)

    @connection_required
    def get_allocated(self, day_date):
        """
        :param self:
        :param day_date:
        :return:
        """
        q_allocated = """
            SELECT
              sum(size),
              CASE
              WHEN storage_product_id IN %(compute)s
                THEN 'computational'
              WHEN storage_product_id IN %(market)s
                THEN 'market'
              ELSE 'vault' END AS product
            FROM applications_allocation
            WHERE storage_product_id IN %(all_types)s
                AND COALESCE(applications_allocation.creation_date, '2014-11-14' :: DATE) <
                      (%(day_date)s :: DATE + '1 day' :: INTERVAL)
            GROUP BY storage_product_id;
        """
        self._db_cur.execute(q_allocated, {
            'compute': settings.COMPUTE_PRODUCT_CODES,
            'market': settings.MARKET_PRODUCT_CODES,
            'all_types': settings.STORAGE_PRODUCT_CODES,
            'day_date': day_date
        })
        return self._db_cur.fetchall()

    @connection_required
    def get_allocated_by_faculty(self, day_date, product='all'):
        """
        :param product:
        :param self:
        :param day_date:
        :return:
        """
#.........这里部分代码省略.........
开发者ID:MartinPaulo,项目名称:ReportsAlpha,代码行数:103,代码来源:vicnode_db.py

示例6: __init__

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]
class DB:
    _db_connection = None
    _db_cur = None

    def __init__(self):
        ssh_tunnel = Credentials.ssh_tunnel
        db_config = Credentials.vicnode_db
        self._server = SSHTunnelForwarder(
            ((ssh_tunnel['host']), (int(ssh_tunnel['port']))),
            ssh_password=ssh_tunnel['ssh_password'],
            ssh_username=(ssh_tunnel['username']),
            ssh_pkey=(ssh_tunnel['private_key_file']),
            remote_bind_address=(db_config['host'], 5432),
            allow_agent=False
        )
        self._server.start()
        # we are about to bind to a 'local' server by means of an ssh tunnel
        # ssh tunnel: which will be seen as a local server...
        # so replace the loaded config host
        db_config['host'] = 'localhost'
        db_config['port'] = self._server.local_bind_port
        self._db_connection = psycopg2.connect(**db_config)
        self._db_cur = self._db_connection.cursor(
            cursor_factory=psycopg2.extras.RealDictCursor)
        self.test_connection()

    def __del__(self):
        self.close_connection()

    def close_connection(self):
        if self._server:
            self._db_connection.close()
            self._server.stop()
            self._server = None

    def test_connection(self):
        self._db_cur.execute("SELECT * FROM applications_suborganization;")
        rows = self._db_cur.fetchall()

    def get_contacts(self):
        """
        Returns: every project name and it's chief investigator (can be more
        than one?).
        """
        query = """
            SELECT
              collection.id,
              contacts_contact.email_address,
              contacts_contact.business_email_address
            FROM applications_project AS collection
              LEFT JOIN applications_custodian
                ON collection_id = collection.id
              LEFT JOIN contacts_contact
                ON applications_custodian.person_id = contacts_contact.id
            WHERE applications_custodian.role_id = 293
            ORDER BY id;
        """
        self._db_cur.execute(query)
        return self._db_cur.fetchall()

    def get_sub_organizations(self):
        """
        Returns: all the projects
        """
        query = """
            SELECT
              applications_request.project_id,
              contacts_organisation.short_name,
            CASE
              WHEN applications_suborganization.id = 1
                THEN 'ABP'
              WHEN applications_suborganization.id = 2
                THEN 'FBE'
              WHEN applications_suborganization.id = 3
                THEN 'FoA'
              WHEN applications_suborganization.id = 4
                THEN 'MGSE'
              WHEN applications_suborganization.id = 5
                THEN 'MSE'
              WHEN applications_suborganization.id = 6
                THEN 'MLS'
              WHEN applications_suborganization.id = 7
                THEN 'MDHS'
              WHEN applications_suborganization.id = 8
                THEN 'FoS'
              WHEN applications_suborganization.id = 9
                THEN 'VAS'
              WHEN applications_suborganization.id = 10
                THEN 'VCAMCM'
              WHEN applications_suborganization.id = 11
                THEN 'External'
              ELSE 'Unknown' END AS faculty
            FROM applications_request
              LEFT JOIN applications_suborganization
                ON applications_request.institution_faculty_id =
                   applications_suborganization.id
              LEFT JOIN contacts_organisation
                ON applications_request.institution_id = contacts_organisation.id
            WHERE -- institution_id = 2
              -- don't worry about this, as only UoM projects are assigned
#.........这里部分代码省略.........
开发者ID:MartinPaulo,项目名称:ReportsAlpha,代码行数:103,代码来源:bc_storage.py

示例7: CdhConfExtractor

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]
class CdhConfExtractor(object):
    def __init__(self, config):
        self._logger = logging.getLogger(__name__)
        self._hostname = config['machines']['cdh-launcher']['hostname']
        self._hostport = config['machines']['cdh-launcher']['hostport']
        self._username = config['machines']['cdh-launcher']['username']
        key_path = config['machines']['cdh-launcher']['key_filename']
        self._key = os.path.expanduser(key_path)
        self._key_password = config['machines']['cdh-launcher']['key_password']
        self._is_openstack = config['openstack_env']
        self._is_kerberos = config['kerberos_used']
        self._cdh_manager_ip = config['machines']['cdh-manager']['ip']
        self._cdh_manager_user = config['machines']['cdh-manager']['user']
        self._cdh_manager_sshtunnel_required = config['machines']['cdh-manager']['sshtunnel_required']
        self._cdh_manager_password = config['machines']['cdh-manager']['password']

    def __enter__(self):
        extractor = self
        try:
            if self._cdh_manager_sshtunnel_required:
                self._logger.info('Creating tunnel to CDH-Manager.')
                extractor.create_tunnel_to_cdh_manager()
                extractor.start_cdh_manager_tunneling()
                self._logger.info('Tunnel to CDH-Manager has been created.')
            else:
                self._logger.info('Connection to CDH-Manager host without ssh tunnel.')
                self._local_bind_address = self.extract_cdh_manager_host()
                self._local_bind_port = 7180
            return extractor
        except Exception as exc:
            self._logger.error('Cannot creating tunnel to CDH-Manager machine.')
            raise exc

    def __exit__(self, exc_type, exc_val, exc_tb):
        try:
            if self._cdh_manager_sshtunnel_required:
                self.stop_cdh_manager_tunneling()
                self._logger.info('Tunelling to CDH-Manager stopped.')
        except Exception as exc:
            self._logger.error('Cannot close tunnel to CDH-Manager machine.')
            raise exc

    # Cdh launcher methods
    def create_ssh_connection(self, hostname, username, key_filename, key_password):
        try:
            self._logger.info('Creating connection to remote host {0}.'.format(hostname))
            self.ssh_connection = paramiko.SSHClient()
            self.ssh_connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.ssh_connection.connect(hostname, username=username, key_filename=key_filename, password=key_password)
            self._logger.info('Connection to host {0} established.'.format(hostname))
        except Exception as exc:
            self._logger.error('Cannot creating connection to host {0} machine. Check your settings '
                               'in fetcher_config.yml file.'.format(hostname))
            raise exc

    def close_ssh_connection(self):
        try:
            self.ssh_connection.close()
            self._logger.info('Connection to remote host closed.')
        except Exception as exc:
            self._logger.error('Cannot close connection to the remote host.')
            raise exc

    def ssh_call_command(self, command, subcommands=None):
        self._logger.info('Calling remote command: "{0}" with subcommands "{1}"'.format(command, subcommands))
        ssh_in, ssh_out, ssh_err = self.ssh_connection.exec_command(command, get_pty=True)
        if subcommands != None:
            for subcommand in subcommands:
                ssh_in.write(subcommand + '\n')
                ssh_in.flush()
        return ssh_out.read() if ssh_out is not None else ssh_err.read()

    def extract_cdh_manager_host(self):
        self._logger.info('Extracting CDH-Manager address.')
        if self._cdh_manager_ip is None:
            self.create_ssh_connection(self._hostname, self._username, self._key, self._key_password)
            if self._is_openstack:
                ansible_ini = self.ssh_call_command('cat ansible-cdh/platform-ansible/inventory/cdh')
            else:
                ansible_ini = self.ssh_call_command('cat ansible-cdh/inventory/cdh')
            self._cdh_manager_ip = self._get_host_ip('cdh-manager', ansible_ini)
            self.close_ssh_connection()
        self._logger.info('CDH-Manager adress extracted: {}'.format(self._cdh_manager_ip))
        return self._cdh_manager_ip

    # Cdh manager methods
    def create_tunnel_to_cdh_manager(self, local_bind_address='localhost', local_bind_port=7180, remote_bind_port=7180):
        self._local_bind_address = local_bind_address
        self._local_bind_port = local_bind_port
        self.cdh_manager_tunnel = SSHTunnelForwarder(
            (self._hostname, self._hostport),
            ssh_username=self._username,
            local_bind_address=(local_bind_address, local_bind_port),
            remote_bind_address=(self.extract_cdh_manager_host(), remote_bind_port),
            ssh_private_key_password=self._key_password,
            ssh_private_key=self._key
        )

    def start_cdh_manager_tunneling(self):
        try:
#.........这里部分代码省略.........
开发者ID:butla,项目名称:apployer,代码行数:103,代码来源:cdh_utilities.py

示例8: Instance

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]
class Instance(object):
    """ Encapsulates the AWS EC2 instance to add additional functionality
    for running the MuMax3 simulations.
    """

    def __init__(self, aws_instance):
        self._instance = aws_instance
        self._forward = None

    def start(self):
        aws.start_instances(instance_ids=self.id)
        self.add_ready_tags()

    def add_ready_tags(self):
        self._instance.add_tag('mucloud', __version__)

    def stop(self):
        aws.stop_instances(instance_ids=[self.id])

    def terminate(self):
        # Toggle on delete on termination
        devices = ["%s=1" % dev for dev, bd in
                   self._instance.block_device_mapping.items()]
        self._instance.modify_attribute('BlockDeviceMapping', devices)
        aws.terminate_instances(instance_ids=[self.id])

    def is_up(self):
        return self._instance.state == u'running'

    def is_ready(self):
        return self.state == u'ready'

    def is_simulating(self):
        return self.state == u'simulating'

    def wait_for_boot(self, delay=10):
        """ Waits for an instance to boot up """
        log.info("Waiting for instance to boot...")
        while not self.is_up():
            sleep(delay)
            self._instance.update()
        sleep(delay)

    @property
    def directory(self):
        return "/home/%s" % config.get('EC2', 'User')

    def paths(self, local_input_file):
        basename = os.path.basename(local_input_file)
        directory = "/home/%s" % config.get('EC2', 'User')

        return {
            'local_input_file': local_input_file,
            'local_output_dir': local_input_file.replace(".mx3", ".out"),
            'input_file': "%s/simulations/%s" % (directory, basename),
            'output_dir': "%s/simulations/%s" % (
                directory,
                basename.replace(".mx3", ".out")
            ),
            'basename': basename,
            'log': "%s/log.txt" % directory,
            'finished': "%s/finished" % directory,
        }

    def connect(self):
        """ Connects to the instance through SSH and SFTP
        """
        log.info("Making secure connection to instance %s..." % self.id)
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(
            self.ip,
            username=config.get('EC2', 'User'),
            key_filename=config.get('EC2', 'PrivateKeyFile')
        )
        sftp = ssh.open_sftp()
        return ssh, sftp

    def run(self, local_input_file, port=PORT, detach=False):
        """ Run the mumax input file on a ready instance """

        if not self.is_ready():
            raise Exception("The instance %s is not ready to be run" % repr(
                            self))

        try:
            ssh, sftp = self.connect()
        except:
            log.error("Could not connect to remote server")
            return

        try:

            # Determine file paths
            paths = self.paths(local_input_file)

            self._instance.add_tags({
                'local_input_file': local_input_file,
                'port': port,
            })
#.........这里部分代码省略.........
开发者ID:ralph-group,项目名称:mucloud,代码行数:103,代码来源:mucloud.py

示例9: getRemoteDB

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]
	def getRemoteDB(self):
		# TODO: This needs to add new rows since the last update, rather than replace everything.
		'''
		# Get the local database last record timestamp
		conn = pymysql.connect(host='127.0.0.1', port=3306, user=self.config['localusername'], passwd=self.config['localpassword'], db=self.config['localdbname'])
		cur = conn.cursor()
		cur.execute('SELECT Time_uploaded_to_server FROM remotedata ORDER BY Time_uploaded_to_server ASC;')
		lts = cur.fetchone()
		lastTimestamp = lts[0]

		cur.close()
		conn.close()
		'''

		# The database query
		sql = """select enc.dateTime, enc.pushedToServerDateTime, enc.howFeeling, enc.takenMedsToday+0,
				 MAX(IF(Observation.question_id = "20140544-1bee-4d02-b764-d80102437adc", Observation.valueNumeric, NULL)) AS Nose,
				 MAX(IF(Observation.question_id = "22d7940f-eb30-42cd-b511-d0d65b89eec6", Observation.valueNumeric, NULL)) AS Eyes,
				 MAX(IF(Observation.question_id = "2cd9490f-d8ca-4f14-948a-1adcdf105fd0", Observation.valueNumeric, NULL)) AS Breathing,
				 demo.birthYear, demo.gender, demo.allergiesDivulged+0, demo.hayFever+0, demo.asthma+0, demo.otherAllergy+0,
				 demo.unknownAllergy+0, loc.latitude, loc.longitude, loc.accuracy, loc.whenObtained
				 from Encounter as enc inner join Observation on Observation.encounter_id = enc.id
				 inner join Question on Observation.question_id = Question.id join Demographics as demo on demo.id = enc.demographics_id
				 join LocationInfo as loc on loc.id = enc.locationInfo_id
				 group by enc.id;"""

		# Open SSH tunnel
		server = SSHTunnelForwarder(
			(self.config['remotehostname'], 1522),
			ssh_username=self.config['remoteusername'],
			ssh_password=self.config['remotepassword'],
			remote_bind_address=('127.0.0.1', 3306)
		)

		# Select data from remote database
		server.start()
		#print(server.local_bind_port)
		#print(server.tunnel_is_up)
		#print(server.local_is_up(('127.0.0.1', 3306)))

		if server.is_alive:
			print('Connection up...executing sql...')
			#print(os.system('mysql -h 127.0.0.1 --port='+str(server.local_bind_port)+' -u'+self.config['remoteusername']+' -p'))

			try:
				conn = pymysql.connect(host='127.0.0.1', port=server.local_bind_port, user=self.config['remoteusername'], passwd=self.config['remotedbpassword'], db=self.config['remotedbname'])
				cur = conn.cursor()
				cur.execute(sql)
				cur.close()
				conn.close()

			except pymysql.err.OperationalError:
				print('MySQL failed...exiting.')
				server.stop()
				sys.exit(0)

		# Close the ssh tunnel
		server.stop()

		# Update local database
		lconn = pymysql.connect(host='127.0.0.1', port=3306, user=self.config['localusername'], passwd=self.config['localpassword'], db=self.config['localdbname'], autocommit=True)
		lcur = lconn.cursor()
		lcur.execute('TRUNCATE TABLE remotedata;')

		rowcount = 0
		for row in cur:
			#print(row[0])
			sql = """INSERT INTO remotedata (id, Time_answered_on_phone, Time_uploaded_to_server, How_feeling, Taken_meds_today, Nose, Eyes, Breathing, Year_of_Birth, Gender, Optional_data_shared, hay_fever, asthma, other_allergy, unknown, latitude, longitude, accuracy, time_of_location_fix) VALUES ('', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}');
			""".format(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17])

			# Update the remotedata table
			lcur.execute(sql)

			# Update the row count
			rowcount = rowcount+1

		print('Rows processed: ', rowcount)

		# Done
		lcur.close()
		lconn.close()

		# DB sync complete, update the postcodes
		self.addPostcodesToDB()
开发者ID:robdunne-uom,项目名称:bb-data-viz,代码行数:86,代码来源:api.py

示例10: ServerManager

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]

#.........这里部分代码省略.........
            conn = self.connections[conn_info['conn_id']] = Connection(
                self, conn_info['conn_id'], conn_info['database'],
                conn_info['auto_reconnect'], conn_info['async_'],
                use_binary_placeholder=conn_info['use_binary_placeholder'],
                array_to_string=conn_info['array_to_string']
            )

            # only try to reconnect if connection was connected previously and
            # auto_reconnect is true.
            if conn_info['wasConnected'] and conn_info['auto_reconnect']:
                try:
                    # Check SSH Tunnel needs to be created
                    if self.use_ssh_tunnel == 1 and not self.tunnel_created:
                        status, error = self.create_ssh_tunnel(
                            data['tunnel_password'])

                        # Check SSH Tunnel is alive or not.
                        self.check_ssh_tunnel_alive()

                    conn.connect(
                        password=data['password'],
                        server_types=ServerType.types()
                    )
                    # This will also update wasConnected flag in connection so
                    # no need to update the flag manually.
                except Exception as e:
                    current_app.logger.exception(e)
                    self.connections.pop(conn_info['conn_id'])

    def release(self, database=None, conn_id=None, did=None):
        # Stop the SSH tunnel if release() function calls without
        # any parameter.
        if database is None and conn_id is None and did is None:
            self.stop_ssh_tunnel()

        if did is not None:
            if did in self.db_info and 'datname' in self.db_info[did]:
                database = self.db_info[did]['datname']
                if hasattr(str, 'decode') and \
                        not isinstance(database, unicode):
                    database = database.decode('utf-8')
                if database is None:
                    return False
            else:
                return False

        my_id = (u'CONN:{0}'.format(conn_id)) if conn_id is not None else \
            (u'DB:{0}'.format(database)) if database is not None else None

        if my_id is not None:
            if my_id in self.connections:
                self.connections[my_id]._release()
                del self.connections[my_id]
                if did is not None:
                    del self.db_info[did]

                if len(self.connections) == 0:
                    self.ver = None
                    self.sversion = None
                    self.server_type = None
                    self.server_cls = None
                    self.password = None

                self.update_session()

                return True
开发者ID:asheshv,项目名称:pgadmin4,代码行数:70,代码来源:server_manager.py

示例11: __init__

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]
class StorageDB:
    _db_connection = None
    _db_cur = None

    def __init__(self):
        ssh_tunnel = Credentials.ssh_tunnel
        db_config = Credentials.vicnode_db
        self._server = SSHTunnelForwarder(
            ((ssh_tunnel['host']), (int(ssh_tunnel['port']))),
            ssh_password=ssh_tunnel['ssh_password'],
            ssh_username=(ssh_tunnel['username']),
            ssh_pkey=(ssh_tunnel['private_key_file']),
            remote_bind_address=(db_config['host'], 5432),
            allow_agent=False
        )
        self._server.start()
        # we are about to bind to a 'local' server by means of an ssh tunnel
        # ssh tunnel: which will be seen as a local server...
        # so replace the loaded config host
        db_config['host'] = 'localhost'
        db_config['port'] = self._server.local_bind_port
        self._db_connection = psycopg2.connect(**db_config)
        self._db_cur = self._db_connection.cursor(
            cursor_factory=psycopg2.extras.RealDictCursor)
        self.test_connection()

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        self.close_connection()

    def __del__(self):
        self.close_connection()

    def close_connection(self):
        if self._server:
            self._db_connection.close()
            self._server.stop()
            self._server = None

    def test_connection(self):
        self._db_cur.execute("SELECT * FROM applications_suborganization;")
        rows = self._db_cur.fetchall()

    def get_contacts(self):
        """
        Returns: every project name and it's chief investigator (can be more
        than one).
        """
        query = """
            SELECT
              collection.id,
              contacts_contact.first_name,
              contacts_contact.last_name,
              contacts_contact.email_address,
              contacts_contact.business_email_address
            FROM applications_project AS collection
              LEFT JOIN applications_custodian
                ON collection_id = collection.id
              LEFT JOIN contacts_contact
                ON applications_custodian.person_id = contacts_contact.id
            WHERE applications_custodian.role_id = 293
            ORDER BY id;
        """
        self._db_cur.execute(query)
        return self._db_cur.fetchall()

    def get_sub_organizations(self):
        """
        Returns: all the projects that have a suborganization
        """
        query = """
            SELECT
              applications_allocation.collection_id AS project_id,
              CASE
              WHEN applications_suborganization.id = 1
                THEN 'ABP'
              WHEN applications_suborganization.id = 2
                THEN 'FBE'
              WHEN applications_suborganization.id = 3
                THEN 'FoA'
              WHEN applications_suborganization.id = 4
                THEN 'MGSE'
              WHEN applications_suborganization.id = 5
                THEN 'MSE'
              WHEN applications_suborganization.id = 6
                THEN 'MLS'
              WHEN applications_suborganization.id = 7
                THEN 'MDHS'
              WHEN applications_suborganization.id = 8
                THEN 'FoS'
              WHEN applications_suborganization.id = 9
                THEN 'VAS'
              WHEN applications_suborganization.id = 10
                THEN 'VCAMCM'
              WHEN applications_suborganization.id = 11
                THEN 'Services'
              ELSE 'Unknown' END AS faculty
            FROM applications_allocation
#.........这里部分代码省略.........
开发者ID:MartinPaulo,项目名称:ReportsAlpha,代码行数:103,代码来源:storage_yearly_usage.py

示例12: Device

# 需要导入模块: from sshtunnel import SSHTunnelForwarder [as 别名]
# 或者: from sshtunnel.SSHTunnelForwarder import stop [as 别名]
class Device(object):
    # ==================================================================================================================
    # FRAMEWORK ATTRIBUTES
    # ==================================================================================================================
    # Connection Parameters
    _ip, _port, _agent_port, _username, _password = '', '', '', '', ''
    _tools_local = None
    # Port Forwarding
    _frida_server = None
    _port_forward_ssh, _port_forward_agent = None, None
    # App specific
    _applist, _ios_version = None, None
    # Reference to External Objects
    ssh, agent = None, None
    app, installer = None, None
    local_op, remote_op = None, None
    printer = None
    # On-Device Paths
    TEMP_FOLDER = Constants.DEVICE_PATH_TEMP_FOLDER
    DEVICE_TOOLS = Constants.DEVICE_TOOLS

    # ==================================================================================================================
    # INIT
    # ==================================================================================================================
    def __init__(self, ip, port, agent_port, username, password, pub_key_auth, tools):
        # Setup params
        self._ip = ip
        self._port = port
        self._agent_port = agent_port
        self._username = username
        self._password = password
        self._pub_key_auth = bool(pub_key_auth)
        self._tools_local = tools
        # Init related objects
        self.app = App(self)
        self.local_op = LocalOperations()
        self.remote_op = RemoteOperations(self)
        self.printer = Printer()
        self.agent = NeedleAgent(self)

    # ==================================================================================================================
    # UTILS - USB
    # ==================================================================================================================
    def _portforward_usb_start(self):
        """Setup USB port forwarding with TCPRelay."""
        # Check if the user chose a valid port
        if str(self._port) == '22':
            raise Exception('Chosen port must be different from 22 in order to use USB over SSH')
        # Setup the forwarding
        self.printer.debug('Setting up USB port forwarding on port %s' % self._port)
        cmd = '{app} -t 22:{port}'.format(app=self._tools_local['TCPRELAY'], port=self._port)
        self._port_forward_ssh = self.local_op.command_subproc_start(cmd)

    def _portforward_usb_stop(self):
        """Stop USB port forwarding."""
        self.printer.debug('Stopping USB port forwarding')
        self.local_op.command_subproc_stop(self._port_forward_ssh)

    # ==================================================================================================================
    # UTILS - SSH
    # ==================================================================================================================
    def _connect_ssh(self):
        """Open a new SSH connection using Paramiko."""
        try:
            self.printer.verbose("[SSH] Connecting ({}:{})...".format(self._ip, self._port))
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(self._ip, port=self._port, username=self._username, password=self._password,
                        allow_agent=self._pub_key_auth, look_for_keys=self._pub_key_auth)
            self.printer.notify("[SSH] Connected ({}:{})".format(self._ip, self._port))
            return ssh
        except paramiko.AuthenticationException as e:
            raise Exception('Authentication failed when connecting to %s. %s: %s' % (self._ip, type(e).__name__, e.message))
        except paramiko.SSHException as e:
            raise Exception('Connection dropped. Please check your connection with the device, '
                            'and reload the module. %s: %s' % (type(e).__name__, e.message))
        except Exception as e:
            raise Exception('Could not open a connection to %s. %s - %s' % (self._ip, type(e).__name__, e.message))

    def _disconnect_ssh(self):
        """Close the SSH connection, if available."""
        self.printer.verbose("[SSH] Disconnecting...")
        if self.ssh:
            self.ssh.close()

    @Retry()
    def _exec_command_ssh(self, cmd, internal):
        """Execute a shell command on the device, then parse/print output."""
        def hotfix_67():
            # TODO: replace with a more long-term fix
            import time
            timeout = 30
            endtime = time.time() + timeout
            while not stdout.channel.eof_received:
                time.sleep(1)
                if time.time() > endtime:
                    stdout.channel.close()
                    break

        # Paramiko Exec Command
#.........这里部分代码省略.........
开发者ID:mwrlabs,项目名称:needle,代码行数:103,代码来源:device.py


注:本文中的sshtunnel.SSHTunnelForwarder.stop方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。