khalequzzaman17

Let's Encrypt Force IPv4

Mar 19th, 2022 (edited)
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.75 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. set -euf -o pipefail
  3.  
  4. HOSTSFILE="/etc/hosts"
  5.  
  6. # Use the `host` command to extract the IPv4 address
  7. IP4=$(host -c IN -t A acme-v02.api.letsencrypt.org | awk '/has address/ { print $4 ; exit }')
  8.  
  9. # Sanity check on what we found
  10. if [[ ! $IP4 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  11.   echo "$IP4 is not IP"
  12.   exit 1
  13. fi
  14.  
  15. # If there is already a line for this domain, we must replace it
  16. if grep -qE "^[0-9].*acme-v02.api.letsencrypt.org" "$HOSTSFILE"; then
  17.   to_replace=$(grep -Em 1 "^[0-9].*acme-v02.api.letsencrypt.org$" "$HOSTSFILE")
  18.   sed -i "s/$to_replace/$IP4 acme-v02.api.letsencrypt.org/" "$HOSTSFILE"
  19. else
  20.   # Otherwise we are adding a new line
  21.   printf "%s acme-v02.api.letsencrypt.org\n" "$IP4" >> "$HOSTSFILE"
  22. fi
Add Comment
Please, Sign In to add comment