问题
I'm required to start a series of python scripts and/or other windows executables. Some of these require a Windows system, others require a Linux machine.
Currently there are designated machines to run the OS-dependent programs. So I know where I want to start which program.
Is there a way to start a python script (or a windows executable) from a python script, on the local network, on another computer (e.g. run 192.168.0.101:/dir/python_script_123.py
?
The script, which should then run various programs may then look something like this in pseudo code..
linuxip = 192.168.0.101
linuxparam = "required parameter"
winip = 192.168.0.201
winparam = "required parameter"
#option 1 (run all), 2(run linux only), 3(run windows only), 4(run local only)
option = 1
if option == 1:
magic_things.run("linuxip:/dir/linux_script.py" + linuxparam)
magic_things.run("winip:C:\\dir\\windows_prog.exe" + winparam)
subprocess.call(["/dir/local_script.py","parameter"])
subprocess.call(["/dir/another_local_script.py","parameter"])
elif option ==2:
[...]
回答1:
You need to connect to your server machine from your client. In case of the linux machine you could use SSH. see http://en.wikipedia.org/wiki/Secure_Shell
Assuming you have a ssh server
on the linux server running you could use the package paramiko (http://docs.paramiko.org/en/1.15/api/client.html) to connect to the machine and run your script there.
This could look something like this:
from paramiko.client import SSHClient
client = SSHClient()
client.load_system_host_keys()
client.connect('linuxip', username='your_user', password='very_secret')
stdin, stdout, stderr = client.exec_command('python /home/your_user/your/path/to/scripty.py')
However please note that it's not very secure to store passwords in scripts and it's probably better to use a public/private key authentication (See the wiki article).
The paramiko package also offers the option for an ssh server, so this might be a solution for your windows machine, but I am not very sure as I don't run any windows machines any more.
Hope this was helpful!
David
回答2:
install ipython
and ipython kernel
on the remote server, and ipython
and ipython kernel
on the local machine. Then you can connect to the remote server using the settings here: https://stackoverflow.com/a/48332182/4752883
and run any program that would run on the remote machine using subprocess
or the os
builtin libraries. Further this is OS independent, so it will work whether your client/server is linux
or Windows
or Mac
来源:https://stackoverflow.com/questions/27056430/start-a-process-on-another-computer-on-the-network