-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathdns_add_windows_dns_server
More file actions
executable file
·39 lines (29 loc) · 1.2 KB
/
dns_add_windows_dns_server
File metadata and controls
executable file
·39 lines (29 loc) · 1.2 KB
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
#!/usr/bin/env bash
# Windows DNS server using powershell - dnscmd is going to be deprecated
# Using Windows Sublinux for executing windows commands
# dnscmd command will be depricated use powershell instead
regexp='[A-z0-9]+(\.(co|com))?\.\w+$'
fulldomain=${1}
# Get root domain api.[domain|.co|.uk]
rootdomain=$(echo "${fulldomain}" | grep -Eo "${regexp}")
# Exlude root domain [api].domain.com
subdomain=$(result=$(echo "${fulldomain}" | grep -Po '(.*)(?=\.[A-z0-9]+(\.(co|com))?\.\w+$)') && if [[ ${#result} -gt 0 ]]; then echo ".${result}"; else echo ""; fi)
token=${2}
nloop=1
retries=15 # Sometimes it fails
while [[ ${nloop} -le ${retries} ]]; do
# Add TXT record
echo "Tries ${nloop} out of ${retries}"
echo "Adding acme challenge record for ${fulldomain} with token ${token}"
cmd=(powershell.exe Add-DnsServerResourceRecord -DescriptiveText \'"${token}"\' -Name \'"_acme-challenge${subdomain}"\' -Txt -ZoneName \'"${rootdomain}"\' -TimeToLive 0:0:0:1)
echo "${cmd[@]}"
result_stderr=$({ "${cmd[@]}" ;} 2>&1)
if [[ ${#result_stderr} -eq 0 ]]; then
break
else
echo "${result_stderr}"
fi
nloop=$((nloop+1))
echo "Sleeping 5 seconds"
sleep 5
done