关于通过linux访问windows执行windows环境下的Python文件的相关操作。

今天有个需求需要将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上面的程序。至此问题得到解决。

 

posted on 2020-05-28 14:40  gxg123  阅读(947)  评论(0编辑  收藏  举报

导航