#!/bin/bash # Christopher Jones # 08/09/2013 # iplistquery.sh # script to query list of ip's and return country information. # dependences ./countrycodes.txt #Intialize variables COUNTRYCODES="./countrycodes.txt" #First a simple Bashtrap function to handle interupt (CTRL+C) trap bashtrap INT bashtrap(){ echo echo echo 'CTRL+C has been detected!.....shutting down now' | grep --color '.....shutting down now' #exit entire script if called exit; } #End bashtrap() usage_info(){ echo echo "iplistquery.sh - Filters a list of ips based on country" echo echo "USAGE: iplistquery.sh [-c countrycode] [-i input file] [-o output file]" echo -e "\tCommand Summary:" echo -e "\t -b|--build\t\t\t\tBuild iplist using getproxies.sh in override mode" echo -e "\t -c|--country\t\t[country]\tCountry code to filter*" echo -e "\t -h|--help\t\t\t\tHelp (View Usage)" echo -e "\t -i|--iplist\t\t[inputfile]\tComplete path to file to use as input file*" echo -e "\t -o|--outputfile\t[outputfile]\tComplete path to file to output filtered list*" echo echo "* Optional Argument, will revert to defaults if not supplied" exit 0 } # END usage_info() #Main---------------------------------------------------------------- IPLIST="" OUTPUTFILE="" COUNTRY="" BUILDFLAG=0 # options may be followed by one colon to indicate they have a required argument if ! options=$(getopt -o bhc:i:o: -l build,help,country,iplist,outputfile: -- "$@") then # something went wrong, getopt will put out an error message for us echo "Invalid arguments" usage_info exit 1 fi set -- $options while [ $# -gt 0 ] do case $1 in # for options with required arguments, an additional shift is required -b|--build) BUILDFLAG=1 ;; -h|--help) usage_info ; exit 0;; -c|--country) COUNTRY="$2" ; shift;; -i|--iplist) IPLIST="$2" ; shift;; -o|--outputfile) OUTPUTFILE="$2" ; shift;; (--) shift; break;; (-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;; (*) break;; esac shift done # Remove single quotes around Variables COUNTRY=$(echo "$COUNTRY" | sed "s/'//g") IPLIST=$(echo "$IPLIST" | sed "s/'//g") OUTPUTFILE=$(echo "$OUTPUTFILE" | sed "s/'//g") if [ "$BUILDFLAG" == 1 ]; then echo "Building iplist $OUTPUTFILE" getproxies.sh -o -v | pv -lN "Getting IP Addresses" > /dev/null fi if [ "$IPLIST" == "" ]; then IPLIST="./proxielist.txt" echo "No IP list provided, Using Default IP List ($IPLIST)" fi ipcount=$(cat $IPLIST | grep -v "~"| wc -l) if [ "$ipcount" -lt 1 ]; then echo "No ip addresses to search" exit fi echo -n "$ipcount ip addresses to search. Getting country codes, This may take a while.." #Parallel processing ips+=$(cat $IPLIST | grep -v "~" | parallel -k 'country=$(ipcountry.sh {});echo "{},$country"') echo "Done" #echo "$ips" if [ "$COUNTRY" == "" ]; then echo "No Country Code or Output file Specified, showing all Ip Addresses with Countries." if [ "$OUTPUTFILE" == "" ]; then echo "$ips" | sed 's/,/\t\t\t/g' else echo "Writing All Ip Addresses and Country Codes to $OUTPUTFILE" echo -n "" >> $OUTPUTFILE echo "$ips" | sed 's/,/\t\t\t/g' > $OUTPUTFILE fi else if [ ${#COUNTRY} -gt 2 ]; then echo "Country Code not given, lets see if I can find the code for that country.." CountrySearch=$( grep -i "$COUNTRY" $COUNTRYCODES ) Results=$(echo $CountrySearch | wc -l) if [ "$Results" -lt 1 -o "$CountrySearch" == "" ]; then echo "$COUNTRY not found!" echo -n "Would you like to see the available countries there are to filter? y/n: " read Y_N if [ "$Y_N" == "y" -o "$Y_N" == "Y" -o "$Y_N" == "yes" ]; then clear cat $COUNTRYCODES | sed 's/\t/----\t-\t-\t---/g' exit 1 fi elif [ "$Results" -gt 1 ]; then echo "More than 1 match found." echo -n "Would you like to see the available countries there are to filter? y/n: " read Y_N if [ "$Y_N" == "y" -o "$Y_N" == "Y" -o "$Y_N" == "yes" ]; then clear cat $COUNTRYCODES | sed 's/\t/-----------------/g' exit 1 fi else COUNTRY=$( echo "$CountrySearch" | cut -f2) fi fi echo "Filtering Ip Addresses for $COUNTRY.." FilteredIps=$(echo "$ips" | grep -i "$COUNTRY" ) echo "$(echo $FilteredIps | wc -l) Ip Addresses for $COUNTRY found.." if [ "$OUTPUTFILE" == "" ]; then echo "$FilteredIps" | sed 's/,/\t\t\t/g' else echo "Writing Ip Addresses to file" echo "$FilteredIps" | sed 's/,$COUNTRY//g' > $OUTPUTFILE fi fi unix2dos $OUTPUTFILE 2&>1 /dev/null exit 0 #EOF