Get Serial Number of USB device with Python 3 - python

I have been using pyusb to access the detail of a printer plugged in via USB. I currently have the following code working, but it appears that different devices require a different index. Here is my current code:
import usb
dev = usb.core.find(idProduct=0x001f)
print(usb.util.get_string(dev,256,3))
dev2 = usb.core.find(idProduct=0x0009)
print(usb.util.get_string(dev2,256,3))
The code for dev works perfectly, outputting a serial number, but dev2 outputs 'Zebra,' the manufacturer name. If I change 3 to either 6 or 7 it works, but then the first dev returns an error.
One solution in Python 2 is to use print(dev.serial_number), but in the serial_number attribute doesn't appear to exist in pyusb for Python 3.
Is there a way to get this working reliably for all devices? Thanks.

For the present PyUSB version 1.0.2, I found the correct syntax to answer this question to be:
import usb
dev = usb.core.find(idProduct=0x001f)
print( usb.util.get_string( dev, dev.iSerialNumber ) )
dev2 = usb.core.find(idProduct=0x0009)
print( usb.util.get_string( dev2, dev2.iSerialNumber ) )

Related

snmp_walk function from easysnmp python library return empty list

I'm using snmp_walk function from easysnmp python library to get the current value from Jacarta powerZook meter but it returns an empty list (no values) but when I use Qtmib (SNMP MIB browser) I can get the value. I'm using Ubuntu 18.04 and tried all python interpreters (2.7, 3.5, 3.6, 3.7) but still nothing. also I ran the python script with "sudo" privileges but still no results. FYI the power meter connected directly to the ethernet port of my PC.
my code:
from easysnmp import snmp_walk
snmp_walk(oids='.1.3.6.1.4.1.19011.1.3.5.1.3.1.0 - Current/AMPS', hostname='192.168.1.200',
community='public', version=1, timeout=3, retries=5, remote_port=161, use_long_names=True,
retry_no_such=True, abort_on_nonexistent=True)
output:
[]
Note: I followed all instructions and settings of the powerZook's installation guide and searched internet to find a solution but unfortunately I didn't find anything can help me
screenshot for Qtmib and return values from powerZook
enter image description here
The solution was as follow:
installing npcap (I'm not sure if this is part of the solution)
remove the last zero from OIDs and the two words "Current/AMPS"
the original OIDs from device installation guide was like:
'.1.3.6.1.4.1.19011.1.3.5.1.3.1.0 - Current/AMPS'
after modification become like:
'.1.3.6.1.4.1.19011.1.3.5.1.3.1'
I would like to thank and give my gratitude to #hansolo for his time and effort to help me.

GATT Characteristics with read-property not found by application

I am trying to develop an application that is communicating with an external device using BLE. I have decided to use pygatt (Python) with BGAPI (using a BlueGiga dongle).
The device I am communicating with has a custom primary service with a set of characteristics. According to their specs they have 2 READ characteristics, 8 NOTIFY chars and 1 WRITE char. Initially, I want to read one of the two READ chars, but I am unable to do so. Their UUIDs are not recognized as characteristics. How can this be? I am 100% certain that they are entered correctly.
import pygatt
import bleconnect
import blelib
import logging
logging.basicConfig()
logging.getLogger('pygatt').setLevel(logging.DEBUG)
adapter = pygatt.BGAPIBackend(serial_port='/dev/tty.usbmodem1')
adapter.start()
# Find the device
result = adapter.scan(timeout=5)
for item in result:
scan_name = item['name']
scan_rssi = item['rssi']
scan_address = item['address']
if scan_name == bleconnect.TARGET_NAME:
break
# Connect
device = adapter.connect(address=scan_address)
device.char_read(blelib.CHARACTERISTIC_DEVICE_FEATURES)
I can see in the debug messages that all the NOTIFY and WRITE characteristics are found, but not the two READ characteristics.
What am I missing?
This appears to be some kind of shortcoming in the pygatt API. I managed to find the actual value using bgapi only.

Using Python hidapi to open device with multiple usages

I'm new to the Python hidapi although I've used the C version that it is based on before. The Python library is really different and I can't figure out how to use it from the one example that is provided. Does anyone know of any good documentation for this library?
If you're looking for a specific question, I'm trying to open an HID device that has multiple usages. My device has the following relevant characteristics:
vendor_id: 10618
product_id: 4
usage: 8
usage_page: 1
interface_number: 1
I have tried using hid_enumerate to select the dictionary that I want but after instantiating the device object the device will not open even though I know it's there (since it is listed in enumerate).
Although I would still like to find some decent documentation, after using the C hidapi header for reference I found an answer to my original question. In order to specify the usage, you must use open_path() instead of the regular open() method (see below):
import hid
#Get the list of all devices matching this vendor_id/product_id
vendor_id = 10618
product_id = 4
device_list = hid.enumerate(vendor_id, product_id)
#Find the device with the particular usage you want
device_dict = (device in device_list if device['usage'] == '8').next()
device = hid.device()
device.open_path(device_dict['path']) #Open from path

scapy WARNING: can't import layer inet: 'module' object has no attribute 'IPPROTO_IPIP

I am using scapy with python 2.6.3 on windows 7
When I enter, I get a list of warnings, some less important but some like this
"WARNING: can't import layer inet: 'module' object has no attribute 'IPPROTO_IPIP"
when I try to send or receive packets I get errors.
I installed following instructions and downloading files from
http://www.secdev.org/projects/scapy/doc/installation.html#windows
Can someone help me figure out what I can do to fix this bug?
Trouble in using constant "socket.IPPROTO_IPIP", which is not implemented in this version of Python 2.
Look in file "scapy/layers/inet6.py", edit last string:
bind_layers(IPv6, IP, nh = socket.IPPROTO_IPIP )
replace with:
bind_layers(IPv6, IP, nh = 4 )
and delete inet6.pyc
Proof from IP protocol numbers
4 0x04 IP-in-IP IP in IP (encapsulation) RFC 2003

Reading iOS device generation and model number over USB using Python on Windows

Hello I have a GUI and I would like to detect iPod touches and iPhone's generations and model numbers over a usb connection on a desktop computer. I have seen libgpod but I use an windows machine and have little idea on how to install it (if its even compatible with windows)
My hope is that I can connect to the device, find and print the generation (it would be something like iPod 1,1 - iPod touch 1G) and then specify a file to download related to the device.
Well I wanted to give you a better answer with a code example, but this should get you started.
pywinusb is your best bet, as it reads more information more reliably about the plugged in devices. There are a few good examples for outputting connected devices and the like out there, here are some pretty good examples. You'll want to find the vendor_id or look for product_id 's for your usage, below is some example code.
from pywinusb import hid
# filter connected devices by vendor and product
filter = hid.HidDeviceFilter(vendor_id = 0x0777, product_id = 0x0077)
# filter connected devices by vendor
# filter = hid.HidDeviceFilter(vendor_id = 0x0777)
# filter connected devices by product
# filter = hid.HidDeviceFilter(product_id = 0x0077)
devices = filter.get_devices()
for device in devices:
print device
You can find more examples here.
Possibly try using win32com to list the Win32_USBHub devices from WMI??
import win32com.client
wmi = win32com.client.GetObject ("winmgmts:")
for usb in wmi.InstancesOf ("Win32_USBHub"):
print "DeviceID - " + usb.DeviceID
print "Name - " + usb.Name
print "Description - " + usb.Description
print "Caption - " + usb.Caption
print "SystemName - " + usb.SystemName
print "PNPDeviceID - " + usb.PNPDeviceID
print "\n\n"

Categories