本文整理汇总了Python中winrm.Session方法的典型用法代码示例。如果您正苦于以下问题:Python winrm.Session方法的具体用法?Python winrm.Session怎么用?Python winrm.Session使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类winrm
的用法示例。
在下文中一共展示了winrm.Session方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ntlm
# 需要导入模块: import winrm [as 别名]
# 或者: from winrm import Session [as 别名]
def ntlm(action, host_ip, powershell_script, username, password, port):
# Adds needed https and port number to host IP
action.logger.info('Running a NTLM connection')
host_connection = 'https://{host_ip}:{port}/wsman'.format(host_ip=host_ip, port=port)
action.logger.debug('Host Connection: ' + host_connection)
action.logger.debug('PowerShell script: ' + powershell_script)
powershell_session = winrm.Session(host_connection, auth=(username, password), transport='ntlm')
# Forces the Protocol to not fail with self signed certs
p = winrm.Protocol(endpoint=host_connection,
transport='ntlm',
username=username,
password=password,
server_cert_validation='ignore',
message_encryption='auto')
powershell_session.protocol = p
run_script = powershell_session.run_ps(powershell_script)
exit_code = run_script.status_code
error_value = run_script.std_err
output = run_script.std_out
try:
error_value = error_value.decode('utf-8')
except AttributeError:
pass
output = output.decode('utf-8')
if exit_code != 0:
action.logger.error(error_value)
raise Exception('An error occurred in the PowerShell script, see logging for more info')
return {'output': output, 'stderr': error_value}
示例2: credssp
# 需要导入模块: import winrm [as 别名]
# 或者: from winrm import Session [as 别名]
def credssp(action, host_ip, powershell_script, username, password, port):
# Adds needed https and port number to host IP
action.logger.info('Running a CredSSP connection')
host_connection = 'https://{host_ip}:{port}/wsman'.format(host_ip=host_ip, port=port)
action.logger.debug('Host Connection: ' + host_connection)
action.logger.debug('PowerShell script: ' + powershell_script)
powershell_session = winrm.Session(host_connection, auth=(username, password), transport='credssp')
# Forces the Protocol to not fail with self signed certs
p = winrm.Protocol(endpoint=host_connection,
transport='credssp',
username=username,
password=password,
server_cert_validation='ignore',
message_encryption='auto')
powershell_session.protocol = p
run_script = powershell_session.run_ps(powershell_script)
exit_code = run_script.status_code
error_value = run_script.std_err
output = run_script.std_out
try:
error_value = error_value.decode('utf-8')
except AttributeError:
pass
output = output.decode('utf-8')
if exit_code != 0:
action.logger.error(error_value)
raise Exception('An error occurred in the PowerShell script, see logging for more info')
return {'output': output, 'stderr': error_value}
示例3: __init__
# 需要导入模块: import winrm [as 别名]
# 或者: from winrm import Session [as 别名]
def __init__(self, **kwargs):
super(SCVMMSystem, self).__init__(**kwargs)
self.host = kwargs["hostname"]
self.port = kwargs.get("winrm_port", 5985)
self.scheme = kwargs.get("winrm_scheme", "http")
self.winrm_validate_ssl_cert = kwargs.get("winrm_validate_ssl_cert", False)
self.user = kwargs["username"]
self.password = kwargs["password"]
self.domain = kwargs["domain"]
self.provisioning = kwargs["provisioning"]
self.api = winrm.Session(
'{scheme}://{host}:{port}'.format(scheme=self.scheme, host=self.host, port=self.port),
auth=(self.user, self.password),
server_cert_validation='validate' if self.winrm_validate_ssl_cert else 'ignore',
)
示例4: winrm_login
# 需要导入模块: import winrm [as 别名]
# 或者: from winrm import Session [as 别名]
def winrm_login(self):
self.con = winrm.Session(self.endpoint,
auth=('{}\\{}'.format(self.args.domain, self.args.user), self.args.passwd),
transport='ntlm',
server_cert_validation='ignore')
示例5: connect
# 需要导入模块: import winrm [as 别名]
# 或者: from winrm import Session [as 别名]
def connect(state, host):
'''
Connect to a single host. Returns the winrm Session if successful.
'''
kwargs = _make_winrm_kwargs(state, host)
logger.debug('Connecting to: {0} ({1})'.format(host.name, kwargs))
# Hostname can be provided via winrm config (alias), data, or the hosts name
hostname = kwargs.pop(
'hostname',
host.data.winrm_hostname or host.name,
)
logger.debug('winrm_username:{} winrm_password:{} '
'winrm_port:{}'.format(host.data.winrm_username, host.data.winrm_password,
host.data.winrm_port))
try:
# Create new session
host_and_port = '{}:{}'.format(hostname, host.data.winrm_port)
logger.debug('host_and_port: {}'.format(host_and_port))
session = winrm.Session(
host_and_port,
auth=(
host.data.winrm_username,
host.data.winrm_password,
),
)
return session
# TODO: add exceptions here
except Exception as e:
auth_kwargs = {}
for key, value in kwargs.items():
if key in ('username', 'password'):
auth_kwargs[key] = value
continue
auth_args = ', '.join(
'{0}={1}'.format(key, value)
for key, value in auth_kwargs.items()
)
logger.debug(str(e))
_raise_connect_error(host, 'Authentication error', auth_args)
示例6: RemoteCommand
# 需要导入模块: import winrm [as 别名]
# 或者: from winrm import Session [as 别名]
def RemoteCommand(self, command, should_log=False, ignore_failure=False,
suppress_warning=False, timeout=None):
"""Runs a powershell command on the VM.
Args:
command: A valid powershell command.
should_log: A boolean indicating whether the command result should be
logged at the info level. Even if it is false, the results will
still be logged at the debug level.
ignore_failure: Ignore any failure if set to true.
suppress_warning: Suppress the result logging from IssueCommand when the
return code is non-zero.
timeout: Float. A timeout in seconds for the command. If None is passed,
no timeout is applied. Timeout kills the winrm session which then
kills the process being executed.
Returns:
A tuple of stdout and stderr from running the command.
Raises:
RemoteCommandError: If there was a problem issuing the command or the
command timed out.
"""
logging.info('Running command on %s: %s', self, command)
s = winrm.Session(
'https://%s:%s' % (self.GetConnectionIp(), self.winrm_port),
auth=(self.user_name, self.password),
server_cert_validation='ignore')
encoded_command = six.ensure_str(
base64.b64encode(command.encode('utf_16_le')))
@timeout_decorator.timeout(timeout, use_signals=False,
timeout_exception=errors.VirtualMachine.
RemoteCommandError)
def run_command():
return s.run_cmd('powershell -encodedcommand %s' % encoded_command)
r = run_command()
retcode, stdout, stderr = r.status_code, six.ensure_str(
r.std_out), six.ensure_str(r.std_err)
debug_text = ('Ran %s on %s. Return code (%s).\nSTDOUT: %s\nSTDERR: %s' %
(command, self, retcode, stdout, stderr))
if should_log or (retcode and not suppress_warning):
logging.info(debug_text)
else:
logging.debug(debug_text)
if retcode and not ignore_failure:
error_text = ('Got non-zero return code (%s) executing %s\n'
'STDOUT: %sSTDERR: %s' %
(retcode, command, stdout, stderr))
raise errors.VirtualMachine.RemoteCommandError(error_text)
return stdout, stderr