python读取mac地址(python获取局域网中的ip和mac地址)
怎么用python在arp缓存中获取mac地址‘
这个需要安装一个模块scapy
代码如下:
#!/usr/bin/env?python
#?-*-?coding:?utf-8?-*-
from?scapy.all?import?srp,Ether,ARP,conf
ipscan='192.168.1.1/24'
try:
????ans,unans?=?srp(Ether(dst="FF:FF:FF:FF:FF:FF")/ARP(pdst=ipscan),timeout=2,verbose=False)
except?Exception,e:
????print?str(e)
else:
????for?snd,rcv?in?ans:
????????list_mac=rcv.sprintf("%Ether.src%?-?%ARP.psrc%")
????????print?list_mac
为什么用python获取mac地址会变动?
首先声明,我本人还没有研究出来问题的究竟。此处只是写下我本人的一点小心得,大家一起进步。
因为我发现,使用uuid库得到的mac地址,总有最后一位不对。所以,我就查看了python官方的uuid文档,找到了问题的关键是调用UUID()的时候,会调用getnode()函数以得到物理地址。
这个是getnode()函数的定义:
我把它摘出来,考到下面。
def getnode(*, getters=None):
"""Get the hardware address as a 48-bit positive integer.
The first time this runs, it may launch a separate program, which could
be quite slow. ?If all attempts to obtain the hardware address fail, we
choose a random 48-bit number with its eighth bit set to 1 as recommended
in RFC 4122.
"""
global _node
if _node is not None:
return _node
if sys.platform == 'win32':
getters = _NODE_GETTERS_WIN32
else:
getters = _NODE_GETTERS_UNIX
for getter in getters + [_random_getnode]:
try:
_node = getter()
except:
continue
if (_node is not None) and (0 = _node (1 48)):
return _node
assert False, '_random_getnode() returned invalid value: {}'.format(_node)
我正在试图通过研究这个问题来试图研究。但同样,我觉得我们可以直接让python调用系统库,从而执行系统自带的命令:(类似于windows下cmd里面"ipconfig -all"命令,或者ubuntu下terminal中"ifconfig"命令)。实现物理地址。之后,根据“短时间内该机器的网卡不会出现过大的变动这个前提”,我们可以根据返回内容,切片出我们需要的部分即可。
利用python写一段读取电脑配置信息的程序
主要利用python的wmi模块,提供非常多的信息。
import?wmi
def?sys_version():
????c?=?wmi.WMI()
????#?操作系统版本,版本号,32位/64位
????print('\nOS:')
????sys?=?c.Win32_OperatingSystem()[0]
????print(sys.Caption,?sys.BuildNumber,?sys.OSArchitecture)
????#?CPU类型?CPU内存
????print('\nCPU:')
????processor?=?c.Win32_Processor()[0]
????print(processor.Name.strip())
????Memory?=?c.Win32_PhysicalMemory()[0]
????print(int(Memory.Capacity)//1048576,'M')
????#?硬盘名称,硬盘剩余空间,硬盘总大小
????print('\nDISK:')
????for?disk?in?c.Win32_LogicalDisk(DriveType=3):
????????print(disk.Caption,'free:',?int(disk.FreeSpace)//1048576,'M\t',?'All:',?int(disk.Size)//1048576,'M')
????#?获取MAC和IP地址
????print('\nIP:')
????for?interface?in?c.Win32_NetworkAdapterConfiguration(IPEnabled=1):
????????print("MAC:?%s"?%?interface.MACAddress)
????????for?ip_address?in?interface.IPAddress:
????????????print("\tIP:?%s"?%?ip_address)
????#?BIOS版本?生产厂家?释放日期
????print('\nBIOS:')
????bios?=?c.Win32_BIOS()[0]
????print(bios.Version)
????print(bios.Manufacturer)
????print(bios.ReleaseDate)
sys_version()
显示:
OS:
Microsoft?Windows?10?专业版?17134?64?位
CPU:
Intel(R)?Core(TM)?i5-7300HQ?CPU?@?2.50GHz
8192?M
DISK:
C:?free:?34165?M ?All:?120825?M
D:?free:?265648?M ?All:?390777?M
E:?free:?35669?M ?All:?204796?M
F:?free:?5814?M ?All:?28163?M
G:?free:?328650?M ?All:?329999?M
IP:
MAC:?00:50:56:C0:00:01
IP:?192.168.182.1
IP:?fe80::e0fb:efd8:ecb0:77f4
MAC:?00:50:56:C0:00:08
IP:?192.168.213.1
IP:?fe80::8da1:ce76:dae:bd48
MAC:?54:E1:AD:77:57:AB
IP:?192.168.199.105
IP:?fe80::aca8:4e6f:46e7:ef4a
BIOS:
LENOVO?-?1
LENOVO
20170518000000.000000+000