运行从主机传递的python脚本

时间:2021-12-23 01:14:46

I am trying to run a python script on a remote computer via psexec. I am able to connect and run python.exe with the following:

我正在尝试通过psexec在远程计算机上运行python脚本。我能够连接和运行python。exe以下:

C:\test>psexec \\192.168.X.X -u domain\administrator -p password -i C:\Anaconda\python.exe

The path to python.exe is the path on the remote machine. This opens a python window on the remote machine - all good.

python之路。exe是远程机器上的路径。这将在远程机器上打开一个python窗口——一切正常。

I want to now pass a python script from the host machine to run on the remote. This script is on the host machine in C:\test\test.py. I tried

现在,我想从主机上传递一个python脚本,以便在远程上运行。此脚本位于C:\test\test.py中的主机上。我试着

psexec \\192.168.X.X -u domain\administrator -p password -i "C:\Anaconda\python.exe" -c C:\test\test.py

and get:

并获得:

C:\Anaconda\python.exe exited on 192.168.X.X with error code 1.

I also tried-c test.py without the full path, and got a similar error. My thought is the remote application cannot find C:\test\test.py. I want to be able to pass the script from the host machine.

我也tried-c测试。没有完整路径的py,也有类似的错误。我的想法是远程应用程序找不到C:\测试\test.py。我希望能够从主机上传递脚本。

Any help is much appreciated. Thanks.

非常感谢您的帮助。谢谢。

2 个解决方案

#1


0  

If the .py extension has been associated with the Python installation on the remote machine, you may be able to run your Python script by simply removing the Python executable from the command line:

如果.py扩展与远程机器上的Python安装相关联,您可以通过从命令行删除Python可执行文件来运行Python脚本:

psexec \\192.168.X.X -u domain\administrator -p password -i -c C:\test\test.py

Please note that I have not tried this as I don't presently have access to a remote machine, so I can't guarantee that it will work.

请注意,我没有尝试过这一点,因为我目前没有访问远程机器的权限,所以我不能保证它会工作。

The line

这条线

psexec \\192.168.X.X -u domain\administrator -p password -i "C:\Anaconda\python.exe" -c C:\test\test.py

may be trying to run the command "C:\Anaconda\python.exe" -c C:\test\test.py on the remote machine. In other words, Python may be interpreting the -c switch, rather than PsExec. The Python switch -c specifies some Python code to run, and of course a filename is not valid Python code:

可能正在尝试运行命令“C:\Anaconda\python”。exe”- C C:\ \测试。在远程机器上。换句话说,Python可能正在解释-c开关,而不是PsExec。Python交换机-c指定要运行的一些Python代码,当然文件名不是有效的Python代码:

C:\Users\Luke>python -c "print 2 + 2"
4

C:\Users\Luke>python -c C:\test\test.py
  File "<string>", line 1
    C:\test\test.py
     ^
SyntaxError: invalid syntax

C:\Users\Luke>echo %ERRORLEVEL%
1

#2


0  

Was able to access a python script on a shared drive from the remote computer and host, and so by copying to the share from the host and reading from the share on the remote machine i had a suitable workaround (the -i switch is not required).

能够从远程计算机和主机访问共享驱动器上的python脚本,因此通过从主机复制到共享并从远程机器上的共享读取共享,我有了一个合适的解决方案(不需要i开关)。

psexec \\remote_machine_name -u domain\user -p pswrd -i C:/Anaconda/python.exe \\server\share\test\test.py

Related: if you are running on windows and writing to a UNC path from a python script i.e test.py above, helpful path formatting help:

相关:如果您正在windows上运行,并从python脚本i写入UNC路径。e测试。以上是有用的路径格式化帮助:

python copy files to a network location on Windows without mapping a drive

python将文件复制到Windows上的网络位置,而不映射驱动器

#1


0  

If the .py extension has been associated with the Python installation on the remote machine, you may be able to run your Python script by simply removing the Python executable from the command line:

如果.py扩展与远程机器上的Python安装相关联,您可以通过从命令行删除Python可执行文件来运行Python脚本:

psexec \\192.168.X.X -u domain\administrator -p password -i -c C:\test\test.py

Please note that I have not tried this as I don't presently have access to a remote machine, so I can't guarantee that it will work.

请注意,我没有尝试过这一点,因为我目前没有访问远程机器的权限,所以我不能保证它会工作。

The line

这条线

psexec \\192.168.X.X -u domain\administrator -p password -i "C:\Anaconda\python.exe" -c C:\test\test.py

may be trying to run the command "C:\Anaconda\python.exe" -c C:\test\test.py on the remote machine. In other words, Python may be interpreting the -c switch, rather than PsExec. The Python switch -c specifies some Python code to run, and of course a filename is not valid Python code:

可能正在尝试运行命令“C:\Anaconda\python”。exe”- C C:\ \测试。在远程机器上。换句话说,Python可能正在解释-c开关,而不是PsExec。Python交换机-c指定要运行的一些Python代码,当然文件名不是有效的Python代码:

C:\Users\Luke>python -c "print 2 + 2"
4

C:\Users\Luke>python -c C:\test\test.py
  File "<string>", line 1
    C:\test\test.py
     ^
SyntaxError: invalid syntax

C:\Users\Luke>echo %ERRORLEVEL%
1

#2


0  

Was able to access a python script on a shared drive from the remote computer and host, and so by copying to the share from the host and reading from the share on the remote machine i had a suitable workaround (the -i switch is not required).

能够从远程计算机和主机访问共享驱动器上的python脚本,因此通过从主机复制到共享并从远程机器上的共享读取共享,我有了一个合适的解决方案(不需要i开关)。

psexec \\remote_machine_name -u domain\user -p pswrd -i C:/Anaconda/python.exe \\server\share\test\test.py

Related: if you are running on windows and writing to a UNC path from a python script i.e test.py above, helpful path formatting help:

相关:如果您正在windows上运行,并从python脚本i写入UNC路径。e测试。以上是有用的路径格式化帮助:

python copy files to a network location on Windows without mapping a drive

python将文件复制到Windows上的网络位置,而不映射驱动器