# -*- coding: utf-8 -*-
import json
import socket
import requests
from psutil import net_if_addrs
def get_mac_address():
for k, v in net_if_addrs().items():
if k == '以太网':
for item in v:
address = item[1]
if '-' in address and len(address) == 17:
return address
# 获取本机电脑名
myname = socket.getfqdn(socket.gethostname())
# 获取本机ip
myaddr = socket.gethostbyname(myname)
# 获取MAC地址
mymac = get_mac_address()
# 要发送的消息内容
json = {
"msgtype": "text",
"text": {
"content": f"获取到一条新主机信息!\n 主机名:{myname} \n IP地址:{myaddr} \n MAC地址:{mymac}"
}
}
def send_dingtlak():
access_token = 'xxxxxxx'
ding_url = f'https://oapi.dingtalk.com/robot/send?access_token={access_token}'
# print(ding_url)
resp = requests.post(url=ding_url, json=json)
# print(resp.text)
if __name__ == '__main__':
send_dingtlak()