小绿和小蓝 堆糖:如果得到网卡mac地址

来源:百度文库 编辑:中财网 时间:2024/04/28 15:42:37
>>> import uuid
>>> node = uuid.getnode()
>>> mac = uuid.UUID(int=node)
>>> addr = mac.hex[-12:]
>>> addr
'001a4d71d4ca' 自动化运维案例分享讨论之二(大奖)|ChinaUnix活动列表 积分好礼等你拿! | Linux平台存储大家谈! | ChinaUnix十年十城沙龙ppt下载



ibetibet当前离线
空间积分
0
信誉积分
100
UID
51397
阅读权限
10
积分
3
帖子
4
精华
0
可用积分
3
专家积分
0
在线时间
2 小时
注册时间
2006-11-20
最后登录
2010-05-06

白手起家

白手起家, 积分 3, 距离下一级还需 197 积分
帖子
4
主题
1
精华
0
可用积分
3
专家积分
0
在线时间
2 小时
注册时间
2006-11-20
最后登录
2010-05-06
  • 串门
  • 好友
  • 博客
  • 消息
论坛徽章:
0
7楼[报告] 发表于 2008-02-15 14:14:16|只看该作者在c:/python25/Lib/uuid.py可以看看uuid的源码:
在其_ipconfig_getnode()函数的基础上,我添加了一个函数,考虑多个网卡的情况:
def ipconfig_getnode(id):
    """----add by liangzi----get mac if have more than one newwork hardware"""
    import os, re
    dirs = ['', r'c:\windows\system32', r'c:\winnt\system32']
    try:
        import ctypes
        buffer = ctypes.create_string_buffer(300)
        ctypes.windll.kernel32.GetSystemDirectoryA(buffer, 300)
        dirs.insert(0, buffer.value.decode('mbcs'))
    except:
        pass
    macad=[]
    for dir in dirs:
        try:
            pipe = os.popen(os.path.join(dir, 'ipconfig') + ' /all')
        except IOError:
            continue
        for line in pipe:
            value = line.split(':')[-1].strip().lower()
            if re.match('([0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]', value):
                 macad.append(value)          #我需要的字符串
                 #macad.append(int(value.replace('-', ''), 16))  这里是返回数字
   
    return macad[id]