Skip to content

Instantly share code, notes, and snippets.

@wuftymerguftyguff
Created May 31, 2019 08:50
Show Gist options
  • Save wuftymerguftyguff/a5f12fd69b68f29ae9be71312694f33d to your computer and use it in GitHub Desktop.
Save wuftymerguftyguff/a5f12fd69b68f29ae9be71312694f33d to your computer and use it in GitHub Desktop.
Hosts File Validation
#!/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