Created
May 31, 2019 08:50
-
-
Save wuftymerguftyguff/a5f12fd69b68f29ae9be71312694f33d to your computer and use it in GitHub Desktop.
Hosts File Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
function valid_ip(ip) { | |
octets=4 | |
search="." | |
if ( occurances_of_seperator(ip,search) != 3 ) { | |
return false | |
} | |
for (i=1;i<=octets;i++) { | |
if ( ! valid_int_in_range(array[i],0,255) ) { | |
return false | |
} | |
} | |
return true | |
} | |
function valid_fqdn(fqdn){ | |
search="." | |
return ( occurances_of_seperator(fqdn,search) >= 1) | |
} | |
function occurances_of_seperator(string,seperator) { | |
return split(string,array,seperator) - 1 | |
} | |
function valid_int_in_range(val,range_min,range_max) { | |
return ( range_min <= val && val <= range_max) | |
} | |
BEGIN { | |
print "Starting" | |
true=1 | |
false=0 | |
} | |
!/^($|[:space:]*#)/ { | |
ip=$1 | |
fqdn=$2 | |
$1="" | |
$2="" | |
aliases=$0 | |
if ( ! valid_ip(ip) ) { | |
printf("Invalid IP Address on Line number: %s\n",NR) | |
} | |
if ( ! valid_fqdn(fqdn) ) { | |
printf("Invalid FQDN on Line number: %s\n",NR) | |
} | |
} | |
END { | |
print "Ending" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment