如何确定路由器的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/ in () ----> 1 gws=netifaces.gateways() AttributeError: 'module' object has no attribute 'gateways' 

怎么能帮到我,我需要帮助。 谢谢。

  1. 使用pip安装netifaces

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