4

I am connecting to virtual machine with putty ssh from my local machine and running a python script on virtual machine. From the script, I want to open a web browser with specific URL on my local machine. Is it possible to do so with python?

SimpleHTTPServer is running on virtual machine, and HTML file is present on it, so I can open URL on virtual machine with localhost:8080 and for local machine, I can use hostname of virtual machine <hostname>:8080.

If I have to open the web browser on virtual machine, I can use following:

import webbrowser
url = "http://localhost:8080/"
webbrowser.open_new(url)

But if I want to open the same URL using hostname of virtual machine on local web browser, not sure what can be used:

import os
hostname = os.uname()[1]
url = "http://%s:8080/" % hostname  # this hostname is virtual machine's hostname
print "Opening this URL in your browser: %s" % url
# Here I have to open a web browser manually and copy the URL.
# Looking for something which can open web browser automatically.
npatel
  • 1,081
  • 2
  • 13
  • 21
  • To get your local machine to open a web page, you need to be running a script on your local machine. So you could try running a Python script on your local machine that uses an ssh library (or possibly calls putty as a subprocess) to connect to the remote machine and run the script there. Then your Python script on the local machine should retrieve the relevant URL from the remote session and launch the web page locally. – Matthias Fripp Apr 11 '18 at 20:17
  • Your wording may be ambiguous or misunderstood. Is your goal to open the **browser** on your local machine? Or to have a browser on the *virtual machine* open with a **url** that points to your local machine? In other words, should the browser process run on your local machine or the virtual machine? – sytech Apr 11 '18 at 20:20
  • i have edited the question and clarified where `SimpleHTTPServer` is running. – npatel Apr 11 '18 at 20:27
  • I wonder if you ever did make it. I want to use this type of functionality as well. – Community Aug 12 '20 at 12:51

2 Answers2

0

When you use a virtual machine , you're totally switched to that environment. Also when you refer to localhost , that points to that port on the virtual machine and not your local machine . One best approach to this would be to use your Host IP address . Change your code to

import webbrowser
url = "<your-local-Ip-address>:8080/"

Open url in a new window of the default browser, if possible, otherwise, open url in the only browser window.

webbrowser.open_new(url)

Open url in a new page (“tab”) of the default browser, if possible, otherwise equivalent to open_new().

webbrowser.open_new_tab(url)

You can also export your host to ip-adress before starting the server.. If this approach falls back . try using services like ngrok or localtunnel to expose your localhost to a public cloud and access that URl ,but that is slow and not the recommended way

karthik006
  • 886
  • 9
  • 19
0

You can exploit the fact that the SSH client (your local machine) IP will be present in the environment variables on the server (virtual machine) when you're connected by SSH.

import os
import webbrowser
connection = os.environ.get('SSH_CONNECTION')  #corrected keyword
client_ip, client_port, server_ip, server_port = connection.split()
url = "http://{hostname}:8080/".format(hostname=client_ip)
webbrowser.open(url)
npatel
  • 1,081
  • 2
  • 13
  • 21
sytech
  • 29,298
  • 3
  • 45
  • 86
  • It is showing `Making HTTP connection to :8080` and hanged there. – npatel Apr 11 '18 at 20:21
  • Ensure that your firewall on both your local machine and virtual machine allows traffic across port 8080 and the local machine is accepting external connections on that port. Can you see the page by putting that address in your browser manually? – sytech Apr 11 '18 at 20:22
  • I have to give hostname of virtual machine in URL, `SimpleHTTPServer` is running on VM, not on local machine. – npatel Apr 11 '18 at 20:29
  • Then shouldn't simply using `localhost` work for you? You mentioned the script is running on the virtual machine, if the webserver is also running on the virtual machine, the local machine is not involved. If you want to control your local machine *from* the virtual machine, you will need another vehicle for that communication. For your intents and purposes, your virtual machine should be thought of as a completely different computer. – sytech Apr 11 '18 at 20:32
  • `localhost` works if I want to open webbrowser on VM (through VNC), but if I want to open webbrowser on local machine, it has to be `hostname`. Looks like there is no way I can webbrowser on local machine, but just display a message and let user open browser manually. – npatel Apr 11 '18 at 20:34
  • 1
    Correct. The SSH server (virtual machine) cannot issue commands to the client (your local machine). To do this, you would need a server running on your local machine that listens for connections/commands. – sytech Apr 11 '18 at 20:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168767/discussion-between-npatel-and-sytech). – npatel Apr 11 '18 at 20:46