def create_remote_script(): global conn, shell_id, command_id, stdout, stderr, return_code endpoint = "%s://%s:%s/wsman" % (protocol, address, port) conn = Protocol(endpoint=endpoint, transport=transport, username=username, password=password) shell_id = conn.open_shell() # read the content file to var with open(file_path, "r") as script_file: script_content = script_file.read() # the second part of script (this is)_ the script we want create text_file = script_content # first part of the script to create part_1 = """$stream = [System.IO.StreamWriter] "%s%s" $s = @" """ % (PS_path, file_name) # the last part of the script part_2 = """ "@ | %{ $_.Replace("`n","`r`n") } $stream.WriteLine($s) $stream.close()""" script = part_1 + text_file + part_2 encoded_script = base64.b64encode(script.encode("utf_16_le")) # send the script to powershell, tell it the script is encoded command_id = conn.run_command(shell_id, "powershell -encodedcommand %s" % (encoded_script)) stdout, stderr, return_code = conn.get_command_output(shell_id, command_id) conn.cleanup_command(shell_id, command_id) print "STDOUT: %s" % (stdout) print "STDERR: %s" % (stderr)
def create_connection(protocol, address, port, password, username): global conn, shell_id, command_id, stdout, stderr, return_code endpoint = "%s://%s:%s/wsman" % (protocol, address, port) conn = Protocol(endpoint=endpoint, transport=transport, username=username, password=password) shell_id = conn.open_shell() return shell_id
def test_winrm_connection(target, port, user, password): protocol = Protocol( endpoint='https://{target}:{port}/wsman'.format(target=target, port=port), transport='ntlm', username=user, password=password, server_cert_validation='ignore') try: shell_id = protocol.open_shell() command_id = protocol.run_command(shell_id, 'whoami') std_out, std_err, status_code = protocol.get_command_output(shell_id, command_id) protocol.cleanup_command(shell_id, command_id) protocol.close_shell(shell_id) return {"msg":"Connection succeed.", "error": "", "output_command": std_out, "status_code": status_code} except Exception: return {"msg":"Connection failed.", "error": sys.exc_info()[1] , "output_command": "", "status_code": ""}
def get_connection(): # address = "localhost" # transport = "plaintext" # username = "******" # password = "******" # protocol = "http" # port = 5985 # endpoint = "%s://%s:%s/wsman" % (protocol, address, port) address = settings.address transport = settings.transport username = settings.username password = settings.password protocol = settings.protocol port = settings.port endpoint = settings.endpoint conn = Protocol(endpoint=endpoint, transport=transport, username=username, password=password) shell_id = conn.open_shell() return conn, shell_id
def run_cmd_winrm(cmd: str) -> Response: """ Run batch script using winrm client. Args: cmd: batch script to run. Returns: Response object containing stderr, stdout and exit_status. """ client = Protocol(endpoint='http://{}:5985/wsman'.format(config['host']), transport='ntlm', username='******'.format(config['domain'], config['user']), password=config['pass'], server_cert_validation='ignore') shell_id = client.open_shell() command_id = client.run_command(shell_id, cmd) rs = Response(client.get_command_output(shell_id, command_id)) client.cleanup_command(shell_id, command_id) client.close_shell(shell_id) return rs
#! /usr/bin/python from winrm import Session, Protocol print("Starting PyWinRM Script") host = input("hostname of windows with port: ") user = input("username of windows: ") password = input("password of windows: ") print("Running High Level API Test") s = Session(host, auth=(user, password)) r = s.run_cmd('ipconfig', ['/all']) print(r.std_out, r.std_err) print("Running Low Level API Test") p = Protocol(endpoint='http://' + host + '/wsman', transport='ntlm', username=user, password=password, server_cert_validation='ignore') shell_id = p.open_shell() command_id = p.run_command(shell_id, 'ipconfig', ['/all']) std_out, std_err, status_code = p.get_command_output(shell_id, command_id) p.cleanup_command(shell_id, command_id) p.close_shell(shell_id) print(std_out, std_err, status_code) print("Stopping PyWinRM Script")
from winrm import Protocol import base64 import sys address = "127.0.0.1" transport = "plaintext" username = "******" password = "******" protocol = "http" port = 55985 endpoint = "%s://%s:%s/wsman" % (protocol, address, port) conn = Protocol(endpoint=endpoint, transport=transport, username=username, password=password) shell_id = conn.open_shell() # the text file we want to send # this could be populated by reading a file from disk instead # has some special characters, just to prove they won't be a problem text_file = """this is a multiline file that contains special characters such as "blah" '#@$*&&($} that will be written onto the windows box""" # first part of the powershell script # streamwriter is the fastest and most efficient way to write a file # I have found # notice the @", this is like a "here document" in linux