Skip to content

Instantly share code, notes, and snippets.

@tonious
Created July 24, 2012 17:21
Show Gist options
  • Save tonious/3171327 to your computer and use it in GitHub Desktop.
Save tonious/3171327 to your computer and use it in GitHub Desktop.
A domain name expiry checker.
#!/bin/bash
whoisfile="/tmp/namecheck.whois.$$.txt"
listfile="/tmp/namecheck.list.$$.txt"
trap "cleanup; exit 1" SIGINT SIGTERM
# Clean up temporary files.
function cleanup {
rm -f $whoisfile $listfile
}
function buildlist {
echo -n "" > $listfile
for name in `cat $1`
do
whois $name > $whoisfile
expiry=`egrep '(Expiration Date|Expires on|Expiry date):\s?(.+)' \
$whoisfile | \
sed -r -e 's/(Expiration Date|Expires on|Expiry date):\s?//' | \
head -1`
expiry=`date --date "$expiry" +%D`
expiry_epoch=`date --date "$expiry" +%s`
now_epoch=`date --date "now" +%s`
daysleft=$(( (expiry_epoch - now_epoch)/86400 ))
if grep -i mrx $whoisfile >> /dev/null; then
responsible="MRX"
else
responsible="Client"
fi
echo "$name, $expiry, $daysleft, $responsible" >> $listfile
done
}
function sortlist {
echo "Domain Name, Expires On, Who Renews?"
sort -n -t ',' -k 3 $listfile | cut -d ',' -f 1,2,4
}
function report {
echo "Domains expiring in less than seven days."
echo
awk '{if(int($3) <= 7 ) print $0}' $listfile | \
sort -n -t ',' -k 3 | cut -d ',' -f 1,2,4
echo
echo "Domains expiring in less than thirty days."
echo
awk '{if(int($3) <= 30 ) print $0}' $listfile | \
sort -n -t ',' -k 3 | cut -d ',' -f 1,2,4
echo
}
function show_help {
cat <<EOF
Usage: $0 -f domainlist.txt -r|-l|-h
-f A list of domain names, one name per line.
-r Report expiring domains over the next 7 and 30 day periods.
-l List all domains in the order they shall expire.
-h Show this help message.
EOF
}
action_report=0
action_list=0
action_help=0
# Parse options.
while getopts "f:rlh" opt; do
case $opt in
f)
inputlist=$OPTARG
;;
r)
action_report=1
;;
l)
action_list=1
;;
h)
action_help=1
;;
esac
done
# Do some error checking.
if [[ $action_help -eq '1' ]]; then
show_help
exit 0
elif [[ $(( $action_list + $action_report )) -ne '1' ]]; then
echo "Must specify exactly one of -r or -l." >&2
exit 1
elif [[ ! -r $inputlist ]]; then
echo "Cannot read input file." >&2
exit 1
elif [[ ! -v inputlist ]]; then
echo "Must specify an input file." >&2
exit 1
fi
# Now we're getting somewhere.
buildlist $inputlist
if [[ $action_list -eq '1' ]]; then
sortlist
elif [[ $action_report -eq '1' ]]; then
report
fi
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment