You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写Shell脚本批量检查Linux主机存储挂载及可访问性

Batch Check Mount Points & Accessibility with pdsh

Alright, let's build a robust shell script using pdsh to batch check mount points and their accessibility across your Linux hosts. I'll walk you through the complete script and break down how each part works so you can tweak it to fit your exact needs.

Complete Script

#!/bin/bash

# -------------------------- Configuration Section --------------------------
# Replace with your target hosts: can be a space-separated list or read from a file
HOST_LIST="web01 db01 storage01 backup01"
# Add all mount points you want to check to this array
MOUNT_POINTS=(
    "/data"
    "/backup"
    "/shared_nfs"
    "/mnt/ssd_storage"
)
# ---------------------------------------------------------------------------

# Loop through each mount point to run checks across all hosts
for mount_point in "${MOUNT_POINTS[@]}"; do
    echo "=== Starting check for mount point: *$mount_point* ==="
    
    # Use pdsh to execute remote check commands
    pdsh -w "$HOST_LIST" << 'REMOTE_CMD'
TARGET_MOUNT="$1"

# 1. Check if the mount point is actively mounted (using findmnt for reliability)
if ! findmnt -n -o TARGET "$TARGET_MOUNT" > /dev/null 2>&1; then
    echo "ERROR: Not mounted"
    exit 1
fi

# 2. Check if the mount point is accessible (adjust test condition as needed)
# Use test -x for "can enter directory" (basic accessibility)
# Use test -w for "can write to directory" if you need to verify write access
if ! test -x "$TARGET_MOUNT"; then
    echo "ERROR: Not accessible (permission denied or invalid path)"
    exit 1
fi

# If all checks pass
echo "OK: Mounted and accessible"
REMOTE_CMD
    # Parse pdsh output to highlight errors clearly
    | awk -v mp="$mount_point" '{
        if ($0 ~ /ERROR:/) {
            # Extract hostname from pdsh's [host]: output format
            host = substr($1, 2, length($1)-2);
            # Extract error message
            sub(/^\[[^]]+\]: /, "", $0);
            printf "[FAIL] %s | Mount point: %s | Reason: %s\n", host, mp, $0;
        }
    }'
    
    echo "" # Add blank line between mount point checks for readability
done

echo "=== All checks completed ==="

Key Features & Explanations

  • Reliable Mount Check: Uses findmnt instead of mount | grep because it's purpose-built for querying mount information, avoiding false positives from partial matches in mount output.
  • Customizable Accessibility Check: The script uses test -x to verify you can enter the directory (basic accessibility). Swap this with test -w if you need to confirm write access for a storage location.
  • Clear Error Output: Uses awk to parse pdsh's default [hostname]: output format and print human-readable failure messages with the host, mount point, and reason.
  • Easy Configuration: Just update the HOST_LIST and MOUNT_POINTS array at the top to match your environment.

Usage Tips

  1. Prerequisites: Ensure pdsh is installed on your control machine, and you've set up passwordless SSH access to all target hosts (otherwise you'll be stuck entering passwords for each host).
  2. Host List from File: If your hosts are stored in a text file (one per line), replace the HOST_LIST line with:
    HOST_LIST=$(cat /path/to/your/hosts.txt)
    
  3. Test First: Run a quick test on a single host to validate the check logic before batch running:
    pdsh -w web01 "findmnt /data && test -x /data && echo OK"
    

内容的提问来源于stack exchange,提问作者Dr.Bake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:40:22