How do I export hard disk and enclosure information from my NAS using a script?


Last modified date: 2025-09-17

Applicable Products

NAS (QTS / QuTS Hero)


Scenario

If you use a QNAP NAS with an expansion unit, you may want to export detailed information about all installed hard drives—such as brand, model and serial number—for documentation or inventory. Although this information appears in the graphical interface, advanced users can retrieve and export these disk details using the command line via SSH. This is helpful for automation or to take inventory of your IT assets. 


Procedure

  1. Enable SSH access on your QNAP NAS.
    Note: Enabling SSH may introduce security risks. Ensure you use strong passwords and disable SSH when not in use.
  2. Connect to your NAS via SSH using an SSH client (such as PuTTY or Terminal).
  3. Use the provided hdd_id.sh script to export disk information:
    • Download or create the script: Save the following content as hdd_id.shon your NAS:
      #!/bin/sh
      # Usage:
      #   ./hdd_id.sh               # all enclosures, tab-separated (to stdout)
      #   ./hdd_id.sh --csv         # all enclosures, write to <HOST>-<YYYYMMDD>-DiskList.csv
      #   ./hdd_id.sh 1             # only enc_id=1, tab-separated
      #   ./hdd_id.sh --csv 1       # only enc_id=1, write to <HOST>-<YYYYMMDD>-DiskList.csv
      
      CSV=0
      if [ "$1" = "--csv" ]; then
        CSV=1
        shift
      fi
      
      if [ -n "$1" ]; then
        ENC_LIST="$1"
      else
        ENC_LIST=$(hal_app --se_enum 2>/dev/null | awk '
          /^=/{next}
          NF==0{next}
          /(^|[[:space:]])enc_id($|[[:space:]])/ { hdr=1; next }
          hdr && $1 ~ /^[0-9]+$/ { print $1 }
        ' | sort -n | uniq)
      fi
      
      [ -z "$ENC_LIST" ] && { echo "No enclosure enc_id found."; exit 1; }
      
      OUTFILE=""
      DELIM="\t"
      if [ $CSV -eq 1 ]; then
        HOSTNAME=$(uname -n 2>/dev/null)
        [ -z "$HOSTNAME" ] && HOSTNAME="NAS"
        DATESTR=$(date +%Y%m%d)
        OUTFILE="${HOSTNAME}-${DATESTR}-DiskList.csv"
        DELIM="," 
        echo "Enclosure,Slot,Brand,Model,Serial" > "$OUTFILE"
      fi
      
      for ENC in $ENC_LIST; do
        NAME=$(hal_app --se_get_jbod_display_name enc_id=$ENC 2>/dev/null)
        [ -z "$NAME" ] && NAME="ENC_${ENC}"
      
        hal_app --pd_enum enc_id=$ENC 2>/dev/null | awk -v name="$NAME" -v D="$DELIM" -v csv="$CSV" -v out="$OUTFILE" '
          /^=/{next}
          NF==0{next}
          !hdr && /(^|[[:space:]])port_id($|[[:space:]])/ && /vendor/ && /model/ && /serial_no/ {
            for(i=1;i<=NF;i++){
              if($i=="port_id")    pid=i
              if($i=="vendor")     bid=i
              if($i=="model")      mid=i
              if($i=="serial_no")  sid=i
            }
            hdr=1; next
          }
          hdr && $1 ~ /^[0-9]+$/ && pid>0 && bid>0 && mid>0 && sid>0 {
            line = name D $(pid) D $(bid) D $(mid) D $(sid)
            if (csv==1) {
              print line >> out
            } else {
              print line
            }
          }
        '
      done
              
    • Make the script executable:
      chmod +x hdd_id.sh
    • Run the script as needed:
      • ./hdd_id.sh — Lists all enclosures, outputs tab-separated data to the terminal.
      • ./hdd_id.sh --csv — Lists all enclosures, outputs to a CSV file named <HOST>-<YYYYMMDD>-DiskList.csv.
      • ./hdd_id.sh 1 — Lists only enclosure with enc_id=1, tab-separated.
      • ./hdd_id.sh --csv 1 — Lists only enclosure enc_id=1, outputs to CSV.
    • Example output:
      ENC_0	3	TOSHIBA	MC04ACA300E	85G9XXXXFLVA
      ENC_0	6	Seagate	ST8000VX010-2ZR188	WPV0XYZZ
      ENC_0	7	Seagate	ST8000VX010-2ZR188	WPV0ZZZZ
      ENC_0	8	Seagate	ST8000VX010-2ZR188	WPVXXXR5
      TR-004-N001	1	Seagate	ST4000NE001-2MA101	WS24ZZZB
      TR-004-N001	2	Seagate	ST4000NE001-2MA101	WS24XXXY
              

      Each line contains: Enclosure Name, Slot, Brand, Model, Serial.

    • Download the exported file (if using --csv) from your NAS using File Station or SMB/CIFS.



Further reading

Was this article helpful?

Thank you for your feedback.

Please tell us how this article can be improved:

If you want to provide additional feedback, please include it below.

Choose specification

      Show more Less

      Choose Your Country or Region

      back to top