2

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.

P.S 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!

6
  • you have to log in B first... or transfer the file there using scp /home/pi/Documents/webTest.py [email protected]:~/ Commented Jul 13, 2017 at 3:48
  • thanks for your answer. But could you please tell me what you mean by "log in B"? and why do I need to transfer files?python is stored on B and I want it to run on B by controlling A.Could you please specify?Thanks a lot@Eliethesaiyan Commented Jul 13, 2017 at 4:22
  • This is not a programming question. Commented Jul 13, 2017 at 5:36
  • Yes but it is a question someone might encounter while programming.Indeed there is such a need to accomplish such functions to use program in a proper way but I don't know how.Could you please help to work it out?Thanks @ Klaus D. Commented Jul 13, 2017 at 6:02
  • Your SSH connection might not have the appropriate permissions to launch a browser. What happens if you try the same thing from an interactive SSH session? Commented Jul 13, 2017 at 7:54

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you very much for your detailed answer.I think I got the reason why I cannot display a web on B. I tried to use "ssh -Y ssh <username of B>@<ip of B>" on A's terminal,but it says "could not resolve hostname ssh:name or service not known".I can ping ip of B but that command does not work.
sorry but you said"open the window on the local screen (in your example on A)",actually I want to open a web on B ,not on A
THANK YOU VERY MUCH! You are really smart!!! After changing your suggested command "ssh -Y ssh <username of B>@<ip of B>" into "ssh -Y <username of B>@<ip of B>" ,the web is opened successfully on the remote machine!!!
@HelenCui: Oups, I should have payed more attention to the code... Fixed.
1

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
$ 

4 Comments

thanks,I tried " ssh pi@B python X.py " command on A, but it only allow X.py to execute on A. I want it to run on B.
@HelenCui if you copy X.py to B (first command scp) it will works on B too. The commands above are from my terminal, with some hostname and username substitution. So I'm pretty sure it works.
Indeed it is excuted on B,and the output is dispayed on A.Do you have any ideas to display the output on B?what do I need to do on B?like open shell of python or..? @ rth
Can you describe what you want to achieve in details? It is hard to suggest anything without a better understanding of your goal.
0

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
>>>

1 Comment

Note that if you call ssh -XY <user>@<IP Address> or your default settings have ForwardX11 yes, "DISPLAY" variable is set and web browser appears on your current display.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.