def run(client, module): # ps_script = module.get('ps') args = module.get('args') if args and args.has_key('chdir'): base_dir = args['chdir'] ps_script = 'cd {base_dir}; {ps}'.format(base_dir=base_dir, ps=module.get('ps')) else: ps_script = module.get('ps') try: channel = client.run_ps(ps_script, codepage=936) except ConnectionError, err: logging.error(err) raise WinRmNoValidConnectionsError
def __init__(self, remote_config, parser=None): Executor.__init__(self, remote_config, parser) self.client = SSHClient() self.client.set_missing_host_key_policy(AutoAddPolicy()) self.client.load_system_host_keys() try: if remote_config.ssh_key: if path.isfile(remote_config.ssh_key): self.pKeyConnect(remote_config) else: self.passConnect(remote_config) except NoValidConnectionsError, err: logging.error(err.args[1].decode('gbk')) raise SSHNoValidConnectionsError
def run(client, module): # ps_script = module.get('ps') args = module.get('args') if args and args.has_key('chdir'): base_dir = args['chdir'] ps_script = 'cd {base_dir}; {ps}'.format(base_dir=base_dir, ps=module.get('ps')) else: ps_script = module.get('ps') try: channel = client.run_ps(ps_script, codepage=936) except ConnectionError, err: logging.error(err) raise WinRmNoValidConnectionsError except InvalidCredentialsError, err: logging.error(err) raise WinRmAuthenticationException else: stdout, stderr = _stdout(), _stderr() stdout.channel.recv_exit_status = lambda: channel.status_code stdout.read = change_read_encoding(channel.std_out) stdout.readlines = change_readlines_encoding(stdout.read) stderr.read = change_read_encoding(channel.std_err) stderr.readlines = change_readlines_encoding(stderr.read) return stdout, stderr class _output(object): def read(self): pass
self.client.set_missing_host_key_policy(AutoAddPolicy()) self.client.load_system_host_keys() try: if remote_config.ssh_key: if path.isfile(remote_config.ssh_key): self.pKeyConnect(remote_config) else: self.passConnect(remote_config) except NoValidConnectionsError, err: logging.error(err.args[1].decode('gbk')) raise SSHNoValidConnectionsError except AuthenticationException, err: logging.error(err.args[1].decode('gbk')) raise SSHAuthenticationException except Exception, err: logging.error(err.args[1].decode('gbk')) raise SSHException def pKeyConnect(self, ssh_config): try: pKey = RSAKey.from_private_key_file(filename=ssh_config.ssh_key) except PasswordRequiredException: if ssh_config.ssh_key_pass: pKey = RSAKey.from_private_key_file( filename=ssh_config.ssh_key, password=ssh_config.ssh_key_pass) else: err_msg = 'Fail to Load RSAKey({}), make sure password for key is correct.' \ .format(ssh_config.ssh_key) logging.warning(err_msg) raise ImportRSAkeyFaild(err_msg)