DomainNameValidation.sh
· 1.9 KiB · Bash
Исходник
#!/bin/bash
#Validation Function:
#- Uses a regular expression to check if the domain name format is valid.
#Domain Pointing Check:
#- Uses the dig command to resolve the domain to an IP address.
#- Checks if the resolved IP address matches the server's IP address.
#Input and Validation:
#- Gets the server's IP address using the hostname -I command.
#- Prompts the user to enter a domain name.
#- Validates the domain name format.
#- Checks if the domain points to the server's IP address.
#- Displays appropriate messages based on the validation and check results.
# Function to validate the domain name
function validate_domain() {
local domain="$1"
# Check if the domain name format is valid
if [[ "$domain" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
return 0
else
echo "Invalid domain name format."
return 1
fi
}
# Function to check if the domain points to the server's IP
function check_domain_pointing() {
local domain="$1"
local server_ip="$2"
# Get the IP address associated with the domain
domain_ip=$(dig +short "$domain" | grep -m 1 '^[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}$')
if [[ -z "$domain_ip" ]]; then
echo "Domain does not resolve to any IP address."
return 1
fi
if [[ "$domain_ip" == "$server_ip" ]]; then
echo "Domain points to the server."
return 0
else
echo "Domain does not point to the server."
return 1
fi
}
# Get server IP address
server_ip=$(hostname -I | awk '{print $1}')
# Get domain name input from the user
read -p "Enter domain name: " domain
# Validate the domain name
if validate_domain "$domain"; then
# Check if the domain points to the server
if check_domain_pointing "$domain" "$server_ip"; then
echo "Domain is valid and points to the server."
else
echo "Domain is valid but does not point to the server."
fi
else
echo "Invalid domain name. Please try again."
fi
| 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 |
| 69 |