python 获取本机ip和mac
时间: 2023-08-22 11:10:01 浏览: 55
你可以使用 Python 的 socket 和 uuid 模块来获取本机的 IP 地址和 MAC 地址。
获取 IP 地址的代码示例:
```python
import socket
# 获取本机的 IP 地址
ip_address = socket.gethostbyname(socket.gethostname())
print(ip_address)
```
获取 MAC 地址的代码示例:
```python
import uuid
# 获取本机的 MAC 地址
mac_address = ':'.join(format(s, '02x') for s in uuid.getnode().to_bytes(6, 'big'))
print(mac_address)
```
需要注意的是,获取 MAC 地址的代码可能在 Windows 系统上无法正常运行。在 Linux 和 macOS 系统上则可以正常运行。
相关问题
python收集linux IP地址和mac地址
你可以使用 Python 中的 `socket` 和 `subprocess` 模块来收集 Linux 系统上的 IP 地址和 MAC 地址。以下是一个示例代码:
```python
import socket
import subprocess
# 获取主机名
host_name = socket.gethostname()
# 获取 IP 地址
ip_address = socket.gethostbyname(host_name)
# 获取所有网络接口信息
ifconfig_result = subprocess.run(['ifconfig'], stdout=subprocess.PIPE).stdout.decode('utf-8')
# 解析出每个网络接口的 IP 地址和 MAC 地址
interface_info = {}
for line in ifconfig_result.split('\n'):
if line.startswith(' '):
# 处理多行输出的情况
interface_info[interface_name]['output'].append(line.strip())
continue
if ':' in line:
# 解析出网络接口名称和 MAC 地址
parts = line.split()
interface_name = parts[0].strip(':')
mac_address = parts[1]
interface_info[interface_name] = {'mac_address': mac_address, 'output': [line.strip()]}
else:
interface_name = None
# 输出结果
print('Hostname:', host_name)
print('IP address:', ip_address)
for interface_name, info in interface_info.items():
print('Interface:', interface_name)
print(' MAC address:', info['mac_address'])
print(' Output:')
for line in info['output']:
print(' ', line)
```
这段代码可以输出主机名、主机的默认 IP 地址,以及每个网络接口的 IP 地址和 MAC 地址。注意,如果一个网络接口有多个 IP 地址,这段代码只会输出其中的一个 IP 地址。如果需要获取所有 IP 地址,需要对代码进行相应的修改。
python写代码用MAC地址在DHCP服务器中获取相应的ip地址
可以使用Python中的socket和struct库获取MAC地址,并使用Python的requests库向DHCP服务器发送请求,获取相应的IP地址。以下是一个示例代码:
```python
import socket
import struct
import requests
# 获取本机MAC地址
def get_mac_address():
mac = ':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff)
for ele in range(0, 8 * 6, 8)][::-1])
return mac
# 将MAC地址转换为二进制格式
def mac_to_bytes(mac):
return struct.pack('!6B', *[int(x, 16) for x in mac.split(':')])
# 发送DHCP请求并获取IP地址
def get_ip_address(mac):
# 构造DHCP请求包
dhcp_packet = b'\x01\x01\x06\x00' + b'\x00' * 236 + b'\x63\x82\x53\x63' + b'\x35\x01\x03' + \
b'\x3d\x07\x01' + mac_to_bytes(mac) + b'\xff\x00'
# 绑定UDP套接字并发送DHCP请求
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.bind(('0.0.0.0', 68))
sock.sendto(dhcp_packet, ('255.255.255.255', 67))
# 接收DHCP响应并解析IP地址
while True:
data, server = sock.recvfrom(1024)
if data[0] == 0x02 and data[1] == 0x01 and data[2] == 0x06 and data[3] == 0x00:
ip_address = '.'.join(map(str, data[20:24]))
return ip_address
# 使用requests库发送HTTP请求获取IP地址
def get_ip_address_http(mac):
url = 'http://dhcp_server_ip_address/dhcp_request?mac=' + mac
response = requests.get(url)
if response.status_code == 200:
ip_address = response.text
return ip_address
# 测试
mac = get_mac_address()
ip_address = get_ip_address(mac)
print('IP address:', ip_address)
ip_address = get_ip_address_http(mac)
print('IP address:', ip_address)
```
以上代码中的get_mac_address()函数使用uuid库获取本机MAC地址,并将其转换为标准格式。mac_to_bytes()函数将MAC地址转换为二进制格式,以便构造DHCP请求包。get_ip_address()函数构造并发送DHCP请求,接收并解析DHCP响应中的IP地址。get_ip_address_http()函数使用requests库发送HTTP请求获取IP地址。
请注意替换代码中的dhcp_server_ip_address为您的DHCP服务器的IP地址。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)