简体   繁体   中英

how to execute remote python to open a web on remote machine by using a command on a local machine

I've been searching on net for a long time but I still don't get the answer.

Let me give an examle to describe my question more clearly:
machine A(local) is now conneted with machine B(remote).
ALL I WANT TO DO is to :

run a command on A(local),then stop and wait ,and do nothing,and then,a web page is opened on B(remote) automatically.

PS this python program is stored on machine B.

Here's what I've achived by now:

This is my python program named test.py,and it is stored on B under /home/pi/Documents:

import webbrowser
webbrowser.open('http://www.google.com')

On A,I used command:

ssh <username of B>@<ip of B> python /home/pi/Documents/test.py

After running the above command on A,there is no errors on A but also no action on B.

if I change the command into creating a file on B or sudo reboot ,then after running this command there will be a file on B created or B is shut down successfully.

if I change the python program into printing something,like:

print("hello from B")

the content is magically printed on A's terminal.

It seems this command does not work well if I want to open a web on B or print somthing on B. Can anyone help me with this or is there any other way to accomplish it?

helpless..

Someone has any ideas please??? Thanks in advance!

One of the simplest solutions is to use redirect of stdin

$ ssh pi@B python <<EOF
> print "Hello World from B"
> EOF
Hello World from B
$ 

However, if the script is quite big, it is better to copy py file to server B and then call ssh with the file name as @Eliethesaiyan suggested.

$ scp  X.py pi@B:/home/pi/
X.py           100%   26     0.0KB/s   00:00    
$ ssh pi@B python X.py
Hello World from B
$ 

Assuming B is a Linux or Unix-like system, you have a DISPLAY problem. The webbrowser module locates a browser on the machine, and tries to open it on current display. It works when you launch it localy, but a ssh session has by default no configured display, so any attempt to launch a XWindow (GUI) application will fail.

The rationale behind it is that the -X and -Y flags of the ssh command allow to pass the client display to the server and open the window on the local screen (in your example on A). So if the permissions of the X servers on A and B are compatible, you could try:

A$ ssh -Y <username of B>@<ip of B>       # open an interactive shell on B
B$ echo $DISPLAY                          # control the DISPLAY env variable
-> localhost:10.0                         # common value for ssh transported DISPLAY
B$ python /home/pi/Documents/test.py      # open the window on A

To force the opening on B, you could set the DISPLAY to localhost:0.0 (primary XWindow)

A$ ssh ssh <username of B>@<ip of B>   # open an interactive shell on B
B$ DISPLAY = localhost:0.0             # sets the DISPLAY env variable
B$ export DISPLAY
B$ python /home/pi/Documents/test.py      # open the window on B

You might need to tweek authorization of the XWindow servers (or use the awful xhost + ) on A and/or B to make the above examples work

Once you have been able to successfully open a window on the proper screen, you will just have to set the DISPLAY environment variable to the correct value in your Python script before opening the browser window.

I've tested this using a VM running Ubuntu, which OS are you running on your remote system? Here's my launch_google.py :

import os
os.environ["DISPLAY"] = ":0"
import webbrowser
webbrowser.open("https://google.com")

Launch this using:

ssh <user>@<IP Address> "python launch_google.py&"

I included the ampersand otherwise the ssh session remains open. The python process doesn't need to keep running.

It is important to set the DISPLAY environment variable before importing the webbrowser module, otherwise the browsers won't be setup correctly. You can verify this running python via SSH:

>>> import os
>>> "DISPLAY" in os.environ
False
>>> import webbrowser
>>> len(webbrowser._browsers)
0
>>> webbrowser.open("https://google.com")
False
>>> os.environ["DISPLAY"] = ":0"
>>> reload(webbrowser)
<module 'webbrowser' from '/usr/lib/python2.7/webbrowser.pyc'>
>>> len(webbrowser._browsers)
3
>>> webbrowser.open("https://google.com")
True
>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM