今天有個需求需要將linux和windows 進行打通,然后執行windows上面的python文件。
我們是通過Python執行調用執行windows上面的東西,windows上面也是Python文件。
具體參考:https://blog.csdn.net/Together_CZ/article/details/86623977#comments_12356187
這里值得注意的是,我們從linux向windows發送指令的時候,我們一定要能從linux ping的通我們本地的windows服務器。
如果ping 不同的話會出現下面這個錯誤:
Connection to 10.0.3.129 timed out. (connect timeout=30)')
如果你服務器能ping的通的話可能還會出現下面這個問題。
winrm.exceptions.InvalidCredentialsError: the specified credentials were rejected by the server
這個問題參考這個鏈接:https://www.jianshu.com/p/e473fbdf9854
我運行的代碼如下:
#!/usr/bin/python # coding:utf-8 from winrm.protocol import Protocol p = Protocol( endpoint='http://10.0.3.123:5985/wsman', transport='ntlm', username=r'用戶名', password='gxg123456', server_cert_validation='ignore') shell_id = p.open_shell() command_id = p.run_command(shell_id, b'python F:/softinstall/Python/pythonworkplace/test.py', ['/all']) std_out, std_err, status_code = p.get_command_output(shell_id, command_id) p.cleanup_command(shell_id, command_id) print(std_out, status_code) p.close_shell(shell_id)
執行出來的結果如下:
這樣我們在linux服務器上面 就能遠程執行我們windows上面的程序。至此問題得到解決。