识别内部驱动器与USB驱动器bash

时间:2013-09-30 16:20:43

标签: bash

我认为我有一个很好的方法来确定哪些驱动器是内部驱动器,哪些驱动器是外部驱动器(USB)。直到我遇到这个:

的/ var /日志/消息*

    Sep 24 03:40:02 myHostName kernel: sd 0:0:0:0: [sda] 3907029164 512-byte logical blocks: (2.00 TB/1.81 TiB)
    Sep 24 03:40:02 myHostName kernel: sd 1:0:0:0: [sdb] 976773168 512-byte logical blocks: (500 GB/465 GiB)
    Jul 21 14:57:01 myHostName kernel: sd 6:0:0:0: [sdb] 3907029164 512-byte logical blocks: (2.00 TB/1.81 TiB)
    Jul 22 06:45:39 myHostName kernel: sd 1:0:0:0: [sdb] 976773168 512-byte logical blocks: (500 GB/465 GiB)
    Jul 22 15:06:30 myHostName kernel: sd 0:0:0:0: [sda] 3907029164 512-byte logical blocks: (2.00 TB/1.81 TiB)
    Jul 22 15:06:30 myHostName kernel: sd 1:0:0:0: [sdb] 976773168 512-byte logical blocks: (500 GB/465 GiB)
    Jul 25 15:13:06 myHostName kernel: sd 7:0:0:0: [sdc] 3907029164 512-byte logical blocks: (2.00 TB/1.81 TiB)
    Jul 26 15:19:39 myHostName kernel: sd 0:0:0:0: [sda] 3907029164 512-byte logical blocks: (2.00 TB/1.81 TiB)
    Jul 26 15:19:39 myHostName kernel: sd 1:0:0:0: [sdb] 976773168 512-byte logical blocks: (500 GB/465 GiB)
    Jul 26 15:23:28 myHostName kernel: sd 0:0:0:0: [sda] 976773168 512-byte logical blocks: (500 GB/465 GiB)

此系统上的SDA和SDB驱动器从内部交换到外部!有谁知道我如何识别哪些设备是内置的,哪些是外置的?我需要查看HISTORY,所以我真的需要能够回顾以前安装的设备。我的旧方法是查看“sdb-sdX”忽略sda的所有内容。这在上面的机器上是有缺陷的。我甚至都不知道它是怎么做到的。一直以为SDA是第一个内部设备。

2 个答案:

答案 0 :(得分:3)

我用它来识别驱动器是否可弹出(USB): -

diskutil info /dev/disk[n|s{n}] | grep Ejectable

“磁盘工具”为您提供所有磁盘的序列号。

答案 1 :(得分:0)

在我的RHEL6系统上,我能够进行以下查询以获得我正在寻找的输出:

cat /var/log/messages* | egrep -i 'usb [0-9]-|mounted|logical blocks' > ${USBDATA}
cat ${USBDATA} | grep "New USB device found" > ${NewUSBDev}


while read line; do

    MONTH="`echo ${line} | awk '{print $1}'`" ;  # -- Jul
    DAY="`echo ${line} | awk '{print $2}'`" # -- day_
    HOUR="`echo ${line} | awk '{print $3}' | cut -d':' -f1`:" # -- hour:
    MIN="`echo ${line} | awk '{print $3}' | cut -d':' -f2`:" # -- hour:

    # -- echo "M:[$MONTH]  D:[$DAY]  H:[${HOUR}${MIN}]"

    cat ${USBDATA} | grep ${MONTH} | grep "${DAY} ${HOUR}${MIN}" > ${CHUNK}

    awk '/New USB device found/{p++}p==1' ${CHUNK} > ${TMPFILE}
    cp ${TMPFILE} ${CHUNK}

    if [ "$(cat $CHUNK| egrep 'MiB|GiB|TiB')" ] && ! [ "$(cat $CHUNK | grep 'Mounted root filesystem')" ] ;then
        # -- cat ${CHUNK}
        STR="$(less ${CHUNK}|grep 'idProduct' | head -1 | awk '{print $1" "$2" "$3" "$12" "$13}')"
        STR="$STR $(less ${CHUNK}|grep 'Product:' | awk '{print ", "$9" "$10" "$11}')"
        STR="$STR $(less ${CHUNK}|grep 'Manufacturer:' | awk '{print ", "$9" "$10" "$11}')"
        STR="$STR $(less ${CHUNK}|grep 'SerialNumber:' | awk '{print ", "$9}')"
        STR="$STR $(less ${CHUNK}|grep 'logical blocks:' | head -1 | awk '{print ", "$13" "$14" "$15" "}')"
        echo "${STR}"
        echo "${STR}" >> ${DEVICES}
    fi

    [ -f ${CHUNK} ] && rm ${CHUNK}

done < ${NewUSBDev}
echo "----------------------------------------------"
cat ${DEVICES} | sort -M -k 2 > ${SORTEDDEV}
cat ${SORTEDDEV}

基本上,它有点难看,但它会搜索所有“找到的新USB设备”,并列出包含该列表的行。然后拉出与该时间戳匹配的所有内容。然后在“找到新USB设备”之前拔出所有内容,并在“找到新USB设备”后第二次匹配后拔出所有内容。

示例$ {CHUNK}的数据如下所示:

    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: New USB device found, idVendor=1058, idProduct=071a
    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: Product: My Passport 071A
    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: Manufacturer: Western Digital
    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: SerialNumber: 575846314136304130353235
    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: configuration #1 chosen from 1 choice
    Sep 30 09:55:22 oc7446274465 kernel: sd 5:0:0:0: [sdb] 1465092096 512-byte logical blocks: (750 GB/698 GiB)

将此数据放入临时文件中,然后查找重要位。这可以肯定地改进,但它现在可以工作(上面的代码中不包括grepping部分):

示例输出:

    Sep 10 16:19:14 idVendor=8564, idProduct=1000 , Mass Storage Device , JetFlash   , 2978449637 , (31.6 GB/29.4 GiB) 
    Sep 14 15:15:04 idVendor=054c, idProduct=0544 , DSC-HX9V   , Sony   , D89BE0866 , (20.0 MB/19.1 MiB) 
    Sep 17 19:38:04 idVendor=054c, idProduct=0544 , DSC-HX9V   , Sony   , D89BE0866 , (31.9 GB/29.7 GiB) 
    Sep 19 05:23:07 idVendor=054c, idProduct=0544 , DSC-HX9V   , Sony   , D89BE0866 , (31.9 GB/29.7 GiB) 
    Sep 30 09:55:10 idVendor=1058, idProduct=071a , My Passport 071A , Western Digital  , 57584631413630XXXXXX235 , (750 GB/698 GiB) 

因此,我得到时间,ID信息,产品名称,序列等。我应该包括检查数据是否缺失。但你明白了。感谢投入的人。