-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathutils.py
44 lines (35 loc) · 1.27 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import socket
import psutil
from cpuid import CPUID
import struct
import subprocess
def get_host_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
res = s.getsockname()[0]
s.close()
return res
def get_host_name(host_ip):
return socket.gethostbyaddr(host_ip)[0]
def is_under_vmware():
VM_TOOLS_PROCESS_NAME = "vmtoolsd"
for proc in psutil.process_iter():
try:
if VM_TOOLS_PROCESS_NAME in proc.name().lower():
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
def decode_CPUID_values(ebx, ecx, edx):
ebx = struct.unpack("<I", struct.pack(">I", ebx))[0]
ecx = struct.unpack("<I", struct.pack(">I", ecx))[0]
edx = struct.unpack("<I", struct.pack(">I", edx))[0]
return (bytes.fromhex(hex(ebx)[2:]) + bytes.fromhex(hex(ecx)[2:]) + bytes.fromhex(hex(edx)[2:])).decode()
def get_HyperVisorName():
q = CPUID()
_, ebx, ecx, edx = q(0x40000000)
return decode_CPUID_values(ebx, ecx, edx)
def is_running_under_VMware():
return get_HyperVisorName() == "VMwareVMware"
def run_process(process, args):
subprocess.run([process] + [args])