Skip to content

Instantly share code, notes, and snippets.

@hoducha
Created July 29, 2016 03:59
Show Gist options
  • Save hoducha/9140243d4719944df44acab8a31e5b4a to your computer and use it in GitHub Desktop.
Save hoducha/9140243d4719944df44acab8a31e5b4a to your computer and use it in GitHub Desktop.
Domain checker shell script
#!/bin/bash
### REQUIREMENTS
# sudo apt-get install whois
# sudo apt-get install curl
### CONSTANTS
# By default the script uses a 500ms delay.
sleep=0.5
notFound='No match'
# Twilio
toNumber=
fromNumber=
accountSid=
authToken=
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
### CONFIGURATION
trap ctrl_c INT
function ctrl_c() {
echo -e "${GREEN}Exiting...${NC}"
exit
}
### FUNCTIONS
function usage {
echo "usage: domain-checker [[-d single_domain] | [-f input_file output_file] | [-h]]"
}
function check_domain {
local domain=$1
whois_result=`whois $domain`
if [[ $whois_result == *$notFound* ]]; then
echo "available";
else
echo "taken"
fi
}
function send_sms {
local message="Free domains: $1"
curl -s -X POST "https://api.twilio.com/2010-04-01/Accounts/$accountSid/Messages.json" \
--data-urlencode "To=$toNumber" \
--data-urlencode "From=$fromNumber" \
--data-urlencode "Body=$1" \
-u $accountSid:$authToken \
> /dev/null
}
### MAIN PROCESS
# Declare variables
interactive=
single_domain=
input_file=
output_file=
monitoring_list=
available_list=
# Get arguments
while [ "$1" != "" ]; do
case $1 in
-d | --domain ) shift
single_domain=$1
;;
-f | --file ) shift
input_file=$1
shift
output_file=$1
;;
-i | --interactive ) interactive=1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
# Implement interactive
if [ "$interactive" = "1" ]; then
response=
while [[ -z "$input_file" && ! -f $input_file ]]; do
printf "${GREEN}Enter name of input file [$input_file] > ${NC}"
read response
if [ -n "$response" ]; then
if [ -f $response ]; then
input_file=$response
else
printf "${RED}File does not exists. ${GREEN}Enter another file? (y/n) > ${NC}"
read response
if [ "$response" != "y" ]; then
echo "${GREEN}Exiting program.${NC}"
exit 1
fi
fi
fi
done
fi
# Check for a single domain
if [ ! -z "$single_domain" ]; then
result=`check_domain $single_domain`
if [ $result == "available" ]; then
available_list="$single_domain,$available_list"
echo "${GREEN}$single_domain ******FREE******${NC}"
else
echo "${RED}$single_domain TAKEN :-(${NC}"
fi
fi
# Check for a list of domains
if [ ! -z "$input_file" ]; then
if [ ! -f $input_file ]; then
echo "${RED}Input file does not exists.${NC}"
exit 1
fi
if [ -z "$output_file" ]; then
output_file="available_list.txt"
fi
domain_list=`cat $input_file`
for domain in $domain_list; do
sleep $sleep
result=`check_domain $domain`
if [ $result == "available" ]; then
available_list="$domain,$available_list"
echo "${GREEN}$domain ******FREE******${NC}"
else
monitoring_list="$domain\n$monitoring_list"
echo "${RED}$domain TAKEN :-(${NC}"
fi
done
echo $available_list | tr "," "\n" >> $output_file
echo $monitoring_list > $input_file
fi
# Send SMS
# if [ ! -z "$available_list" ]; then
# send_sms $available_list
# fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment