#!/usr/bin/env bash
#===============================================================================
#
#          FILE: rescan_scsi.sh
# 
#         USAGE: ./rescan_scsi.sh 
# 
#   DESCRIPTION: 批量执行扫描 SCSI 总线
# 
#  ORGANIZATION: dqzboy.com
#       CREATED: 2021
#===============================================================================

echo
cat << EOF
██████╗  ██████╗ ███████╗██████╗  ██████╗ ██╗   ██╗            ██████╗ ██████╗ ███╗   ███╗
██╔══██╗██╔═══██╗╚══███╔╝██╔══██╗██╔═══██╗╚██╗ ██╔╝           ██╔════╝██╔═══██╗████╗ ████║
██║  ██║██║   ██║  ███╔╝ ██████╔╝██║   ██║ ╚████╔╝            ██║     ██║   ██║██╔████╔██║
██║  ██║██║▄▄ ██║ ███╔╝  ██╔══██╗██║   ██║  ╚██╔╝             ██║     ██║   ██║██║╚██╔╝██║
██████╔╝╚██████╔╝███████╗██████╔╝╚██████╔╝   ██║       ██╗    ╚██████╗╚██████╔╝██║ ╚═╝ ██║
╚═════╝  ╚══▀▀═╝ ╚══════╝╚═════╝  ╚═════╝    ╚═╝       ╚═╝     ╚═════╝ ╚═════╝ ╚═╝     ╚═╝
                                                                                          
EOF

# 重新扫描SCSI总线
function rescan_scsi_bus() {
    echo "Rescanning SCSI bus..."
    echo "- - -" > /sys/class/scsi_host/${1}/scan
}

# 获取 SCSI host
scsi_hosts=$(ls /sys/class/scsi_host/)

# 遍历每个 SCSI 主机并重新扫描总线
for host in $scsi_hosts; do
    rescan_scsi_bus $host
done

echo "SCSI bus rescan complete!"
