免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 12304 | 回复: 11
打印 上一主题 下一主题

如果得到网卡mac地址 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-23 10:52 |只看该作者 |倒序浏览
RT

得到本机mac地址

论坛徽章:
4
CU大牛徽章
日期:2013-03-13 15:29:07CU大牛徽章
日期:2013-03-13 15:29:49CU大牛徽章
日期:2013-03-13 15:30:192015亚冠之广州恒大
日期:2015-07-22 17:20:15
2 [报告]
发表于 2007-11-29 23:45 |只看该作者
import os
import re

def GetMac():
    if os.name == 'nt':
        try:
            ret = ''
            CmdLine = 'ipconfig /all'
            r = os.popen(CmdLine).read()
            if r:
                L = re.findall('Physical Address.*?([0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2}-[0-9,A-F]{2})', r)
                if len(L) > 0:
                    ret = L[0]
        except:
            pass
        
    elif os.name == "posix":
        try:
            ret = ''
            CmdLine = 'ifconfig'
            r = os.popen(CmdLine).read()
            if r:
                L = re.findall('HWaddr.*?([0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2}:[0-9,A-F]{2})', r)
                if len(L) > 0:
                    ret = L[0]
        except:
            pass
    else:
        pass
    return ret

if __name__ == '__main__':
    mac =  GetMac()
    print mac

以前写的 没想到什么更好的办法
linux下是centos测试的

论坛徽章:
0
3 [报告]
发表于 2007-11-30 19:13 |只看该作者
TO:CmdLine = 'ifconfig'

linux下面是ifconfig吧

论坛徽章:
0
4 [报告]
发表于 2007-11-30 22:03 |只看该作者
虽然我现在在用python , 但是很不喜欢python的正则,   

perl 下很简单的实现..

  1. $a=`ipconfig /all`;
  2. if ($a=~ /Physical.*:(.*)/) {
  3.         print $1;
  4. }
复制代码

论坛徽章:
4
CU大牛徽章
日期:2013-03-13 15:29:07CU大牛徽章
日期:2013-03-13 15:29:49CU大牛徽章
日期:2013-03-13 15:30:192015亚冠之广州恒大
日期:2015-07-22 17:20:15
5 [报告]
发表于 2007-12-02 13:29 |只看该作者
呵呵 3楼的朋友 。。。。

论坛徽章:
0
6 [报告]
发表于 2007-12-03 11:30 |只看该作者
>>> import uuid
>>> node = uuid.getnode()
>>> mac = uuid.UUID(int=node)
>>> addr = mac.hex[-12:]
>>> addr
'001a4d71d4ca'

论坛徽章:
0
7 [报告]
发表于 2008-02-15 14:14 |只看该作者
在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]

论坛徽章:
0
8 [报告]
发表于 2008-02-15 15:14 |只看该作者
>>> import uuid
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named uuid
是为什呢?

>>> print os.name
posix

[ 本帖最后由 p4apple 于 2008-2-15 15:15 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2008-02-15 15:24 |只看该作者
不知道啊,我的是win,安装的py2.5,uuid是标准模块里的

论坛徽章:
0
10 [报告]
发表于 2008-02-17 01:05 |只看该作者
哈哈, 可能是版本问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP