Usb serial number linux

How do I find the Serial Number of a USB Drive?

I’m trying to re-enable USB Autoplay in a secure way, by installing a program on each of the computers that I use so that I can run my launcher (PStart in this case) whenever I plug in my specific USB drive. The tool that I’m using to enable this — AutoRunGuard — needs the serial number of the USB drive that I am using. I can’t figure out where to find this in Windows. Ideally I would not need to install and run a separate program to do this (seemingly) simple task. Since this is a pretty easy question, bonus points if you also tell me how to discover it in Linux as well. What steps do I need to take to retrieve a USB Drive’s serial number? UPDATE: Just incase people come here looking for the answer for AutoRunGuard, I discovered that they don’t want the USB device serial number, but the volume serial number. The drive serial can be found by going into the command line, navigating to the drive, and executing dir . The volume serial number is found in the top two lines — use it without the dash.

Just for completeness— if you’re looking for how to do this on MacOS and you arrived here, check out apple.stackexchange.com/questions/275382/…

7 Answers 7

USBDeview is a small utility that lists all USB devices that currently connected to your computer, as well as all USB devices that you previously used. For each USB device, exteneded information is displayed: Device name/description, device type, serial number (for mass storage devices), the date/time that device was added, VendorID, ProductID, and more. USBDeview also allows you to uninstall USB devices that you previously used, and disconnect USB devices that are currently connected to your computer. You can also use USBDeview on a remote computer, as long as you login to that computer with admin user.

On Linux, usbview will do this, just click on the device in the left pane.

1. He wants to use the information on a MSWindows box, but getting the info on Linux machine probably doesn’t require installing anything. 2. If you read the post, he said: bonus points if you also tell me how to discover it in Linux as well.

Get USB Serial Numbers on Windows with PowerShell

Here’s a PowerShell solution that will give you the serial number of all «USB Mass Storage Devices» mounted on the system which you run it. It uses Get-CIMInstance to query the Win32_PnPSignedDriver class to get the property values with filtering, looping, setting a couple variables, using a method, etc.

Within the PowerShell script below, I left some commented out logic and comments for running with the legacy Get-WMIObject cmdlet for systems with versions below PowerShell 3.0.

PowerShell

$DevId = (((Get-CimInstance -Class win32_PnPSignedDriver) | ?<($_.Description -like '*mass*')>).DeviceID); $DevSerial = @($DevId | %); $DevSerial ##### // Everything below is commented out with comments for each section \\ ##### ## -- See everything or the selected properties per above #((Get-CimInstance -Class win32_PnPSignedDriver) | ?<($_.Description -like '*mass*')>) | #Select Description, DeviceClass, DeviceID, Manufacturer ## -- Correlated legacy PS code older than PowerShell version 3 #$DevId = ((Get-WmiObject Win32_USBControllerDevice | % | ?<($_.Description -like '*mass*')>).DeviceID); #$DevSerial = @($DevId | %); #$DevSerial ## -- See everything or selected properties per above legacy PS code #Get-WmiObject Win32_USBControllerDevice | % | ? <($_.Description -like '*mass*')>| #Select Description, DeviceID, Manufacturer, Service 

Supporting Resources

The ‘ ? ‘ symbol and Where are both aliases for Where-Object. If you explicitly want to run the Where-Object command, run Where-object or ‘ ? ‘

Источник

USB-drive serial number under linux C++

Is there any way to determine s/n of usb-drive in linux using C++ ? If not C++ is there any other way different from hwinfo -disk and hdparm -i ?

4 Answers 4

I’ll try to summarize my experience regarding storage drive serial number retrieval on linux.
I assume you want the serial number of the storage device identity (as per SCSI specification) not the serial number of the USB device (as per USB specification under Device Descriptor ), these two are different entities.

NOTICE!
Most devices tend to implement a serial number in the USB-Controller and leave the serial number of the inner SCSI-disk unimplemented.
So if you want to uniquely identify an USB-device the best way is to create a string from the Device Descriptor (USB specification) like VendorId-ProductId-HardwareRevision-SerialNumber
In the following I shall describe how to retrieve the SN of the storage drive, as asked.

Drives fall in 2 categories (actually more, but let’s simplify): ATA-like (hda, hdb . ) and SCSI-like (sda sdb . ). USB drives fall in the second category, they are called SCSI attached disks. In both situation ioctl calls can be used to retrieve the required information (in our case the serial number).

For SCSI devices (and these include USB drives) the Linux generic driver and it’s API is documented at tldp.
The serial number on SCSI devices is available inside the Vital Product Data (short: VPD) and is retrievable by using the SCSI Inquiry Command. A commad line utility in linux that can fetch this VPD is sdparm:

> yum install sdparm > sdparm --quiet --page=sn /dev/sda Unit serial number VPD page: 3BT1ZQGR000081240XP7 

Note that not all devices have this serial number, the market is flooded with cheep knockoffs, and some usb flash disks return strange serials (for example my sandisk cruzer returns just the letter «u»). To overcome this some people choose to create a unique identifier by mixing different strings from VPD like Product ID, Vendor ID and Serial Number.

#include #include #include #include #include #include #include #include #include int scsi_get_serial(int fd, void *buf, size_t buf_len) < // we shall retrieve page 0x80 as per http://en.wikipedia.org/wiki/SCSI_Inquiry_Command unsigned char inq_cmd[] = ; unsigned char sense[32]; struct sg_io_hdr io_hdr; int result; memset(&io_hdr, 0, sizeof (io_hdr)); io_hdr.interface_id = 'S'; io_hdr.cmdp = inq_cmd; io_hdr.cmd_len = sizeof (inq_cmd); io_hdr.dxferp = buf; io_hdr.dxfer_len = buf_len; io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; io_hdr.sbp = sense; io_hdr.mx_sb_len = sizeof (sense); io_hdr.timeout = 5000; result = ioctl(fd, SG_IO, &io_hdr); if (result < 0) return result; if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) return 1; return 0; >int main(int argc, char** argv) < char *dev = "/dev/sda"; char scsi_serial[255]; int rc; int fd; fd = open(dev, O_RDONLY | O_NONBLOCK); if (fd < 0) < perror(dev); >memset(scsi_serial, 0, sizeof (scsi_serial)); rc = scsi_get_serial(fd, scsi_serial, 255); // scsi_serial[3] is the length of the serial number // scsi_serial[4] is serial number (raw, NOT null terminated) if (rc < 0) < printf("FAIL, rc=%d, errno=%d\n", rc, errno); >else if (rc == 1) < printf("FAIL, rc=%d, drive doesn't report serial number\n", rc); >else < if (!scsi_serial[3]) < printf("Failed to retrieve serial for %s\n", dev); return -1; >printf("Serial Number: %.*s\n", (size_t) scsi_serial[3], (char *) & scsi_serial[4]); > close(fd); return (EXIT_SUCCESS); > 

For the sake of completness i’ll also provide the code to retrieve the serial number for ATA devices (hda, hdb . ). This will NOT work for USB devices.

#include #include #include #include #include #include #include #include #include int main() < struct hd_driveid *id; char *dev = "/dev/hda"; int fd; fd = open(dev, O_RDONLY|O_NONBLOCK); if(fd < 0) < perror("cannot open"); >if (ioctl(fd, HDIO_GET_IDENTITY, id) < 0) < close(fd); perror("ioctl error"); >else < // if we want to retrieve only for removable drives use this branching if ((id->config & (1 command_set_1 & 4)) < close(fd); printf("Serial Number: %s\n", id->serial_no); > else < perror("support not removable"); >close(fd); > > 

Источник

USB serial number not shown with lsusb -v command

I have a USB-Stick from which I would like to read the serial number. If I invoke the command lsusb -v the output in the line iSerial is as follows:

Host scsi10: usb-storage Vendor: USB Product: Disk 2.0 Serial Number: 92071573E1272519149 Protocol: Transparent SCSI Transport: Bulk Quirks: 

Why is there no serial output with the lsusb command on the one hand, but on the other hand I get a serial number from /proc/scsi/usb-storage . Where is the difference between the two methods to gather the serial?

3 Answers 3

lsusb may try to open the USB device as O_RDWR (read/write mode) and your user might not have the rights to do this (some error message «Couldn’t open device, some information will be missing» should be inbetween the output, if so). Started as root lsusb should also be able to output the whole iSerial value.

There are lots of different USB devices in general (keyboard, mice, webcams, . ). lsusb deals with the connected devices on the USB protocol level.

Some USB device are storage devices (USB sticks, USB harddisks, . ). They understand a different protocol (more or less SCSI) on top of the USB protocol. Within this protocol, an USB storage device has a serial number (as does an ATA device). This is what you see in /proc/scsi/usb-storage .

The iSerial number you see in lsusb has nothing to do with it.

So that’s why you see the serial number you are interested in with one method, but not with the other method. And that’s why you can’t use lsusb to get the kind of serial number you are interested in

You can use lsusb with verbose flag, but you need to make sure you use sudo with it, otherwise the serial will be incorrect.

If that is too verbose, then run lsusb to get the device id:

$ lsusb Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 012: ID 1ab1:0e11 Rigol Technologies Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub 

Then run lsusb with -s flag to show only specified device and grep for the serial number.

So for the serial number of Rigol device:

$ sudo lsusb -s 012 -v|grep -i iserial iSerial 3 DP8C221100000 

For more information on lsusb use the —help flag:

$ lsusb --help Usage: lsusb [options]. List USB devices -v, --verbose Increase verbosity (show descriptors) -s [[bus]:][devnum] Show only devices with specified device and/or bus numbers (in decimal) -d vendor:[product] Show only devices with the specified vendor and product ID numbers (in hexadecimal) -D device Selects which device lsusb will examine -t, --tree Dump the physical USB device hierarchy as a tree -V, --version Show version of program -h, --help Show usage and help 

Источник

Get unique serial number of USB device mounted to /dev folder

Just ran into this same problem and it took a bit to find the solution. Any solution which starts with «just use lsusb» is incorrect. You can figure out the devices serial, but none of the extra information it provides help you determine which /dev/video it links to.

/bin/udevadm info --name=/dev/video1 | grep SERIAL_SHORT 
E: ID_SERIAL_SHORT=256DEC57 

@debuti This problem came from your USB devices. They were produced with same serial number or they haven’t serial number capability which cause the same serial number. You can reconfigure their serial number for some hardware. For example you can use CP210xSetIDs.exe if you are using CP2102 USB to UART.

Based on the hint of using udevadm and the tutorial from http://www.signal11.us/oss/udev/ I got below code to get the serial info of my webcam.

#include "stdio.h" #include int main(int argc, char **argv) < struct udev *udev; struct udev_device *dev; struct udev_enumerate *enumerate; struct udev_list_entry *list, *node; const char *path; udev = udev_new(); if (!udev) < printf("can not create udev"); return 0; >enumerate = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(enumerate, "video4linux"); udev_enumerate_scan_devices(enumerate); list = udev_enumerate_get_list_entry(enumerate); udev_list_entry_foreach(node, list) < path = udev_list_entry_get_name(node); dev = udev_device_new_from_syspath(udev, path); printf("Printing serial for %s\n", path); printf("ID_SERIAL=%s\n", udev_device_get_property_value(dev, "ID_SERIAL")); printf("ID_SERIAL_SHORT=%s\n", udev_device_get_property_value(dev, "ID_SERIAL_SHORT")); udev_device_unref(dev); >return 0; > 

You can use lsusb , but you need to add verbose flag and make sure you use sudo with it, otherwise the serial will be incorrect.

If that is too verbose, then run lsusb to get the device id:

$ lsusb Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 012: ID 1ab1:0e11 Rigol Technologies Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub 

Then run lsusb with device flag and grep the serial number.

So for the serial number of Rigol device:

$ sudo lsusb -s 012 -v|grep -i iserial iSerial 3 DP8C221100000 

Thanks! This reply deserves to receive a lot more attention IMO, because it (ie lsusb with the verbose flag) is likely to be what a lot of people coming here are looking for 🙂

Playing around with libusb, it looks like there’s a standard getSerialNumber() method. Unfortunately, not all USB devices implement this. I have a couple cheap $4 webcams that return None for it. These interfaces expose other metadata, like VendorID and ProductID, which I’ve seen some code try and use as a unique identifier, but it’s not guaranteed to be unique, especially if you have multiple devices of the same make and model.

But assuming you get a serial number for your device, the next problem is figuring out which /dev/videoN file it corresponds to. I have an old version of libusb installed, so I couldn’t get the method working that returned the full sysfs path of the USB device, so instead I scrapped the output from hwinfo . I extracted all the chunks corresponding to cameras, and then from those I extracted the piece that looked like:

USB devices actually form a complicated tree, and that BusID encodes where the device is located in that tree.

You can then take that BusID to find where the device lives in the filesystem as well as the video path, which should be at:

/sys/bus/usb/devices//video4linux/ 

That’s a directory, and inside it you’ll find a videoN file matching one in /dev.

Источник

Читайте также:  Replace text in pdf linux
Оцените статью
Adblock
detector