如何确定路由器的 IP 地址

如何确定路由器的 IP 地址

我想通过编写 Python 代码来确定路由器地址的 IP。我发现了这个建议:

$ sudo easy_install netifaces

Python 2.6.5 (r265:79063, Oct  1 2012, 22:04:36)
...
$ ipython
...
In [8]: import netifaces

In [9]: gws=netifaces.gateways()

In [10]: gws

Out[10]:

{2: [('192.168.0.254', 'eth0', True)],
 'default': {2: ('192.168.0.254', 'eth0')}}

In [11]: gws['default'][netifaces.AF_INET][0]

Out[11]: '192.168.0.254'

我在终端中得到的结果如下:

In [1]: import netifaces

In [2]: gws=netifaces.gateways()

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

/home/rimeh/Bureau/<ipython-input-2-47b29e25a9b8> in <module>()
----> 1 gws=netifaces.gateways()

AttributeError: 'module' object has no attribute 'gateways'

请问您能如何帮助我,我需要帮助。谢谢。

答案1

  1. netifaces使用以下方式安装pip

    sudo apt-get install python-pip
    sudo apt-get build-dep python-netifaces
    sudo pip install netifaces
    
  2. 要获取网关 IP,您可以使用以下代码:

    $ python 
    Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import netifaces
    >>> gws=netifaces.gateways()
    >>> gws['default'].values()[0][0]
    '192.168.1.1'
    >>> 
    

相关内容