How to reset Ethernet data usage statistics using python?

19 Views Asked by At

How to reset Ethernet data usage statistics using Python? Preferably ctypes or subprocess without rebooting the PC, etc. For example, I want to reset only xray.exe statistics

Honestly, I’ve already tried all the options, I also tried ChatGPT, but it didn’t want to help me with this. Yes, and I want to reset the statistics without rebooting the PC, etc. Via python using subproccess or ctypes

import subprocess
import time

app_name = 'xray.exe'

try:
    pid_command = f'tasklist /fi "imagename eq {app_name}" /fo csv'
    result = subprocess.run(pid_command, shell=True, capture_output=True, text=True)
    pid = result.stdout.split(',')[1]

    reset_command = f'netsh interface ipv4 reset statistics {app_name}'
    try:
        subprocess.run(reset_command, shell=True, check=True)
    except subprocess.CalledProcessError as e:
        pass

    release_command = f'ipconfig /release {app_name}'
    renew_command = f'ipconfig /renew {app_name}'

    subprocess.run(release_command, shell=True, check=True)
    time.sleep(2)
    subprocess.run(renew_command, shell=True, check=True)

finally:
    subprocess.run(f"taskkill /f /pid {pid}", shell=True)
    time.sleep(2)
    subprocess.run(f"start {app_name}", shell=True)
0

There are 0 best solutions below