-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathwin_rdp_port.sh
executable file
·79 lines (67 loc) · 2.87 KB
/
win_rdp_port.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
cd `dirname $0`
source ../../cloudrc
[ $# -lt 2 ] && echo "$0 <vm_ID> <rdp_port>" && exit -1
vm_ID=$1
rdp_port=$2
TIMEOUT=600
WAIT_TIME=5
ELAPSED_TIME=0
# wait for the VM to boot
log_debug $vm_ID "Waiting for Windows VM '$vm_ID' to boot..."
while true; do
# check if the VM is running
VM_STATE=$(virsh domstate "$vm_ID" 2>&1)
if [ "$VM_STATE" == "running" ]; then
log_debug $vm_ID "Windows VM '$vm_ID' is running."
break
fi
# check if the timeout has been reached
if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
log_debug $vm_ID "Timeout waiting for Windows VM '$vm_ID' to start after $TIMEOUT seconds."
die "Timeout waiting for Windows VM '$vm_ID' to start after $TIMEOUT seconds."
fi
sleep $WAIT_TIME
ELAPSED_TIME=$((ELAPSED_TIME + WAIT_TIME))
done
# wait for the windows guest agent to start
log_debug $vm_ID "Waiting for Windows VM '$vm_ID' to start the guest agent..."
while true; do
# check if the guest agent is running
virsh qemu-agent-command "$vm_ID" '{"execute":"guest-ping"}'
if [ $? -eq 0 ]; then
log_debug $vm_ID "Windows VM '$vm_ID' has started the guest agent."
break
fi
# check if the timeout has been reached
if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
log_debug $vm_ID "Timeout waiting for Windows VM '$vm_ID' to start the guest agent after $TIMEOUT seconds."
die "Timeout waiting for Windows VM '$vm_ID' to start the guest agent after $TIMEOUT seconds."
fi
sleep $WAIT_TIME
ELAPSED_TIME=$((ELAPSED_TIME + WAIT_TIME))
done
PS_SCRIPT='Set-ItemProperty -Path \"HKLM:\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp\" -Name PortNumber -Value '${rdp_port}'; Restart-Service -Name \"TermService\" -Force; New-NetFirewallRule -DisplayName \"RDP-TCP-'${rdp_port}'\" -Action Allow -Protocol TCP -LocalPort '${rdp_port}
ELAPSED_TIME=0
PS_SUCEED_TIMES=0
log_debug $vm_ID "Executing PowerShell script to change RDP port..."
while true; do
OUTPUT=$(virsh qemu-agent-command "$vm_ID" '{"execute":"guest-exec","arguments":{"path":"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe","arg":["-Command","'"$PS_SCRIPT"'"],"capture-output":true}}')
if [ -n "${OUTPUT}" ]; then
log_debug $vm_ID "$vm_ID exec powershell: $OUTPUT"
QA_RS=$(jq -r '.return' <<< $OUTPUT)
if [ -n "${QA_RS}" ]; then
PS_SUCEED_TIMES=$((PS_SUCEED_TIMES + 1))
if [ ${PS_SUCEED_TIMES} -gt 2 ]; then
log_debug $vm_ID "$vm_ID exec powershell succeed"
break
fi
fi
fi
if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
log_debug $vm_ID "$vm_ID timeout while waiting guest agent ready"
die "Timeout waiting for Windows VM '$vm_ID' to execute PowerShell script after $TIMEOUT seconds."
fi
sleep $WAIT_TIME
ELAPSED_TIME=$((ELAPSED_TIME + WAIT_TIME))
done