When oracle virtualbox are set, How to get local ip address in python

人走茶凉 提交于 2020-02-04 05:58:48

问题


--

I've searched to find out how to get my local ip address using python. (my python version is 3.4) And there is an answer. That answer says:

import socket
socket.gethostbyname(socket.gethostname())

And it works. script said my ip address is 192.168.56.1 . But my local (=internal) ip address is 192.168.0.100 .

So, What was that ip address? It was an ip address of 'Virtualbox Host-Only Network' which was installed when I setup virtualbox a week ago. (Virtualbox's version is 5.0) I have now 2 network adapter, one is 'Virtualbox Host-Only Network' and the other is local area network

When I turn that 'Virtualbox Host-Only Network' off in network panel. python script says my ip address is 192.168.0.100 which I want.

How to get my ip address '192.168.0.100' regardless of disabling 'Virtualbox Host-Only Network'? ( I really don't want to turn off 'Virtualbox Host-Only Network'. )


回答1:


You can use a module called netifaces to get the ip address of whatever network interfaces are available on your machine.

You can install netifaces with pip if you have pip installed on your system.

pip install netifaces

An example from the command line on a linux system:

My machine has my regular network interface on eth0 and an alias network interface on eth0:0 (this would be similar to your virtualbox host-only network interface, but the name would be different.)

Getting a listing of the network interfaces available on your machine via the python interpreter:

(stackoverflow)[root@joeyoung.io stackoverflow]# python

>>> import netifaces
>>> netifaces.interfaces()
['lo', 'eth0', 'eth0:0']

Getting more information about my regular network interface eth0:

>>> netifaces.ifaddresses('eth0')
{17: [{'broadcast': 'ff:ff:ff:ff:ff:ff', 'addr': '52:54:00:0d:4d:36'}], 2: [{'broadcast': '159.107.29.255', 'netmask': '255.255.254.0', 'addr': '159.107.28.65'}]}

I'm only interested in the Internet Protocal Address Family so lets narrow down the results using AF_INET as a filter:

>>> netifaces.ifaddresses('eth0')[netifaces.AF_INET]
[{'broadcast': '159.107.29.255', 'netmask': '255.255.254.0', 'addr': '159.107.28.65'}]

This gives me back a list with 1 entry. That entry contains a dictionary. I want to get the value represented by the addr key in order to get the ip address of my eth0 interface:

>>> netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr']
'159.107.28.65'

159.107.28.65 is the local IP address of my eth0 interface.

We can do the same steps above to get my alias IP address on eth0:0 as well:

>>> netifaces.ifaddresses('eth0:0')
{2: [{'broadcast': '192.168.13.255', 'netmask': '255.255.255.0', 'addr': '192.168.13.2'}]}
>>> netifaces.ifaddresses('eth0:0')[netifaces.AF_INET]
[{'broadcast': '192.168.13.255', 'netmask': '255.255.255.0', 'addr': '192.168.13.2'}]
>>> netifaces.ifaddresses('eth0:0')[netifaces.AF_INET][0]['addr']
'192.168.13.2'



回答2:


This sequence works for me on a Linux virtual machine:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 53))
print s.getsockname()[0]

result:

10.112.54.236


来源:https://stackoverflow.com/questions/32069628/when-oracle-virtualbox-are-set-how-to-get-local-ip-address-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!