Score:1

Continuously monitor usb connection status

mx flag

I am facing a problem with usb port. when I connect a mobile device to my PC USB port, sometimes it becomes disconnect and immediately gets connected. So I want to monitor the USB connection status continuously. Is there any way I can monitor the connection status live?

It would be good if can get the log file also with only usb connection status

Score:0
iq flag

You could do that with pyudev and a python script that monitors USB connections, logs the events to a file, and prints the events to the console.

start by installing the packagepip install pyudev

then here is the script:

import os
import sys
import pyudev

from datetime import datetime

def log_event(event_type, device):
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    message = f"{timestamp} - {event_type}: {device.get('ID_SERIAL_SHORT') or device.get('ID_SERIAL')} - {device.get('ID_MODEL')}"

    with open("usb_connection_log.txt", "a") as log_file:
        log_file.write(message + "\n")

    print(message)

def monitor_usb_events():
    context = pyudev.Context()
    monitor = pyudev.Monitor.from_netlink(context)
    monitor.filter_by(subsystem='usb')

    for action, device in monitor:
        if action == 'add' and 'ID_SERIAL' in device:
            log_event("Connected", device)
        elif action == 'remove' and 'ID_SERIAL' in device:
            log_event("Disconnected", device)

if __name__ == "__main__":
    try:
        monitor_usb_events()
    except KeyboardInterrupt:
        print("\nMonitoring stopped.")
        sys.exit(0)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.