USB
장치를 꽂은 시점을 알려주기 위해 node-detection
을 사용하고 있습니다. 그러나 어떤 포트에 연결했는지 알 수없는 것 같습니다. sdb1, sdc1 등 간단한 방법이 있습니까?노드에 연결된 장치의 USB 포트 가져 오기
3
A
답변
1
나는 그것이 어쩌면 노드 감지
하지 최선의 방법을 작동하지 않습니다 생각하지만, 이것은 나를 위해 일한 :
var shell = require('shelljs');
var watch = require('node-watch');
var tmpUsb;
Array.prototype.contains = function(needle){
for (var i=0; i<this.length; i++)
if (this[i] == needle) return true;
return false;
}
Array.prototype.diff = function(compare) {
return this.filter(function(elem) {return !compare.contains(elem);})
}
getUsb = function() {
this.usbJson = JSON.parse(shell.exec('lsblk --json', {silent:true}).stdout);
var dev = this.usbJson.blockdevices;
var devices = [];
dev.forEach(function(entry) {
entry.children.forEach(function(e) {
devices.push(e.mountpoint);
});
});
return devices;
}
tmpUsb = getUsb();
watch('/dev/disk/by-id', { recursive: true }, function(evt, name) {
var curr = getUsb();
var add = curr.diff(tmpUsb);
var rem = tmpUsb.diff(curr);
if(add.length > 0) {
console.log("ADD > " + add);
}
if(rem.length > 0) {
console.log("REM > " + rem);
}
tmpUsb = curr;
});
당신의 출력이 무엇인지 질문을 추가하십시오. 어떤 코드를 시도 했습니까?
2
resin.io의 우수한 사람들이 만든 노드 감지 모듈 외에도 drivelist 모듈을 사용할 수 있습니다.
drivelist 모듈은 마운트 포인트 (경로) 정보를 포함하여 연결된 모든 장치를 나열합니다. 이 출력은 운영 체제에 따라 약간 다릅니다. 자세한 정보는 구동 장치 설명서를 확인하십시오.
다음은 이동식 USB 드라이브가 삽입되어 마운트 된 시간을 확인하고 마운트 경로를 얻는 간단한 예입니다.
일부 테스트에서 USB 연결 감지 모듈은 장치가 연결될 때 거의 순간적으로 작동하지만 장치를 장착하는 데는 시간이 걸립니다. 그래서 나는 마운트 포인트가 있는지 계속 확인하기 위해 setInterval을 사용하고 있습니다. 마운트 포인트를 찾으면 간격을 지 웁니다.
// load modules
var usbDetect = require('usb-detection');
const drivelist = require('drivelist');
var checkUSBintervalID;
// This is the listener function for the 'add' event
function addUSBhandler() {
console.log('added event listener for usb ADD');
// Start interval to check if the USB is mounted
checkUSBintervalID = setInterval(function() {
drivelist.list(function(error, drives) {
console.log('listening for USB mount...');
if (error) { throw error; }
// iterate through all drives
for(var d = 0; d < drives.length; d++) {
// if the drive is a removable usb
if(drives[d].system == false) {
// if this drive is mounted
if(drives[d].mountpoints.length > 0) {
console.log('found removable USB');
// the drive is mounted
// do stuff here
// the path is at drives[d].mountpoints[0].path
clearInterval(checkUSBintervalID);
}
}
}
});
}, 1000);
}
// Add listener for when a USB is plugged in
usbDetect.on('add', addUSBhandler);
이 대답은 장착하기 전에 USB를 넣고 뺄 때 고려하지 않습니다. 아마 시간 초과 또는 비슷한 것을 추가해야 할 것입니다. 이것이 도움이되고 행운을 비네.
0
나는 Node.js를 바인딩에 대해 알고 있지만, 정보가 노출되어 외부 udevadm
도구를 사용하지 않습니다
$ udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
KERNEL[766928.886896] add /devices/pci0000:00/0000:00:10.0/usb5/5-1 (usb)
KERNEL[766928.888766] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0 (usb)
UDEV [766929.091194] add /devices/pci0000:00/0000:00:10.0/usb5/5-1 (usb)
KERNEL[766930.777996] add /module/usb_storage (module)
UDEV [766930.780203] add /module/usb_storage (module)
KERNEL[766930.781054] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4 (scsi)
KERNEL[766930.781241] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/scsi_host/host4 (scsi_host)
KERNEL[766930.781370] add /bus/usb/drivers/usb-storage (drivers)
UDEV [766930.782320] add /bus/usb/drivers/usb-storage (drivers)
KERNEL[766931.092511] add /module/uas (module)
UDEV [766931.092584] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0 (usb)
KERNEL[766931.092608] add /bus/usb/drivers/uas (drivers)
UDEV [766931.093550] add /module/uas (module)
UDEV [766931.094000] add /bus/usb/drivers/uas (drivers)
UDEV [766931.094305] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4 (scsi)
UDEV [766931.095737] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/scsi_host/host4 (scsi_host)
KERNEL[766931.807635] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0 (scsi)
KERNEL[766931.807770] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0 (scsi)
KERNEL[766931.807901] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_disk/4:0:0:0 (scsi_disk)
KERNEL[766931.807995] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_device/4:0:0:0 (scsi_device)
KERNEL[766931.809543] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_generic/sg2 (scsi_generic)
KERNEL[766931.809605] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/bsg/4:0:0:0 (bsg)
UDEV [766931.811736] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0 (scsi)
KERNEL[766931.812771] add /devices/virtual/bdi/8:16 (bdi)
UDEV [766931.815246] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0 (scsi)
UDEV [766931.815908] add /devices/virtual/bdi/8:16 (bdi)
UDEV [766931.818228] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_disk/4:0:0:0 (scsi_disk)
UDEV [766931.819748] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/bsg/4:0:0:0 (bsg)
UDEV [766931.821506] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_device/4:0:0:0 (scsi_device)
UDEV [766931.821894] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_generic/sg2 (scsi_generic)
KERNEL[766931.895701] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb (block)
KERNEL[766931.895807] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb1 (block)
KERNEL[766931.895879] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb2 (block)
UDEV [766933.474661] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb (block)
UDEV [766933.566443] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb2 (block)
UDEV [766933.658413] add /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb1 (block)
KERNEL[766942.802911] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/bsg/4:0:0:0 (bsg)
KERNEL[766942.804048] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_generic/sg2 (scsi_generic)
KERNEL[766942.804163] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_device/4:0:0:0 (scsi_device)
KERNEL[766942.804246] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_disk/4:0:0:0 (scsi_disk)
KERNEL[766942.804338] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb2 (block)
KERNEL[766942.804426] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb1 (block)
KERNEL[766942.804518] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb (block)
KERNEL[766942.804590] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0 (scsi)
UDEV [766942.806838] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/bsg/4:0:0:0 (bsg)
UDEV [766942.807543] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_generic/sg2 (scsi_generic)
UDEV [766942.809181] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_disk/4:0:0:0 (scsi_disk)
UDEV [766942.809410] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/scsi_device/4:0:0:0 (scsi_device)
UDEV [766942.812382] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb1 (block)
UDEV [766942.812545] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb2 (block)
UDEV [766942.814711] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0/block/sdb (block)
UDEV [766942.815300] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0/4:0:0:0 (scsi)
KERNEL[766942.816341] remove /devices/virtual/bdi/8:16 (bdi)
KERNEL[766942.816384] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0 (scsi)
UDEV [766942.817716] remove /devices/virtual/bdi/8:16 (bdi)
UDEV [766942.819175] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/target4:0:0 (scsi)
KERNEL[766942.864803] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/scsi_host/host4 (scsi_host)
KERNEL[766942.864891] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4 (scsi)
KERNEL[766942.865168] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0 (usb)
KERNEL[766942.866960] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1 (usb)
UDEV [766942.867670] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4/scsi_host/host4 (scsi_host)
UDEV [766942.868469] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0/host4 (scsi)
UDEV [766942.869291] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1/5-1:1.0 (usb)
UDEV [766942.870791] remove /devices/pci0000:00/0000:00:10.0/usb5/5-1 (usb)
일부 노트 :
- A의 하나 개 이상의 파티션이있을 수 있습니다 USB 스틱 또는 파티션없이 파일 시스템을 직접 가질 수 있습니다.
- USB 장치를 특별히 모니터 할 필요가 없을 수도 있습니다. 아마도 블록 장치일까요?
- 이것은 필터링을 수행하지 않으므로 많은 관련이없는 이벤트가 발생합니다.
node-usb-detection? npmjs에? 그것의 기반으로 usb (npmjs). 항구를 얻기가 어렵습니다. 또한 그것을 찾고. 해결책을 찾으면 게시 할 예정입니다. –