หมวดหมู่: Samba

Samba: Active Directory Domain Controller

Samba: เพิ่ม users ทีละเยอะ ๆSamba: เพิ่ม users ทีละเยอะ ๆ

เป็น shell script ที่ไว้เพิ่ม user เข้า samba ad ที่ละเยอะ ๆ อย่างการที่จะโอน migrate จากระบบเก่ามาที่ละเยอะ ๆ

  1. สร้างไฟล์
    touch add_mass_users.sh
  2. ใส่เนื้อหา
    #!/bin/bash # ─── Defaults ───────────────────────────────────────────────────────────────
    CONTAINER="ad-samba-m-dev"
    BASE_DN="DC=phunsanit,DC=local"
    DEFAULT_PASSWORD="Password123!" # ─── Usage ──────────────────────────────────────────────────────────────────
    usage () { echo "Usage: $0 [OPTIONS]" echo "" echo "Options:" echo " -c, --container Docker container name (default: $CONTAINER) " echo " -b, --base-dn Base DN for LDAP (default: $BASE_DN) " echo " -p, --password Default user password (default: $DEFAULT_PASSWORD) " echo " -h, --help Show this help message" echo "" echo "Example:" echo " $0 --container ad-samba-m-dev --base-dn DC=corp,DC=local --password P@ssw0rd!" exit 0
    } # ─── Parse Parameters ────────────────────────────────────────────────────────
    while [[ $# -gt 0 ]]; do case "$1" in -c|--container) CONTAINER="$2" shift 2 ;; -b|--base-dn) BASE_DN="$2" shift 2 ;; -p|--password) DEFAULT_PASSWORD="$2" shift 2 ;; -h|--help) usage ;; *) echo "❌ Unknown option: $1" usage ;; esac
    done echo "=========================================="
    echo " Adding Mass Users to Samba AD DC"
    echo " Container : $CONTAINER"
    echo " Base DN : $BASE_DN"
    echo "==========================================" # Check if container is running
    if [ "$ (docker inspect -f '{{.State.Running}}' "$CONTAINER" 2>/dev/null) " != "true" ]; then echo "❌ Error: Container '$CONTAINER' is not running." exit 1
    fi # ─── Create OU if not exists ──────────────────────────────────────────────── create_ou () { local ou_name="$1" local ou_dn="OU=${ou_name},${BASE_DN}" echo "🗂 Checking OU: $ou_name" # จับ output ของ ou create แล้วเช็ค "already exists" แทนการเช็ค return code local output output=$ (docker exec "$CONTAINER" samba-tool ou create "$ou_dn" 2>&1) if echo "$output" | grep -q "already exists"; then echo " OU '$ou_name' already exists, skipping." elif echo "$output" | grep -qi "created\|added"; then echo " ✅ OU '$ou_name' created." else echo " ✅ OU '$ou_name' created." fi
    } # ─── Create User if not exists ────────────────────────────────────────────── create_user () { local username="$1" local password="$2" local ou_name="$3" # e.g. "it", "account", "hr" # --userou รับแค่ relative path เช่น "OU=it" samba-tool จะต่อ BASE_DN ให้เอง local ou_relative="OU=${ou_name}" echo "👤 Checking user: $username (OU: $ou_name) " if docker exec "$CONTAINER" samba-tool user show "$username" &>/dev/null; then echo " User '$username' already exists, skipping." else docker exec "$CONTAINER" samba-tool user create "$username" "$password" \ --userou="$ou_relative" && \ echo " ✅ User '$username' created." || \ echo " ⚠️ Failed to create user '$username'." fi
    } # ─── Create OUs ───────────────────────────────────────────────────────────── echo ""
    echo "--- Creating Organizational Units ---"
    create_ou "it"
    create_ou "account"
    create_ou "hr" # ─── Create Users ───────────────────────────────────────────────────────────
    # Format: create_user <username> <password> <ou>
    echo ""
    echo "--- Creating Users ---" # IT Department
    create_user "it.admin" "$DEFAULT_PASSWORD" "it"
    create_user "it.support1" "$DEFAULT_PASSWORD" "it"
    create_user "it.support2" "$DEFAULT_PASSWORD" "it"
    create_user "pitt.p" "$DEFAULT_PASSWORD" "it" # Accounting Department
    create_user "acc.manager" "$DEFAULT_PASSWORD" "account"
    create_user "acc.staff1" "$DEFAULT_PASSWORD" "account"
    create_user "acc.staff2" "$DEFAULT_PASSWORD" "account"
    create_user "phunsait" "$DEFAULT_PASSWORD" "account" # HR Department
    create_user "hr.manager" "$DEFAULT_PASSWORD" "hr"
    create_user "hr.staff1" "$DEFAULT_PASSWORD" "hr"
    create_user "hr.staff2" "$DEFAULT_PASSWORD" "hr"
    create_user "plusmagi" "$DEFAULT_PASSWORD" "hr" echo ""
    echo "=========================================="
    echo "✅ Done."
    echo "=========================================="
    
  3. ให้สิทธิ์
    chmod +x add_mass_users.sh

วิธีใช้งาน

  • รันด้วยค่า default
    ./add_mass_users.sh
  • กำหนด container และ domain อื่น
    ./add_mass_users.sh -c my-container -b DC=corp,DC=local -p MyP@ss123
  • กำหนดเฉพาะ password
    ./add_mass_users.sh --password SecureP@ss!

อ่านเพิ่มเติม