1

I'm writing a python application that is supposed to run differently, depending on host/guest system context.

What I need is a function that tells where I am:

if IamRunningOnHost():
    #start server if not running, connect to server
else: #running on guest
    #use different ip, terminate if server not running

Now the easiest way would be to detect some known file system differences between host and guest system or to have a config which I edit manually each time I switch system context.

Is there a cleaner and more universal way to differentiate whenever I am running inside of VirtualBox?

Edit1: My guest and host are both linux operating systems.

Edit2: I failed to mention, it has to be achieved via python call and without the need for root privileges. I also don't want to install additional software in the guest. I simply want to copy and run the application without the requirement of further system changes. The suggested duplicate "Detect virtualized OS from an application?" is therefore not helpful for me.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
evolution
  • 593
  • 6
  • 20
  • What's the operating system of the Host and Guest? I have a powershell script template I recently put together to grab system info (that would differentiate between virtual and host) so if one or both are Win 7/8 I can give you that. I know for certain that Windows VM's contain system info that is easily recognizable as being Virtual. I can only assume that Linux VM's would share similar qualities. My suggestion is to find some Static VM info, through some kind of CLI spec output, and compare (`If/Else`) that to the hosts respective data. –  Apr 03 '15 at 15:55
  • Both systems are ubuntu 14, so your script probably won't help. But I'll add that to my question. – evolution Apr 03 '15 at 16:11
  • 1
    The process would be similar though. Run a command that lists specs of current running machine. ie: (`lshw`, `lscpu`, `lspci`) Filter out some specific, static information that clearly indicates usage of a VM or Host. Store that information in a variable. Cross reference the variable to something else of your choosing using an `if/else`. A simple suggestion would be... `if Variable = "VirtualBox"` start server `else use different IP` I grabbed a screen shot for you of `lshw` in Ubuntu in a VM. http://s29.postimg.org/aiphz4jmf/Capture.png –  Apr 03 '15 at 16:36
  • Thanks :) `lshw | grep VirtualBox ; lspci | grep VirtualBox ` is good enough and I can combine it with `subprocess.Popen` I can then get the answer via `.communicate()`as suggested here: http://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on – evolution Apr 03 '15 at 17:04

0 Answers0