Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
linux_faq:execute_commands_on_windows_host_using_pywinrm [2019/08/26 07:54] – created admin | linux_faq:execute_commands_on_windows_host_using_pywinrm [2019/08/26 09:16] (current) – [TypeError: startswith first arg must be bytes...] admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | https:// | ||
+ | https:// | ||
+ | https:// | ||
+ | |||
+ | sudo apt-get install python-dev libkrb5-dev | ||
+ | pip install Kerberos pywinrm | ||
+ | |||
+ | < | ||
+ | |||
+ | import winrm | ||
+ | |||
+ | win_host = ' | ||
+ | win_user = ' | ||
+ | win_pass = ' | ||
+ | winrm_transport=' | ||
+ | |||
+ | s = winrm.Session(win_host, | ||
+ | r = s.run_cmd(' | ||
+ | print (r.std_out) | ||
+ | print (r.status_code) | ||
+ | |||
+ | |||
+ | ps_script = """ | ||
+ | Clear | ||
+ | $RAM = WmiObject Win32_ComputerSystem | ||
+ | $MB = 1048576 | ||
+ | |||
+ | " | ||
+ | |||
+ | r= s.run_ps(ps_script) | ||
+ | print (r.status_code) | ||
+ | print (r.std_out) | ||
+ | </ | ||
+ | | ||
+ | < | ||
+ | # | ||
+ | |||
+ | from winrm.protocol import Protocol | ||
+ | |||
+ | p = Protocol( | ||
+ | endpoint=' | ||
+ | # | ||
+ | transport=' | ||
+ | username=r' | ||
+ | password=' | ||
+ | server_cert_validation=' | ||
+ | shell_id = p.open_shell() | ||
+ | command_id = p.run_command(shell_id, | ||
+ | std_out, std_err, status_code = p.get_command_output(shell_id, | ||
+ | p.cleanup_command(shell_id, | ||
+ | p.close_shell(shell_id) | ||
+ | print (std_out) | ||
+ | print (status_code) | ||
+ | </ | ||
+ | |||
+ | ====== Ошибки ====== | ||
+ | ===== TypeError: startswith first arg must be bytes... ===== | ||
+ | При попытке запустить **PowerShell Script** на русскоязычной **Windows** получаем такое: | ||
+ | < | ||
+ | File " | ||
+ | r= s.run_ps(ps_script) | ||
+ | File "/ | ||
+ | rs.std_err = self._clean_error_msg(rs.std_err) | ||
+ | File "/ | ||
+ | if msg.startswith("#< | ||
+ | TypeError: startswith first arg must be bytes or a tuple of bytes, not str | ||
+ | </ | ||
+ | Обсуждение тут: https:// | ||
+ | Судя по всему - причина в каких-то проблемах с кодировкой в функции **_clean_error_msg(self, | ||
+ | В принципе, | ||
+ | В моей системе в файлике **/ | ||
+ | if len(rs.std_err): | ||
+ | # if there was an error message, clean it it up and make it human | ||
+ | # readable | ||
+ | rs.std_err = self._clean_error_msg(rs.std_err) | ||
+ | в функции **run_ps(self, | ||
+ | Однако, | ||
+ | |||
+ | |||
+ | |||