{"id":12141,"date":"2025-01-12T11:07:59","date_gmt":"2025-01-12T19:07:59","guid":{"rendered":"https:\/\/www.xh86.me\/?p=12141"},"modified":"2025-01-12T11:07:59","modified_gmt":"2025-01-12T19:07:59","slug":"vsphere-%e4%b8%ad%e8%87%aa%e5%8a%a8%e5%ae%89%e8%a3%85%e7%b3%bb%e7%bb%9f%ef%bc%88%e6%b5%8b%e8%af%95%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.xh86.me\/?p=12141","title":{"rendered":"vSphere \u4e2d\u81ea\u52a8\u5b89\u88c5\u7cfb\u7edf\uff08\u6d4b\u8bd5\uff09"},"content":{"rendered":"<p>#!\/bin\/bash<\/p>\n<p># vSphere Connection Details<br \/>\nVSPHERE_SERVER=&#8221;your_vsphere_server&#8221;<br \/>\nVSPHERE_USER=&#8221;your_vsphere_username&#8221;<br \/>\nVSPHERE_PASSWORD=&#8221;your_vsphere_password&#8221;<\/p>\n<p># VM Configuration<br \/>\nDATASTORE=&#8221;your_datastore&#8221;<br \/>\nNETWORK=&#8221;your_network&#8221;<br \/>\nRESOURCE_POOL=&#8221;your_resource_pool&#8221;<br \/>\nISO_DOWNLOAD_DIR=&#8221;\/mnt\/data\/iso_downloads&#8221;<\/p>\n<p># OS ISO Download URLs<br \/>\nISO_URLS=(<br \/>\n&#8220;Ubuntu_Server:https:\/\/releases.ubuntu.com\/22.04\/ubuntu-22.04-live-server-amd64.iso&#8221;<br \/>\n&#8220;Debian_Server:https:\/\/cdimage.debian.org\/debian-cd\/current\/amd64\/iso-cd\/debian-12.1.0-amd64-netinst.iso&#8221;<br \/>\n&#8220;CentOS_Stream:https:\/\/mirror.stream.centos.org\/9-stream\/isos\/x86_64\/CentOS-Stream-9-latest-x86_64-dvd1.iso&#8221;<br \/>\n&#8220;Rocky_Linux:https:\/\/download.rockylinux.org\/pub\/rocky\/9\/isos\/x86_64\/Rocky-9.2-x86_64-minimal.iso&#8221;<br \/>\n&#8220;AlmaLinux:https:\/\/repo.almalinux.org\/almalinux\/9.2\/isos\/x86_64\/AlmaLinux-9.2-x86_64-minimal.iso&#8221;<br \/>\n&#8220;RHEL:https:\/\/access.redhat.com\/downloads\/content\/iso&#8221;<br \/>\n&#8220;SLES:https:\/\/download.suse.com\/Download?buildid=sle-15-sp4&#8221;<br \/>\n&#8220;Fedora_Server:https:\/\/download.fedoraproject.org\/pub\/fedora\/linux\/releases\/38\/Server\/x86_64\/iso\/Fedora-Server-dvd-x86_64-38-1.6.iso&#8221;<br \/>\n&#8220;Oracle_Linux:https:\/\/yum.oracle.com\/ISOS\/OracleLinux\/OL9\/u2\/x86_64\/OracleLinux-R9-U2-x86_64-dvd.iso&#8221;<br \/>\n&#8220;Arch_Linux:https:\/\/mirror.rackspace.com\/archlinux\/iso\/latest\/archlinux-x86_64.iso&#8221;<br \/>\n&#8220;Windows_Server_2022:https:\/\/software-download.microsoft.com\/sg\/Win2022.iso&#8221;<br \/>\n&#8220;Windows_Server_2019:https:\/\/software-download.microsoft.com\/sg\/Win2019.iso&#8221;<br \/>\n&#8220;Windows_Server_2025:https:\/\/software-download.microsoft.com\/sg\/Win2025.iso&#8221;<br \/>\n)<\/p>\n<p># VM Parameters<br \/>\nVM_CPU=2<br \/>\nVM_RAM=4096<br \/>\nVM_DISK=40<br \/>\nPASSWORD=&#8221;Digital!!29&#8243;<\/p>\n<p># Function to download ISO<br \/>\ndownload_iso() {<br \/>\nOS_NAME=$1<br \/>\nISO_URL=$2<br \/>\nISO_PATH=&#8221;$ISO_DOWNLOAD_DIR\/${OS_NAME}.iso&#8221;<\/p>\n<p>if [ ! -f &#8220;$ISO_PATH&#8221; ]; then<br \/>\necho &#8220;\u6b63\u5728\u4e0b\u8f7d $OS_NAME&#8230;&#8221;<br \/>\nmkdir -p &#8220;$ISO_DOWNLOAD_DIR&#8221;<br \/>\nwget -O &#8220;$ISO_PATH&#8221; &#8220;$ISO_URL&#8221;<br \/>\ngovc datastore.upload &#8220;$ISO_PATH&#8221; &#8220;$ISO_PATH&#8221;<br \/>\nelse<br \/>\necho &#8220;$OS_NAME \u5df2\u7ecf\u4e0b\u8f7d.&#8221;<br \/>\nfi<br \/>\n}<\/p>\n<p># Function to create a VM<br \/>\ncreate_vm() {<br \/>\nVM_NAME=$1<br \/>\nGUEST_OS=$2<br \/>\nISO_PATH=$3<\/p>\n<p>govc vm.create -dc=&#8221;Datacenter&#8221; -ds=&#8221;$DATASTORE&#8221; -net=&#8221;$NETWORK&#8221; -pool=&#8221;$RESOURCE_POOL&#8221; \\<br \/>\n-m=$VM_RAM -c=$VM_CPU -disk=$VM_DISK -g=&#8221;$GUEST_OS&#8221; &#8220;$VM_NAME&#8221;<\/p>\n<p>govc device.cdrom.insert -vm &#8220;$VM_NAME&#8221; &#8220;$ISO_PATH&#8221;<br \/>\ngovc vm.power -on &#8220;$VM_NAME&#8221;<br \/>\n}<\/p>\n<p># Function to automate Linux post-install configuration<br \/>\nconfigure_linux_vm() {<br \/>\nVM_IP=$1<br \/>\nsshpass -p &#8220;$PASSWORD&#8221; ssh -o StrictHostKeyChecking=no root@$VM_IP &lt;&lt; EOF<br \/>\npasswd root &lt;&lt;EOP<br \/>\n$PASSWORD<br \/>\n$PASSWORD<br \/>\nEOP<br \/>\napt-get update &amp;&amp; apt-get install -y openssh-server<br \/>\nsed -i &#8216;s\/^#PermitRootLogin.*\/PermitRootLogin yes\/&#8217; \/etc\/ssh\/sshd_config<br \/>\nsystemctl enable ssh<br \/>\nsystemctl restart ssh<br \/>\nEOF<br \/>\n}<\/p>\n<p># Function to automate Windows post-install configuration<br \/>\nconfigure_windows_vm() {<br \/>\nVM_IP=$1<br \/>\nwinrm set winrm\/config\/service\/Auth &#8216;@{Basic=&#8221;true&#8221;}&#8217;<br \/>\nwinrm set winrm\/config\/service &#8216;@{AllowUnencrypted=&#8221;true&#8221;}&#8217;<br \/>\nwinrm quickconfig -q<br \/>\npowershell -Command &#8220;net user Administrator $PASSWORD&#8221;<br \/>\n}<\/p>\n<p># Display OS options<br \/>\necho &#8220;\u8bf7\u9009\u62e9\u8981\u5b89\u88c5\u7684\u64cd\u4f5c\u7cfb\u7edf\uff1a&#8221;<br \/>\nfor i in &#8220;${!ISO_URLS[@]}&#8221;; do<br \/>\nOS_NAME=$(echo &#8220;${ISO_URLS[$i]}&#8221; | cut -d: -f1)<br \/>\necho &#8220;$((i+1)). $OS_NAME&#8221;<br \/>\ndone<\/p>\n<p>read -p &#8220;\u8f93\u5165\u64cd\u4f5c\u7cfb\u7edf\u7f16\u53f7\uff08\u591a\u4e2a\u7f16\u53f7\u7528\u7a7a\u683c\u5206\u5f00\uff09: &#8221; -a CHOICES<\/p>\n<p>for choice in &#8220;${CHOICES[@]}&#8221;; do<br \/>\nINDEX=$((choice-1))<br \/>\nOS_NAME=$(echo &#8220;${ISO_URLS[$INDEX]}&#8221; | cut -d: -f1)<br \/>\nISO_URL=$(echo &#8220;${ISO_URLS[$INDEX]}&#8221; | cut -d: -f2)<br \/>\nISO_PATH=&#8221;[datastore1] iso\/${OS_NAME}.iso&#8221;<\/p>\n<p>download_iso &#8220;$OS_NAME&#8221; &#8220;$ISO_URL&#8221;<\/p>\n<p>if [[ $OS_NAME == *&#8221;Windows&#8221;* ]]; then<br \/>\nGUEST_OS=&#8221;windows9Server64Guest&#8221;<br \/>\nelse<br \/>\nGUEST_OS=&#8221;ubuntu64Guest&#8221;<br \/>\nfi<\/p>\n<p>VM_NAME=&#8221;${OS_NAME}_VM&#8221;<br \/>\ncreate_vm &#8220;$VM_NAME&#8221; &#8220;$GUEST_OS&#8221; &#8220;$ISO_PATH&#8221;<\/p>\n<p>echo &#8220;$OS_NAME \u7684 $VM_NAME \u6b63\u5728\u81ea\u52a8\u914d\u7f6e&#8230;&#8221;<br \/>\nsleep 60<br \/>\nVM_IP=$(govc vm.ip -wait=true &#8220;$VM_NAME&#8221;)<br \/>\nif [[ $OS_NAME == *&#8221;Windows&#8221;* ]]; then<br \/>\nconfigure_windows_vm &#8220;$VM_IP&#8221;<br \/>\nelse<br \/>\nconfigure_linux_vm &#8220;$VM_IP&#8221;<br \/>\nfi<\/p>\n<p>done<\/p>\n<p>echo &#8220;\u6240\u9009\u7684\u7cfb\u7edf\u5df2\u81ea\u52a8\u4e0b\u8f7d\u3001\u5b89\u88c5\u3001\u914d\u7f6e\u5b8c\u6210\u5e76\u542f\u52a8\u3002&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#!\/bin\/bash # vSphere Connection Details VSPHERE_SERVER [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-12141","post","type-post","status-publish","format-standard","hentry","category-vsphere"],"_links":{"self":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts\/12141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12141"}],"version-history":[{"count":1,"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts\/12141\/revisions"}],"predecessor-version":[{"id":12142,"href":"https:\/\/www.xh86.me\/index.php?rest_route=\/wp\/v2\/posts\/12141\/revisions\/12142"}],"wp:attachment":[{"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xh86.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}