Python paramiko.client() Examples
The following are 30
code examples of paramiko.client().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
paramiko
, or try the search function
.
Example #1
Source File: test_policy.py From adminset with GNU General Public License v2.0 | 6 votes |
def test_missing_host_key(self): client = paramiko.SSHClient() file1 = make_tests_data_path('known_hosts_example') file2 = make_tests_data_path('known_hosts_example2') filename = make_tests_data_path('known_hosts') copyfile(file1, filename) client.load_host_keys(filename) n1 = len(client._host_keys) autoadd = AutoAddPolicy() entry = paramiko.hostkeys.HostKeys(file2)._entries[0] hostname = entry.hostnames[0] key = entry.key autoadd.missing_host_key(client, hostname, key) self.assertEqual(len(client._host_keys), n1 + 1) self.assertEqual(paramiko.hostkeys.HostKeys(filename), client._host_keys) os.unlink(filename)
Example #2
Source File: test_policy.py From webssh with MIT License | 6 votes |
def test_missing_host_key(self): client = paramiko.SSHClient() file1 = make_tests_data_path('known_hosts_example') file2 = make_tests_data_path('known_hosts_example2') filename = make_tests_data_path('known_hosts') copyfile(file1, filename) client.load_host_keys(filename) n1 = len(client._host_keys) autoadd = AutoAddPolicy() entry = paramiko.hostkeys.HostKeys(file2)._entries[0] hostname = entry.hostnames[0] key = entry.key autoadd.missing_host_key(client, hostname, key) self.assertEqual(len(client._host_keys), n1 + 1) self.assertEqual(paramiko.hostkeys.HostKeys(filename), client._host_keys) os.unlink(filename)
Example #3
Source File: instance.py From flambe with MIT License | 6 votes |
def __init__(self, host: str, private_host: str, username: str, key: str, config: ConfigParser, debug: bool, use_public: bool = True) -> None: self.host = host self.private_host = private_host self.username = username self.key = key self.config = config self.fix_relpaths_in_config() self.use_public = use_public self.debug = debug # Uses only one ssh client per instance object self._cli: SSHClient = None
Example #4
Source File: remote_paramiko.py From cstar with Apache License 2.0 | 6 votes |
def run(self, argv): try: self._connect() cmd = " ".join(self.escape(s) for s in argv) stdin, stdout, stderr = self.client.exec_command(cmd) status, stdout_chunks, stderr_chunks = self._read_results(stdin, stdout, stderr) out = b''.join(stdout_chunks) error = b''.join(stderr_chunks) if status != 0: err("Command %s failed with status %d on host %s" % (cmd, status, self.hostname)) else: debug("Command %s succeeded on host %s, output was %s and %s" % (cmd, self.hostname, str(out, 'utf-8'), str(error, 'utf-8'))) return ExecutionResult(cmd, status, str(out, 'utf-8'), str(error, 'utf-8')) except (ConnectionResetError, paramiko.ssh_exception.SSHException): self.client = None raise BadSSHHost("SSH connection to host %s was reset" % (self.hostname,))
Example #5
Source File: remote_paramiko.py From cstar with Apache License 2.0 | 5 votes |
def _connect(self): if self.client: # Ensure underlying client is still a valid open connection try: stdin, stdout, stderr = self.client.exec_command(PING_COMMAND) except (ConnectionResetError, paramiko.ssh_exception.SSHException): # ConnectionResetError is raised when a connection was established but then broken # paramiko.ssh_exception.SSHException is raised if the connection was known to be broken self.client = None if not self.client: try: self.client = paramiko.client.SSHClient() pkey = None if self.ssh_identity_file != None: pkey = paramiko.RSAKey.from_private_key_file(self.ssh_identity_file, None) debug("Username : ", self.ssh_username) debug("Id file: ", self.ssh_identity_file) self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy()) self.client.connect(self.hostname, compress=True, username=self.ssh_username, password=self.ssh_password, pkey=pkey) # Enable keepalive to improve connection stability when running commands that return no output for # long periods of time. transport = self.client.get_transport() transport.set_keepalive(5) except: self.client = None raise BadSSHHost("Could not establish an SSH connection to host %s" % (self.hostname,))
Example #6
Source File: policy.py From adminset with GNU General Public License v2.0 | 5 votes |
def get_policy_dictionary(): dic = { k.lower(): v for k, v in vars(paramiko.client).items() if type(v) is type and issubclass(v, paramiko.client.MissingHostKeyPolicy) and v is not paramiko.client.MissingHostKeyPolicy } return dic
Example #7
Source File: policy.py From adminset with GNU General Public License v2.0 | 5 votes |
def check_policy_setting(policy_class, host_keys_settings): host_keys = host_keys_settings['host_keys'] host_keys_filename = host_keys_settings['host_keys_filename'] system_host_keys = host_keys_settings['system_host_keys'] if policy_class is paramiko.client.AutoAddPolicy: host_keys.save(host_keys_filename) # for permission test elif policy_class is paramiko.client.RejectPolicy: if not host_keys and not system_host_keys: raise ValueError( 'Reject policy could not be used without host keys.' )
Example #8
Source File: policy.py From adminset with GNU General Public License v2.0 | 5 votes |
def is_missing_host_key(self, client, hostname, key): k = client._system_host_keys.lookup(hostname) or \ client._host_keys.lookup(hostname) if k is None: return True host_key = k.get(key.get_name(), None) if host_key is None: return True if host_key != key: raise paramiko.BadHostKeyException(hostname, key, host_key)
Example #9
Source File: os_backend.py From thonny with MIT License | 5 votes |
def __init__(self, host, user, password, cwd, mp_executable, api_stubs_path): from paramiko.client import SSHClient self._host = host self._user = user self._password = password self._sftp = None self._client = SSHClient() self._client.load_system_host_keys() # TODO: does it get closed properly after process gets killed? self._client.connect(hostname=host, username=user, password=password) self._cwd = cwd super().__init__(mp_executable, api_stubs_path, cwd=cwd)
Example #10
Source File: __init__.py From thonny with MIT License | 5 votes |
def _connect_in_background(self, cmd_line_str): try: from paramiko.client import SSHClient except ImportError: self._show_error( "SSH connection requires an extra package -- 'paramiko'.\n" + "You can install it via 'Tools => Manage plug-ins' or via system package manager." ) return self._client = SSHClient() self._client.load_system_host_keys() self._client.connect(hostname=self._host, username=self._user, password=self._password) self._check_install_thonny_backend() env = { "THONNY_USER_DIR": "~/.config/Thonny", "THONNY_FRONTEND_SYS_PATH": "[]", } stdin, stdout, stderr = self._client.exec_command( cmd_line_str, bufsize=0, timeout=None, get_pty=False, environment=env ) self._proc = SshPopen(stdin, stdout, stderr) # setup asynchronous output listeners Thread(target=self._listen_stdout, args=(stdout,), daemon=True).start() Thread(target=self._listen_stderr, args=(stderr,), daemon=True).start() self._send_msg(ToplevelCommand("get_environment_info")) self._starting = False
Example #11
Source File: remote_paramiko.py From cstar with Apache License 2.0 | 5 votes |
def __init__(self, hostname, ssh_username=None, ssh_password=None, ssh_identity_file=None): if hasattr(hostname, "ip"): self.hostname = hostname.ip else: self.hostname = hostname if not self.hostname: raise NoHostsSpecified("No SSH host specified") self.ssh_username = ssh_username self.ssh_password = ssh_password self.ssh_identity_file = ssh_identity_file self.client = None
Example #12
Source File: test_policy.py From adminset with GNU General Public License v2.0 | 5 votes |
def test_is_missing_host_key(self): client = paramiko.SSHClient() file1 = make_tests_data_path('known_hosts_example') file2 = make_tests_data_path('known_hosts_example2') client.load_host_keys(file1) client.load_system_host_keys(file2) autoadd = AutoAddPolicy() for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0] key = entry.key self.assertIsNone( autoadd.is_missing_host_key(client, hostname, key) ) for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0] key = entry.key key.get_name = lambda: 'unknown' self.assertTrue( autoadd.is_missing_host_key(client, hostname, key) ) del key.get_name for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0][1:] key = entry.key self.assertTrue( autoadd.is_missing_host_key(client, hostname, key) ) file3 = make_tests_data_path('known_hosts_example3') entry = paramiko.hostkeys.HostKeys(file3)._entries[0] hostname = entry.hostnames[0] key = entry.key with self.assertRaises(paramiko.BadHostKeyException): autoadd.is_missing_host_key(client, hostname, key)
Example #13
Source File: remote_paramiko.py From cstar with Apache License 2.0 | 5 votes |
def run_job(self, file, jobid, timeout=None, env={}): try: self._connect() transport = self.client.get_transport() session = transport.open_session() paramiko.agent.AgentRequestHandler(session) dir = ".cstar/remote-jobs/" + jobid self.run(("mkdir", "-p", dir)) self.put_command(file, "%s/job" % (dir,)) # Manually insert environment into script, since passing env into exec_command leads to it being # ignored on most ssh servers. :-( for key in env: if _alnum_re.search(key): raise BadEnvironmentVariable(key) env_str = " ".join(key + "=" + self.escape(value) for key, value in env.items()) remote_script = resource_string('cstar.resources', 'scripts/remote_job.sh') wrapper = remote_script.decode("utf-8") % (env_str,) self.write_command(wrapper, "%s/wrapper" % (dir,)) cmd = """ cd %s nohup ./wrapper """ % (self.escape(dir),) stdin, stdout, stderr = self.client.exec_command(cmd, timeout=timeout) _, _, _ = self._read_results(stdin, stdout, stderr, timeout) real_output = self.read_file(dir + "/stdout") real_error = self.read_file(dir + "/stderr") real_status = int(self.read_file(dir + "/status")) return ExecutionResult(cmd, real_status, real_output, real_error) except (ConnectionResetError, paramiko.ssh_exception.SSHException): raise BadSSHHost("SSH connection to host %s was reset" % (self.hostname,))
Example #14
Source File: remote_paramiko.py From cstar with Apache License 2.0 | 5 votes |
def read_file(self, remotepath): self._connect() with self.client.open_sftp() as ftp_client: with ftp_client.file(remotepath, 'r') as f: return str(f.read(), 'utf-8')
Example #15
Source File: remote_paramiko.py From cstar with Apache License 2.0 | 5 votes |
def put_command(self, localpath, remotepath): self._connect() with self.client.open_sftp() as ftp_client: ftp_client.put(localpath, remotepath) ftp_client.chmod(remotepath, 0o755)
Example #16
Source File: remote_paramiko.py From cstar with Apache License 2.0 | 5 votes |
def write_command(self, definition, remotepath): self._connect() with self.client.open_sftp() as ftp_client: with ftp_client.open(remotepath, 'w') as f: f.write(definition) ftp_client.chmod(remotepath, 0o755)
Example #17
Source File: remote_paramiko.py From cstar with Apache License 2.0 | 5 votes |
def close(self): if self.client: self.client.close() self.client = None
Example #18
Source File: main.py From chain with Apache License 2.0 | 5 votes |
def get_policy_class(policy): origin_policy = policy policy = policy.lower() if not policy.endswith('policy'): policy += 'policy' dic = {k.lower(): v for k, v in vars(paramiko.client).items() if type(v) is type and issubclass(v, paramiko.client.MissingHostKeyPolicy)} try: cls = dic[policy] except KeyError: raise ValueError('Unknown policy {!r}'.format(origin_policy)) return cls
Example #19
Source File: instance.py From flambe with MIT License | 5 votes |
def _get_cli(self) -> paramiko.SSHClient: """Get an `SSHClient` in order to execute commands. This will cache an existing SSHClient to optimize resource. This is a private method and should only be used in this module. Returns ------- paramiko.SSHClient The client for latter use. Raises ------ SSHConnectingError In case opening an SSH connection fails. """ try: if (self._cli is None or self._cli.get_transport() is None or not self._cli.get_transport().is_active()): # Set cli in case it was not set or if it was closed cli = paramiko.SSHClient() cli.set_missing_host_key_policy(paramiko.AutoAddPolicy()) hostname = self.host if self.use_public else self.private_host cli.connect(hostname=hostname, username=self.username, key_filename=self.key, allow_agent=False, look_for_keys=False) self._cli = cli return self._cli except paramiko.ssh_exception.SSHException: raise errors.SSHConnectingError(f"Error opening SSH connection with {hostname}. " "Double check information provided in the secrets file")
Example #20
Source File: awsutils.py From astrobase with MIT License | 5 votes |
def delete_spot_fleet_cluster( spot_fleet_reqid, client=None, ): """ This deletes a spot-fleet cluster. Parameters ---------- spot_fleet_reqid : str The fleet request ID returned by `make_spot_fleet_cluster`. client : boto3.Client or None If None, this function will instantiate a new `boto3.Client` object to use in its operations. Alternatively, pass in an existing `boto3.Client` instance to re-use it here. Returns ------- Nothing. """ if not client: client = boto3.client('ec2') resp = client.cancel_spot_fleet_requests( SpotFleetRequestIds=[spot_fleet_reqid], TerminateInstances=True ) return resp
Example #21
Source File: awsutils.py From astrobase with MIT License | 5 votes |
def sqs_delete_queue(queue_url, client=None): """This deletes an SQS queue given its URL Parameters ---------- queue_url : str The SQS URL of the queue to delete. client : boto3.Client or None If None, this function will instantiate a new `boto3.Client` object to use in its operations. Alternatively, pass in an existing `boto3.Client` instance to re-use it here. Returns ------- bool True if the queue was deleted successfully. False otherwise. """ if not client: client = boto3.client('sqs') try: client.delete_queue(QueueUrl=queue_url) return True except Exception: LOGEXCEPTION('could not delete the specified queue: %s' % (queue_url,)) return False
Example #22
Source File: policy.py From webssh with MIT License | 5 votes |
def is_missing_host_key(self, client, hostname, key): k = client._system_host_keys.lookup(hostname) or \ client._host_keys.lookup(hostname) if k is None: return True host_key = k.get(key.get_name(), None) if host_key is None: return True if host_key != key: raise paramiko.BadHostKeyException(hostname, key, host_key)
Example #23
Source File: policy.py From webssh with MIT License | 5 votes |
def check_policy_setting(policy_class, host_keys_settings): host_keys = host_keys_settings['host_keys'] host_keys_filename = host_keys_settings['host_keys_filename'] system_host_keys = host_keys_settings['system_host_keys'] if policy_class is paramiko.client.AutoAddPolicy: host_keys.save(host_keys_filename) # for permission test elif policy_class is paramiko.client.RejectPolicy: if not host_keys and not system_host_keys: raise ValueError( 'Reject policy could not be used without host keys.' )
Example #24
Source File: policy.py From webssh with MIT License | 5 votes |
def get_policy_dictionary(): dic = { k.lower(): v for k, v in vars(paramiko.client).items() if type(v) is type and issubclass(v, paramiko.client.MissingHostKeyPolicy) and v is not paramiko.client.MissingHostKeyPolicy } return dic
Example #25
Source File: test_policy.py From webssh with MIT License | 5 votes |
def test_is_missing_host_key(self): client = paramiko.SSHClient() file1 = make_tests_data_path('known_hosts_example') file2 = make_tests_data_path('known_hosts_example2') client.load_host_keys(file1) client.load_system_host_keys(file2) autoadd = AutoAddPolicy() for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0] key = entry.key self.assertIsNone( autoadd.is_missing_host_key(client, hostname, key) ) for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0] key = entry.key key.get_name = lambda: 'unknown' self.assertTrue( autoadd.is_missing_host_key(client, hostname, key) ) del key.get_name for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0][1:] key = entry.key self.assertTrue( autoadd.is_missing_host_key(client, hostname, key) ) file3 = make_tests_data_path('known_hosts_example3') entry = paramiko.hostkeys.HostKeys(file3)._entries[0] hostname = entry.hostnames[0] key = entry.key with self.assertRaises(paramiko.BadHostKeyException): autoadd.is_missing_host_key(client, hostname, key)
Example #26
Source File: main.py From chain with Apache License 2.0 | 5 votes |
def get_application_settings(): base_dir = os.path.dirname(__file__) filename = os.path.join(base_dir, 'known_hosts') host_keys = get_host_keys(filename) system_host_keys = get_host_keys(os.path.expanduser('~/.ssh/known_hosts')) policy_class = get_policy_class(options.policy) logging.info(policy_class.__name__) if policy_class is paramiko.client.AutoAddPolicy: host_keys.save(filename) # for permission test host_keys._last_len = len(host_keys) tornado.ioloop.PeriodicCallback( lambda: save_host_keys(host_keys, filename), options.period * 1000 # milliseconds ).start() elif policy_class is paramiko.client.RejectPolicy: if not host_keys and not system_host_keys: raise ValueError('Empty known_hosts with reject policy?') settings = dict( template_path=os.path.join(base_dir, 'templates'), static_path=os.path.join(base_dir, 'static'), cookie_secret=uuid.uuid4().hex, xsrf_cookies=False, ##修改源代码的地方 host_keys=host_keys, system_host_keys=system_host_keys, policy=policy_class(), debug=options.debug ) return settings
Example #27
Source File: awsutils.py From astrobase with MIT License | 4 votes |
def sqs_create_queue(queue_name, options=None, client=None): """ This creates an SQS queue. Parameters ---------- queue_name : str The name of the queue to create. options : dict or None A dict of options indicate extra attributes the queue should have. See the SQS docs for details. If None, no custom attributes will be attached to the queue. client : boto3.Client or None If None, this function will instantiate a new `boto3.Client` object to use in its operations. Alternatively, pass in an existing `boto3.Client` instance to re-use it here. Returns ------- dict This returns a dict of the form:: {'url': SQS URL of the queue, 'name': name of the queue} """ if not client: client = boto3.client('sqs') try: if isinstance(options, dict): resp = client.create_queue(QueueName=queue_name, Attributes=options) else: resp = client.create_queue(QueueName=queue_name) if resp is not None: return {'url':resp['QueueUrl'], 'name':queue_name} else: LOGERROR('could not create the specified queue: %s with options: %s' % (queue_name, options)) return None except Exception: LOGEXCEPTION('could not create the specified queue: %s with options: %s' % (queue_name, options)) return None
Example #28
Source File: awsutils.py From astrobase with MIT License | 4 votes |
def s3_delete_file(bucket, filename, client=None, raiseonfail=False): """This deletes a file from S3. Parameters ---------- bucket : str The AWS S3 bucket to delete the file from. filename : str The full file name of the file to delete, including any prefixes. client : boto3.Client or None If None, this function will instantiate a new `boto3.Client` object to use in its operations. Alternatively, pass in an existing `boto3.Client` instance to re-use it here. raiseonfail : bool If True, will re-raise whatever Exception caused the operation to fail and break out immediately. Returns ------- str or None If the file was successfully deleted, will return the delete-marker (https://docs.aws.amazon.com/AmazonS3/latest/dev/DeleteMarker.html). If it wasn't, returns None """ if not client: client = boto3.client('s3') try: resp = client.delete_object(Bucket=bucket, Key=filename) if not resp: LOGERROR('could not delete file %s from bucket %s' % (filename, bucket)) else: return resp['DeleteMarker'] except Exception: LOGEXCEPTION('could not delete file %s from bucket %s' % (filename, bucket)) if raiseonfail: raise return None ######### ## SQS ## #########
Example #29
Source File: awsutils.py From astrobase with MIT License | 4 votes |
def s3_put_file(local_file, bucket, client=None, raiseonfail=False): """This uploads a file to S3. Parameters ---------- local_file : str Path to the file to upload to S3. bucket : str The AWS S3 bucket to upload the file to. client : boto3.Client or None If None, this function will instantiate a new `boto3.Client` object to use in its operations. Alternatively, pass in an existing `boto3.Client` instance to re-use it here. raiseonfail : bool If True, will re-raise whatever Exception caused the operation to fail and break out immediately. Returns ------- str or None If the file upload is successful, returns the s3:// URL of the uploaded file. If it failed, will return None. """ if not client: client = boto3.client('s3') try: client.upload_file(local_file, bucket, os.path.basename(local_file)) return 's3://%s/%s' % (bucket, os.path.basename(local_file)) except Exception: LOGEXCEPTION('could not upload %s to bucket: %s' % (local_file, bucket)) if raiseonfail: raise return None
Example #30
Source File: awsutils.py From astrobase with MIT License | 4 votes |
def s3_get_url(url, altexts=None, client=None, raiseonfail=False): """This gets a file from an S3 bucket based on its s3:// URL. Parameters ---------- url : str S3 URL to download. This should begin with 's3://'. altexts : None or list of str If not None, this is a list of alternate extensions to try for the file other than the one provided in `filename`. For example, to get anything that's an .sqlite where .sqlite.gz is expected, use altexts=[''] to strip the .gz. client : boto3.Client or None If None, this function will instantiate a new `boto3.Client` object to use in its operations. Alternatively, pass in an existing `boto3.Client` instance to re-use it here. raiseonfail : bool If True, will re-raise whatever Exception caused the operation to fail and break out immediately. Returns ------- str Path to the downloaded filename or None if the download was unsuccessful. The file will be downloaded into the current working directory and will have a filename == basename of the file on S3. """ bucket_item = url.replace('s3://','') bucket_item = bucket_item.split('/') bucket = bucket_item[0] filekey = '/'.join(bucket_item[1:]) return s3_get_file(bucket, filekey, bucket_item[-1], altexts=altexts, client=client, raiseonfail=raiseonfail)