#!/bin/bash
# vSphere Connection Details
VSPHERE_SERVER=”your_vsphere_server”
VSPHERE_USER=”your_vsphere_username”
VSPHERE_PASSWORD=”your_vsphere_password”
# VM Configuration
DATASTORE=”your_datastore”
NETWORK=”your_network”
RESOURCE_POOL=”your_resource_pool”
ISO_DOWNLOAD_DIR=”/mnt/data/iso_downloads”
# OS ISO Download URLs
ISO_URLS=(
“Ubuntu_Server:https://releases.ubuntu.com/22.04/ubuntu-22.04-live-server-amd64.iso”
“Debian_Server:https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-12.1.0-amd64-netinst.iso”
“CentOS_Stream:https://mirror.stream.centos.org/9-stream/isos/x86_64/CentOS-Stream-9-latest-x86_64-dvd1.iso”
“Rocky_Linux:https://download.rockylinux.org/pub/rocky/9/isos/x86_64/Rocky-9.2-x86_64-minimal.iso”
“AlmaLinux:https://repo.almalinux.org/almalinux/9.2/isos/x86_64/AlmaLinux-9.2-x86_64-minimal.iso”
“RHEL:https://access.redhat.com/downloads/content/iso”
“SLES:https://download.suse.com/Download?buildid=sle-15-sp4”
“Fedora_Server:https://download.fedoraproject.org/pub/fedora/linux/releases/38/Server/x86_64/iso/Fedora-Server-dvd-x86_64-38-1.6.iso”
“Oracle_Linux:https://yum.oracle.com/ISOS/OracleLinux/OL9/u2/x86_64/OracleLinux-R9-U2-x86_64-dvd.iso”
“Arch_Linux:https://mirror.rackspace.com/archlinux/iso/latest/archlinux-x86_64.iso”
“Windows_Server_2022:https://software-download.microsoft.com/sg/Win2022.iso”
“Windows_Server_2019:https://software-download.microsoft.com/sg/Win2019.iso”
“Windows_Server_2025:https://software-download.microsoft.com/sg/Win2025.iso”
)
# VM Parameters
VM_CPU=2
VM_RAM=4096
VM_DISK=40
PASSWORD=”Digital!!29″
# Function to download ISO
download_iso() {
OS_NAME=$1
ISO_URL=$2
ISO_PATH=”$ISO_DOWNLOAD_DIR/${OS_NAME}.iso”
if [ ! -f “$ISO_PATH” ]; then
echo “正在下载 $OS_NAME…”
mkdir -p “$ISO_DOWNLOAD_DIR”
wget -O “$ISO_PATH” “$ISO_URL”
govc datastore.upload “$ISO_PATH” “$ISO_PATH”
else
echo “$OS_NAME 已经下载.”
fi
}
# Function to create a VM
create_vm() {
VM_NAME=$1
GUEST_OS=$2
ISO_PATH=$3
govc vm.create -dc=”Datacenter” -ds=”$DATASTORE” -net=”$NETWORK” -pool=”$RESOURCE_POOL” \
-m=$VM_RAM -c=$VM_CPU -disk=$VM_DISK -g=”$GUEST_OS” “$VM_NAME”
govc device.cdrom.insert -vm “$VM_NAME” “$ISO_PATH”
govc vm.power -on “$VM_NAME”
}
# Function to automate Linux post-install configuration
configure_linux_vm() {
VM_IP=$1
sshpass -p “$PASSWORD” ssh -o StrictHostKeyChecking=no root@$VM_IP << EOF
passwd root <<EOP
$PASSWORD
$PASSWORD
EOP
apt-get update && apt-get install -y openssh-server
sed -i ‘s/^#PermitRootLogin.*/PermitRootLogin yes/’ /etc/ssh/sshd_config
systemctl enable ssh
systemctl restart ssh
EOF
}
# Function to automate Windows post-install configuration
configure_windows_vm() {
VM_IP=$1
winrm set winrm/config/service/Auth ‘@{Basic=”true”}’
winrm set winrm/config/service ‘@{AllowUnencrypted=”true”}’
winrm quickconfig -q
powershell -Command “net user Administrator $PASSWORD”
}
# Display OS options
echo “请选择要安装的操作系统:”
for i in “${!ISO_URLS[@]}”; do
OS_NAME=$(echo “${ISO_URLS[$i]}” | cut -d: -f1)
echo “$((i+1)). $OS_NAME”
done
read -p “输入操作系统编号(多个编号用空格分开): ” -a CHOICES
for choice in “${CHOICES[@]}”; do
INDEX=$((choice-1))
OS_NAME=$(echo “${ISO_URLS[$INDEX]}” | cut -d: -f1)
ISO_URL=$(echo “${ISO_URLS[$INDEX]}” | cut -d: -f2)
ISO_PATH=”[datastore1] iso/${OS_NAME}.iso”
download_iso “$OS_NAME” “$ISO_URL”
if [[ $OS_NAME == *”Windows”* ]]; then
GUEST_OS=”windows9Server64Guest”
else
GUEST_OS=”ubuntu64Guest”
fi
VM_NAME=”${OS_NAME}_VM”
create_vm “$VM_NAME” “$GUEST_OS” “$ISO_PATH”
echo “$OS_NAME 的 $VM_NAME 正在自动配置…”
sleep 60
VM_IP=$(govc vm.ip -wait=true “$VM_NAME”)
if [[ $OS_NAME == *”Windows”* ]]; then
configure_windows_vm “$VM_IP”
else
configure_linux_vm “$VM_IP”
fi
done
echo “所选的系统已自动下载、安装、配置完成并启动。”