Skip to content

Instantly share code, notes, and snippets.

@shibumi
Last active September 8, 2015 18:45
Show Gist options
  • Save shibumi/5b485b5126aafbcbdbba to your computer and use it in GitHub Desktop.
Save shibumi/5b485b5126aafbcbdbba to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# scan.sh - simple ip list parser and whois tool
#
# Copyright (c) 2015 by Christian Rebischke
# <echo Q2hyaXMuUmViaXNjaGtlQGdtYWlsLmNvbQo= | base64 -d>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http: #www.gnu.org/licenses/
#
#======================================================================
# Author: Christian Rebischke
# Email : echo Q2hyaXMuUmViaXNjaGtlQGdtYWlsLmNvbQo= | base64 -d
# Github: www.github.com/Shibumi
#
# scan.sh parsed ip list, whois'ed every ip and sorted by country
#
#
# vim:set et sw=2 ts=2 tw=80:
#
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
# simple check for count of arguments
if [ "$#" -ne 2 ]; then
echo "usage: ./scan.sh <source.txt> <target.txt>"
exit 1
fi
# simple check if source file exists
if [ ! -e $1 ]; then
echo "$1 does not exist"
exit 1
fi
# simple check if target file exists if not create, if yes add or delete or exit
if [ -e $2 ]; then
read -r -p "$2 exists do you want to (d)elete, (a)dd output to file or (e)xit? " answer
case $answer in
[dD])
rm $2
;;
[aA])
;;
*)
exit 1
;;
esac
fi
# main programm
for l in `cat $1`
do
h=$l
if ! valid_ip $l; then
l=`host $l | cut -d" " -f4 | head -1`
fi
echo -e "$h\t`whois $l | grep 'country'`\t`whois $l | grep 'origin'`\t`whois $l | grep 'desc' -m 1`" >> $2
done
column -t $2 > result2.txt
sort -k3 result2.txt > $2
rm result2.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment