Python获取网卡信息(名称、MAC、IP、网关等)

简介:

    “人生苦短,我用Python”。Python的高效有一部分是跟它丰富的模块分不开的。Python有很多第三方模块可以帮助我们完成一些事情,减少开发时间。

Python pypi库中一个模块名字叫“netifaces”,使用C语言写的一个第三方模块。可以:

    1.获取本机的所有网关

    2.获取本机所有的接口Interface(网卡NIC)

    3.获取本机指定接口的详细信息,包括IP地址、子网掩码、广播地址、MAC地址等

不过遗憾的是这个模块的功能太有限以及会带出一些令人困惑的信息,例如Windows系统上的子网掩码可能不正确等。

PS:要想获取公网地址,可以使用很多种API,例如:

    # Use 3rd party web-sites to get your IP   
    # Please note that I do not recommend following curl/wget method due to security reasons. You have been warned:    
    curl ifconfig.me    
    curl icanhazip.com    
    curl ipecho.net/plain    
    curl ifconfig.co    
    curl http://ip.chinaz.com/getip.aspx

运行截图如下:

image

image

代码请移步到GitHub:https://github.com/DingGuodong/LinuxBashShellScriptForOps/blob/master/projects/WindowsSystemOps/Network/getNetworkStatus.py

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
# encoding: utf-8
# -*- coding: utf8 -*-
"""
Created by PyCharm.
File:               LinuxBashShellScriptForOps:getNetworkStatus.py
User:               Guodong
Create Date:        2016/11/2
Create Time:        16:20
 
show Windows or Linux network Nic status, such as MAC address, Gateway, IP address, etc
 
# python getNetworkStatus.py
Routing Gateway:               10.6.28.254
Routing NIC Name:              eth0
Routing NIC MAC Address:       06:7f:12:00:00:15
Routing IP Address:            10.6.28.28
Routing IP Netmask:            255.255.255.0
  """
import  os
import  sys
 
try :
     import  netifaces
except  ImportError:
     try :
         command_to_execute  =  "pip install netifaces || easy_install netifaces"
         os.system(command_to_execute)
     except  OSError:
         print  "Can NOT install netifaces, Aborted!"
         sys.exit( 1 )
     import  netifaces
 
routingGateway  =  netifaces.gateways()[ 'default' ][netifaces.AF_INET][ 0 ]
routingNicName  =  netifaces.gateways()[ 'default' ][netifaces.AF_INET][ 1 ]
 
for  interface  in  netifaces.interfaces():
     if  interface  = =  routingNicName:
         # print netifaces.ifaddresses(interface)
         routingNicMacAddr  =  netifaces.ifaddresses(interface)[netifaces.AF_LINK][ 0 ][ 'addr' ]
         try :
             routingIPAddr  =  netifaces.ifaddresses(interface)[netifaces.AF_INET][ 0 ][ 'addr' ]
             # TODO(Guodong Ding) Note: On Windows, netmask maybe give a wrong result in 'netifaces' module.
             routingIPNetmask  =  netifaces.ifaddresses(interface)[netifaces.AF_INET][ 0 ][ 'netmask' ]
         except  KeyError:
             pass
 
display_format  =  '%-30s %-20s'
print  display_format  %  ( "Routing Gateway:" , routingGateway)
print  display_format  %  ( "Routing NIC Name:" , routingNicName)
print  display_format  %  ( "Routing NIC MAC Address:" , routingNicMacAddr)
print  display_format  %  ( "Routing IP Address:" , routingIPAddr)
print  display_format  %  ( "Routing IP Netmask:" , routingIPNetmask)

最后:不要重复制造轮子。重复制造轮子对自己而言,虽然制造的过程是学习巩固的过程,但重复制造轮子对别人没有好处,人生苦短,别重复制造轮子,除非你制造的足够好。

tag:python获取MAC地址,python获取网关地址,python获取IP地址

--end--




本文转自 urey_pp 51CTO博客,原文链接:http://blog.51cto.com/dgd2010/1868851,如需转载请自行联系原作者


相关文章
|
6天前
|
数据挖掘 API 数据安全/隐私保护
python请求模块requests如何添加代理ip
python请求模块requests如何添加代理ip
|
8天前
|
存储 关系型数据库 MySQL
Python搭建代理IP池实现存储IP的方法
Python搭建代理IP池实现存储IP的方法
|
8天前
|
Python
Python动态IP代理防止被封的方法
Python动态IP代理防止被封的方法
|
8天前
|
存储 API Python
python之代理ip的配置与调试
python之代理ip的配置与调试
|
8天前
|
存储 Python
用Python实现批量下载文件——代理ip排除万难
用Python实现批量下载文件——代理ip排除万难
|
8天前
|
数据采集 API Python
python关于ip代理池的获取和随机生成
python关于ip代理池的获取和随机生成
|
8天前
|
数据采集 存储 安全
python检测代理ip是否可用的方法
python检测代理ip是否可用的方法
|
8天前
|
Python
干货文:在 Mac 中卸载 Python 的方式
干货文:在 Mac 中卸载 Python 的方式
13 1
|
23天前
|
Python Linux iOS开发
使用 Python 打印本机 Mac 地址
使用 Python 打印本机 Mac 地址
8 0
|
1月前
|
数据采集 Web App开发 数据安全/隐私保护
Python爬虫-使用代理伪装IP
介绍代理,设置代理,使用代理伪装IP案例
25 0