dartokloning 修订了这个 Gist . 跳至此修订
1 file changed, 68 insertions
DomainNameValidation.sh(file created)
| @@ -0,0 +1,68 @@ | |||
| 1 | + | #!/bin/bash | |
| 2 | + | ||
| 3 | + | #Validation Function: | |
| 4 | + | #- Uses a regular expression to check if the domain name format is valid. | |
| 5 | + | ||
| 6 | + | #Domain Pointing Check: | |
| 7 | + | #- Uses the dig command to resolve the domain to an IP address. | |
| 8 | + | #- Checks if the resolved IP address matches the server's IP address. | |
| 9 | + | ||
| 10 | + | #Input and Validation: | |
| 11 | + | #- Gets the server's IP address using the hostname -I command. | |
| 12 | + | #- Prompts the user to enter a domain name. | |
| 13 | + | #- Validates the domain name format. | |
| 14 | + | #- Checks if the domain points to the server's IP address. | |
| 15 | + | #- Displays appropriate messages based on the validation and check results. | |
| 16 | + | ||
| 17 | + | # Function to validate the domain name | |
| 18 | + | function validate_domain() { | |
| 19 | + | local domain="$1" | |
| 20 | + | ||
| 21 | + | # Check if the domain name format is valid | |
| 22 | + | if [[ "$domain" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then | |
| 23 | + | return 0 | |
| 24 | + | else | |
| 25 | + | echo "Invalid domain name format." | |
| 26 | + | return 1 | |
| 27 | + | fi | |
| 28 | + | } | |
| 29 | + | ||
| 30 | + | # Function to check if the domain points to the server's IP | |
| 31 | + | function check_domain_pointing() { | |
| 32 | + | local domain="$1" | |
| 33 | + | local server_ip="$2" | |
| 34 | + | ||
| 35 | + | # Get the IP address associated with the domain | |
| 36 | + | domain_ip=$(dig +short "$domain" | grep -m 1 '^[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}$') | |
| 37 | + | ||
| 38 | + | if [[ -z "$domain_ip" ]]; then | |
| 39 | + | echo "Domain does not resolve to any IP address." | |
| 40 | + | return 1 | |
| 41 | + | fi | |
| 42 | + | ||
| 43 | + | if [[ "$domain_ip" == "$server_ip" ]]; then | |
| 44 | + | echo "Domain points to the server." | |
| 45 | + | return 0 | |
| 46 | + | else | |
| 47 | + | echo "Domain does not point to the server." | |
| 48 | + | return 1 | |
| 49 | + | fi | |
| 50 | + | } | |
| 51 | + | ||
| 52 | + | # Get server IP address | |
| 53 | + | server_ip=$(hostname -I | awk '{print $1}') | |
| 54 | + | ||
| 55 | + | # Get domain name input from the user | |
| 56 | + | read -p "Enter domain name: " domain | |
| 57 | + | ||
| 58 | + | # Validate the domain name | |
| 59 | + | if validate_domain "$domain"; then | |
| 60 | + | # Check if the domain points to the server | |
| 61 | + | if check_domain_pointing "$domain" "$server_ip"; then | |
| 62 | + | echo "Domain is valid and points to the server." | |
| 63 | + | else | |
| 64 | + | echo "Domain is valid but does not point to the server." | |
| 65 | + | fi | |
| 66 | + | else | |
| 67 | + | echo "Invalid domain name. Please try again." | |
| 68 | + | fi | |
更新
更早