Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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": ""}
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
#! /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")
Ejemplo n.º 5
0
# the replace will change the linux line feed to the
# windows carriage return, line feed
part_2 = """
"@ | %{ $_.Replace("`n","`r`n") }
$stream.WriteLine($s)
$stream.close()"""

# join the beginning of the powershell script with the
# text file and end of the ps script
script = part_1 + text_file + part_2

# base64 encode, utf16 little endian. required for windows
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.decode("utf-8")))
print("STDERR: %s"%(stderr.decode("utf-8")))

# print the file
command_id = conn.run_command(shell_id, "type test.txt")
stdout, stderr, return_code = conn.get_command_output(shell_id, command_id)
conn.cleanup_command(shell_id, command_id)
print("STDOUT: %s"%(stdout.decode("utf-8")))
print("STDERR: %s"%(stderr.decode("utf-8")))

# delete the file
command_id = conn.run_command(shell_id, "del test.txt")
stdout, stderr, return_code = conn.get_command_output(shell_id, command_id)