64 lines
2.1 KiB
PowerShell
64 lines
2.1 KiB
PowerShell
# verifyIPLinux.ps1
|
|
# Deploy to: C:\CLISave\AssignIP\verifyIPLinux.ps1 on the vSphere Windows host
|
|
#
|
|
# Usage: powershell -ExecutionPolicy Bypass -NonInteractive -File verifyIPLinux.ps1 \
|
|
# <vm_id> <ip> <vcenter_host> <vcenter_user> <vcenter_password> <guest_password>
|
|
#
|
|
# Checks that the IP is configured and the interface is UP inside the Linux guest.
|
|
# Output: SUCCESS | FAIL (exactly one line)
|
|
|
|
param(
|
|
[Parameter(Mandatory=$true)][string]$VmId,
|
|
[Parameter(Mandatory=$true)][string]$Ip,
|
|
[Parameter(Mandatory=$true)][string]$VCenterHost,
|
|
[Parameter(Mandatory=$true)][string]$VCenterUser,
|
|
[Parameter(Mandatory=$true)][string]$VCenterPassword,
|
|
[Parameter(Mandatory=$true)][string]$GuestPassword
|
|
)
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
try {
|
|
Import-Module VMware.VimAutomation.Core -ErrorAction Stop
|
|
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
|
|
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -Confirm:$false | Out-Null
|
|
|
|
Connect-VIServer -Server $VCenterHost -User $VCenterUser -Password $VCenterPassword -Force -ErrorAction Stop | Out-Null
|
|
|
|
$vm = Get-VM -Id "VirtualMachine-$VmId" -ErrorAction Stop
|
|
|
|
# Bash snippet: verify IP exists on an active (UP) interface
|
|
$verifyScript = @"
|
|
#!/bin/bash
|
|
# Check the IP appears in 'ip a' output on an interface that is UP (state UP)
|
|
if ip a | grep -q '$Ip'; then
|
|
IFACE=`$(ip -o addr show | awk '/$Ip/{print `$2}' | head -1)
|
|
STATE=`$(cat /sys/class/net/`$IFACE/operstate 2>/dev/null || echo 'unknown')
|
|
if [ "`$STATE" = "up" ] || [ "`$STATE" = "unknown" ]; then
|
|
echo SUCCESS
|
|
exit 0
|
|
fi
|
|
fi
|
|
echo FAIL
|
|
exit 1
|
|
"@
|
|
|
|
$result = Invoke-VMScript -VM $vm `
|
|
-ScriptText $verifyScript `
|
|
-GuestUser "root" `
|
|
-GuestPassword $GuestPassword `
|
|
-ScriptType Bash `
|
|
-ErrorAction Stop
|
|
|
|
if ($result.ScriptOutput -match "SUCCESS") {
|
|
Write-Output "SUCCESS"
|
|
} else {
|
|
Write-Output "FAIL"
|
|
}
|
|
} catch {
|
|
Write-Output "FAIL"
|
|
} finally {
|
|
Disconnect-VIServer -Confirm:$false -ErrorAction SilentlyContinue | Out-Null
|
|
}
|