Skip to content

Instantly share code, notes, and snippets.

@sleekweasel

sleekweasel/huh.txt

Last active Mar 6, 2019
Embed
What would you like to do?
Duplicated Android device id?!
#!/bin/bash
usbreset_serials() {
echo Resetting.. May cause devices to move - files to ioctl/vanish.
xargs -t |
sed 's/\/serial/\/uevent/g' |
xargs -tr grep -h DEVNAME= |
sed 's/DEVNAME=/\/dev\//' |
xargs -trn1 sudo $(dirname $0)/usbreset
}
ANDROIDS=$(grep -li android /sys/bus/usb/devices/*/product | sed -e 's/product$//')
echo Found $(echo -$ANDROIDS | wc -w) /sys/bus/usb/devices matching android.
# Check vs duplicate serial numbers !?
for i in $ANDROIDS ; do
if [ -s $i/serial ] ; then
SERIAL=$(cat $i/serial)
MATCHES=$(grep -l $SERIAL /sys/bus/usb/devices/*/serial)
if [ $(echo $MATCHES | wc -w) -ne 1 ] ; then
echo Duplicates found for $SERIAL: $MATCHES
echo $MATCHES | usbreset_serials
fi
else
echo Empty serial file! $i/serial
fi
done
fgrep -l "$(adb devices|awk '/offline/{print $1}'; echo DUMMY)" /sys/bus/usb/devices/*/serial |
usbreset_serials
automation2.d4:~> grep -li android /sys/bus/usb/devices/*/product | sed -e 's/product/serial/' | xargs grep . | fgrep -F "$(adb devices | awk '/device$/{print $1}')"
/sys/bus/usb/devices/1-1.4.4.4.1/serial:ad0717021c004d53c0
/sys/bus/usb/devices/1-1.4.4.4.2/serial:ad07170216aa8a8b69
/sys/bus/usb/devices/1-1.4.4.4.3.1/serial:ad0717021c0042fa80 <---- duplicate!?
/sys/bus/usb/devices/1-1.4.4.4.3.2/serial:ad07170216aa9e9220
/sys/bus/usb/devices/1-1.4.4.4.3.3/serial:ad0717021cac466269
/sys/bus/usb/devices/1-1.4.4.4.6/serial:ad07170215ad6902ce
/sys/bus/usb/devices/1-1.4.4.4.7.2/serial:ad0517021c8b697b63
/sys/bus/usb/devices/1-1.4.4.4.7.4/serial:ad0717021c0042fa80 <---- duplicate!?
automation2.d4:~> adb devices | awk '/device$/{print $1}'|cat -n
1 ad0717021cac466269
2 ad0717021c0042fa80
3 ad0517021c8b697b63
4 ad0717021c004d53c0
5 ad07170216aa9e9220
6 ad07170216aa8a8b69
7 ad07170215ad6902ce
automation2.d4:~> cat /sys/bus/usb/devices/1-1.4.4.4.3.1/product /sys/bus/usb/devices/1-1.4.4.4.7.4/product
SAMSUNG_Android
SAMSUNG_Android
automation2.d4:~> uname -a
Linux automation2 4.14.75-0.2-default #1 SMP PREEMPT Mon Nov 13 14:53:06 UTC 2017 (c152297) x86_64 x86_64 x86_64 GNU/Linux
/* usbreset -- send a USB port reset to a USB device */
/** cc usbreset.c -o usbreset **/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
int main(int argc, char **argv)
{
const char *filename;
int fd;
int rc;
if (argc != 2) {
fprintf(stderr, "Usage: usbreset device-filename\n");
return 1;
}
filename = argv[1];
fd = open(filename, O_WRONLY);
if (fd < 0) {
perror("Error opening output file");
return 1;
}
printf("Resetting USB device %s\n", filename);
rc = ioctl(fd, USBDEVFS_RESET, 0);
if (rc < 0) {
perror("Error in ioctl");
return 1;
}
printf("Reset successful\n");
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment