python 获取 本地mac 地址,本地ip地址
# -*- encoding=utf-8 -*-
# 获取本机mac地址
import uuid
def get_mac_address():
mac=uuid.UUID(int = uuid.getnode()).hex[-12:]
return ":".join([mac[e:e+2] for e in range(0,11,2)])
import socket
#获取本机电脑名
myname = socket.getfqdn(socket.gethostname())
#获取本机ip
myaddr = socket.gethostbyname(myname)
print(myname)
print(myaddr)
if __name__ == '__main__':
if myaddr == '192.168.2.223':
print("hello")
res=get_mac_address()
print(res)
方法二:
#!/usr/bin/python
#encoding: utf-8
import os
import re
def get_mac_address():
res=os.popen('ifconfig')
d=res.read()
result=re.findall(' ether (.*?) txqueuelen 1000 ',d,re.S)
ip_mac=[]
for each in result:
ip_mac.append(each)
return ip_mac[1]
if __name__ == '__main__':
res=get_mac_address()
print(res)