我是靠谱客的博主 单薄铃铛,最近开发中收集的这篇文章主要介绍python找人脚本_python获取机器信息脚本(网上寻找的),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

获取机器信息(待测试)

# -*- coding: UTF-8 -*-

import psutil

import json

import os

import socket

import struct

import time

import sys

# 当前时间函数

def now_time():

now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))

return now_time

# 获取硬盘信息函数

def disk_info():

disk_dict = {}

disk_name = []

disk_parttions = psutil.disk_partitions()

for i in range(len(disk_parttions)):

parttions_name = disk_parttions[i].device

mountpoint = disk_parttions[i].mountpoint

mountfstype = disk_parttions[i].fstype

disk_info = psutil.disk_usage(mountpoint)

total = disk_info.total

used = disk_info.used

free = disk_info.free

disk_io = psutil.disk_io_counters(perdisk=True)

disk_name.append(str(parttions_name.split('/', parttions_name.count('/'))[-1]))

for key1 in disk_name:

tmp_disk_list = []

for key2 in disk_io.keys():

tmp_disk_list.append(key2)

if key1 in tmp_disk_list:

disk_dict[parttions_name] = {

"read_bytes": disk_io[key1].read_bytes,

"write_bytes": disk_io[key1].write_bytes,

"read_count": disk_io[key1].read_count,

"write_count": disk_io[key1].write_count,

"mountpoint": mountpoint,

"mountfstype": mountfstype,

"total": total,

"used": used,

"free": free

}

else:

pass

return disk_dict

# 获取内存信息函数

def mem_info():

mem_dict_list = psutil.virtual_memory()

mem_total = mem_dict_list.total

mem_used = mem_dict_list.used

mem_free = mem_dict_list.free

cpu_use = psutil.cpu_percent(interval=True)

host_runtime = psutil.boot_time()

mem_list_info = {

"mem_total": mem_total,

"mem_used": mem_used,

"mem_free": mem_free,

"cpu_used": cpu_use,

"host_run": host_runtime

}

return mem_list_info

# 获取CPU信息函数

def cpu_info():

cpu_use = psutil.cpu_percent(interval=True)

cpu_usage = {

"cpu_used": cpu_use

}

return cpu_usage

# 获取网卡信息函数

def net_info():

net_dict_list = {}

net_dev = psutil.net_io_counters(pernic=True)

for dev_name in net_dev.keys():

net_dict_list[dev_name] = {

"RX_b": net_dev[dev_name].bytes_recv,

"TX_b": net_dev[dev_name].bytes_sent,

"Dropout": net_dev[dev_name].dropout,

"Dropint": net_dev[dev_name].dropin

}

return net_dict_list

# 获取用户信息函数

def user_info():

user_dict_list = []

user_dict = psutil.users()

for i in range(len(user_dict)):

user_dict_tmp = {}

user_dict_tmp[user_dict[i].name] = {

"terminal": user_dict[i].terminal,

"host": user_dict[i].host,

"start_time": user_dict[i].started

}

user_dict_list.append(user_dict_tmp)

return user_dict_list

# 获取内存消耗前10的进程函数

def process_info():

process_dict_list_tmp = []

pid_dict = psutil.pids()

for i in pid_dict:

pid = psutil.Process(i)

pid_tuple = (i, pid.name(), pid.memory_percent(), pid.create_time(), pid.status(), pid.cwd(), pid.exe())

process_dict_list_tmp.append(pid_tuple)

process_dict_list_tmp.sort(key=lambda mem1: mem1[2], reverse=True)

process_dict_list = process_dict_list_tmp[0:10]

return process_dict_list

# 获取开机时间函数

def runtime_info():

host_runtime = psutil.boot_time()

host_run_time = {

"host_runtime": host_runtime

}

return host_run_time

# 没用的函数

def get_info():

host_info = {

'disk_info': disk_info(),

'mem_info': mem_info(),

'cpu_info': cpu_info(),

'net_info': net_info(),

'process_info': process_info(),

'user_info': user_info(),

'runtime_info': runtime_info()

}

return repr(host_info)

# socket数据发送函数

def socket_send(socket_server=None, socket_port=None, cammds=None, type=None, PackName=None):

if type == 'regs':

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((socket_server, socket_port))

s.sendall(cammds)

request = str(s.recv(18))

s.close()

return request

elif type == 'data':

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((socket_server, socket_port))

s.sendall(cammds)

request = str(s.recv(128))

if request == 'OK':

Rtime = now_time()

PackSize = sys.getsizeof(cammds)

print(" '%s' : transfer TCP Package '%s' Size '%s' sucessful ") % (Rtime, PackName, PackSize)

s.close()

# 构架TCP格式数据包函数

def trans_pack(commands=None, type=None):

Net_type = 4 # package versions

Mem_type = 1

Cpu_type = 2

Disk_type = 3

Runtime_type = 5

if type == 'Mem':

machine_number = json_conf()["machine_number"] # 获取机器码

body = json.dumps(dict(commands)) # 生成JSON格式包体

header = [Mem_type, body.__len__(), machine_number] # 组成包头格式为:版本,包体长度,机器码

headPack = struct.pack("!3I", *header) # 生成包头

sendPack = headPack + body.encode() # 构建TCP数据包

return sendPack

elif type == 'Net':

machine_number = json_conf()["machine_number"] # 获取机器码

body = json.dumps(dict(commands)) # 生成JSON格式包体

header = [Net_type, body.__len__(), machine_number] # 组成包头格式为:版本,包体长度,机器码

headPack = struct.pack("!3I", *header) # 生成包头

sendPack = headPack + body.encode() # 构建TCP数据包

return sendPack

elif type == 'Cpu':

machine_number = json_conf()["machine_number"] # 获取机器码

body = json.dumps(dict(commands)) # 生成JSON格式包体

header = [Cpu_type, body.__len__(), machine_number] # 组成包头格式为:版本,包体长度,机器码

headPack = struct.pack("!3I", *header) # 生成包头

sendPack = headPack + body.encode() # 构建TCP数据包

return sendPack

elif type == 'Disk':

machine_number = json_conf()["machine_number"] # 获取机器码

body = json.dumps(dict(commands)) # 生成JSON格式包体

header = [Disk_type, body.__len__(), machine_number] # 组成包头格式为:版本,包体长度,机器码

headPack = struct.pack("!3I", *header) # 生成包头

sendPack = headPack + body.encode() # 构建TCP数据包

return sendPack

elif type == 'Run':

machine_number = json_conf()["machine_number"] # 获取机器码

body = json.dumps(dict(commands)) # 生成JSON格式包体

header = [Runtime_type, body.__len__(), machine_number] # 组成包头格式为:版本,包体长度,机器码

headPack = struct.pack("!3I", *header) # 生成包头

sendPack = headPack + body.encode() # 构建TCP数据包

return sendPack

# 读取JSON配置函数

def json_conf():

a = os.getcwd()

# os.chdir("%s/client/conf" % a) # 改变工作目录为JSON文件目录

json_list = file("%s/client/conf/client_conf.json" % a) # 读取JSON配置文件

m = json_list.read()

json_list.seek(0)

json_conf = json.loads(json_list.read()) # 取出JSON配置文件

json_list.close()

return json_conf

def json_write(command=None):

a = os.getcwd()

os.chdir("%s/client/conf" % a)

print(command)

b = type(command)

m = open("client_conf.json", 'w')

# agent第一次注册函数

def client_regs():

host_info = json.dumps({'ip': str(json_conf()['local_adder']), 'mac': str(json_conf()['local_mac'])})

regs_addr = json_conf()['send_server']

regs_port = json_conf()['server_regs_port']

server_request = str(socket_send(socket_server=regs_addr, socket_port=regs_port, cammds=host_info, type='regs'))

conf = json_conf()

conf['machine_number'] = server_request

conf = json.dumps(conf)

json_write(command=conf)

# 数据包发送函数

def send_data():

DiskPack = trans_pack(commands=disk_info(), type='Disk') # 生成TCP数据包

MemPack = trans_pack(commands=mem_info(), type='Mem')

NetPack = trans_pack(commands=net_info(), type='Net')

regs_addr = json_conf()['send_server'] # 获取服务器地址

regs_port = json_conf()['send_port'] # 获取服务器端口

socket_send(socket_server=regs_addr, socket_port=regs_port, cammds=DiskPack, type='data',

PackName='DiskPack') # 向服务器发送数据包

socket_send(socket_server=regs_addr, socket_port=regs_port, cammds=MemPack, type='data', PackName='MemPack')

socket_send(socket_server=regs_addr, socket_port=regs_port, cammds=NetPack, type='data', PackName='NetPack')

time.sleep(json_conf()['sendoff_time']) # 等待延时

def check_client():

print('start get client info....')

machine_number = json_conf()['machine_number']

if machine_number:

print(machine_number)

print(11111)

else:

print(machine_number)

print(22222)

if __name__ == '__main__':

while True:

send_data()

最后

以上就是单薄铃铛为你收集整理的python找人脚本_python获取机器信息脚本(网上寻找的)的全部内容,希望文章能够帮你解决python找人脚本_python获取机器信息脚本(网上寻找的)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部