def execute_cmd(self, command, encoding='437', environment=None): """ Executes a command in a cmd shell and returns the stdout/stderr/rc of that process. This uses the raw WinRS layer and can be used to execute a traditional process. :param command: The command to execute :param encoding: The encoding of the output std buffers, this correlates to the codepage of the host and traditionally en-US is 437. This probably doesn't need to be modified unless you are running a different codepage on your host :param environment: A dictionary containing environment keys and values to set on the executing process. :return: A tuple of stdout: A unicode string of the stdout stderr: A unicode string of the stderr rc: The return code of the process Both stdout and stderr are returned from the server as a byte string, they are converted to a unicode string based on the encoding variable set """ log.info("Executing cmd process '%s'" % command) with WinRS(self.wsman, environment=environment) as shell: process = Process(shell, command) process.invoke() process.signal(SignalCode.CTRL_C) return to_unicode(process.stdout, encoding), \ to_unicode(process.stderr, encoding), process.rc
def test_winrs_unicode(self, wsman_conn): with WinRS(wsman_conn, codepage=65001) as shell: process = Process(shell, "powershell.exe", [u"Write-Host こんにちは"]) process.invoke() process.signal(SignalCode.CTRL_C) assert process.rc == 0 assert process.stdout.decode('utf-8') == u"こんにちは\n" assert process.stderr == b""
def test_winrs_standard(self, wsman_conn): with WinRS(wsman_conn) as shell: process = Process(shell, "cmd.exe", ["/c", "echo", "hi"]) process.invoke() process.signal(SignalCode.CTRL_C) assert process.rc == 0 assert process.stdout == b"hi\r\n" assert process.stderr == b""
def test_winrs_noprofile(self, wsman_conn): with WinRS(wsman_conn, no_profile=True) as shell: process = Process(shell, "cmd.exe", ["/c", "set"]) process.invoke() process.signal(SignalCode.CTRL_C) assert process.rc == 0 assert "USERPROFILE=C:\\Users\\Default" in \ process.stdout.decode('utf-8').splitlines() assert process.stderr == b""
def test_winrs_stderr_rc(self, wsman_conn): with WinRS(wsman_conn) as shell: process = Process(shell, "cmd.exe", ["/c echo out && echo err>&2 && exit 1"]) process.invoke() process.signal(SignalCode.CTRL_C) assert process.rc == 1 assert process.stdout == b"out \r\n" assert process.stderr == b"err \r\n"
def test_winrs(self, functional_transports): for wsman in functional_transports: with wsman, WinRS(wsman) as shell: process = Process(shell, "echo", ["hi"]) process.invoke() process.signal(SignalCode.CTRL_C) assert process.rc == 0 assert process.stdout == b"hi\r\n" assert process.stderr == b""
def test_winrs_operation_timeout(self, wsman_conn): wsman_conn.operation_timeout = 10 with WinRS(wsman_conn) as shell: process = Process( shell, "powershell.exe", ["Write-Host hi; Start-Sleep 30; Write-Host hi again"]) process.invoke() process.signal(SignalCode.CTRL_C) assert process.rc == 0 assert process.stdout == b"hi\nhi again\n" assert process.stderr == b""
def test_winrs_extra_opts(self, wsman_conn): with WinRS(wsman_conn, name="shell 1", lifetime=60, idle_time_out=60, working_directory="C:\\Windows") as shell: assert shell.name == "shell 1" assert shell.lifetime == 60 assert shell.idle_time_out == "PT60.000S" assert shell.working_directory == "C:\\Windows" process = Process(shell, "powershell.exe", ["(pwd).Path"]) process.invoke() process.signal(SignalCode.CTRL_C) assert process.rc == 0 assert process.stdout == b"C:\\Windows\r\n" assert process.stderr == b""
def test_winrs_environment(self, wsman_conn): complex_chars = r'_-(){}[]<>*+-/\?"' '!@#$^&|;:i,.`~0' env_block = OrderedDict([ ('env1', 'var1'), (1234, 5678), (complex_chars, complex_chars), ]) with WinRS(wsman_conn, environment=env_block) as shell: process = Process(shell, "cmd.exe", ["/c", "set"]) process.invoke() process.signal(SignalCode.CTRL_C) env_list = process.stdout.decode('utf-8').splitlines() assert process.rc == 0 assert "env1=var1" in env_list assert "1234=5678" in env_list assert "%s=%s" % (complex_chars, complex_chars) in env_list assert process.stderr == b""
def test_winrs_no_cmd_shell(self, wsman_conn): with WinRS(wsman_conn) as shell: process = Process(shell, "powershell.exe", ["Write-Host", "hi"], no_shell=True) # this will fail as you need to provide the full path when not # running in cmd shell with pytest.raises(WSManFaultError) as exc: process.invoke() assert exc.value.provider_fault == "The system cannot find the file specified." assert exc.value.code == 2147942402 # fix the execute path and invoke again process.executable = r"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" process.invoke() process.signal(SignalCode.CTRL_C) assert process.rc == 0 assert process.stdout == b"hi\n" assert process.stderr == b""
def get_winRS(config): errors = 0 results = [] try: ssl = config['protocol'].split('/')[1] except Exception as e: ssl = "" for member in config['members'].split(','): res = "" try: if ssl: client = WSMan(member, ssl=True, auth="ntlm", cert_validation=False, connection_timeout=3, username=config['user'], password=config['password']) else: client = WSMan(member, ssl=False, auth="ntlm", cert_validation=False, connection_timeout=3, username=config['user'], password=config['password']) with WinRS(client) as shell: process = Process(shell, REMCMD) print(process) stdout, stderr, _rc = process.invoke() if "decode" in dir(stdout): res = stdout.decode() err = stderr.decode() else: res = stdout err = stderr if err: print("get_winRS: {} -> err: {}".format(config, err), file=sys.stderr) errors += 1 except Exception as e: print("get_winRS: Connect to {} failed: {}".format( member, e.args[0]), file=sys.stderr) errors += 1 results.append(res) return errors, config, results