#!/bin/bash # FancyIP (c) Nik 2017 PROGNAME=$(basename $0) TITLE="FancyIP $HOSTNAME" CURRENT_TIME=$(date +"%x %r %Z") TIMESTAMP="Generated $CURRENT_TIME, by $USER" interactive= filename="$HOME/.niceip.db" bold=$(tput bold) underline=$(tput sgr 0 1) reset=$(tput sgr0) purple=$(tput setaf 171) red=$(tput setaf 1) green=$(tput setaf 76) tan=$(tput setaf 3) blue=$(tput setaf 38) bottom=0 nocomment=0 interactive=0 silent=0 verbose=0 # Functions containsElement () { local e match="$1" shift for e; do [[ "$e" == "$match" ]] && return 0; done return 1 } showDB () { kip=() ipc=() uip=() OLDIFS=$IFS IFS=$'\n' getall=$(sqlite3 $filename "SELECT * FROM iplist;") #split into lines IFS=$'\n' read -r -a allin <<< "getall" for f in ${getall[@]}; do IFS=$'|' read -r -a line <<< "$f" if [[ "${line[3]}" == "empty" ]]; then #echo "emp ${line[1]} ${line[3]}" uip+=("${line[1]}") else #echo "some ${line[1]} ${line[3]}" kip+=("${line[1]}") ipc+=("${line[3]}") fi #echo "$f" #IFS=$'\n' done echo " === ${green}Identified${reset} Addresses ===" for v in "${!kip[@]}" do echo "${kip[$v]} : ${ipc[$v]}" done echo " === ${tan}Known${reset} Addresses ===" for v in "${uip[@]}" do echo "$v" done #IFS='\n' read -ra outl <<< "$getcomment" #for i in "${outl[@]}"; do # echo "$i" # done #echo "$getcomment" IFS=$OLDIFS } commentDB () { u=$1 url=$(echo $u | grep -oE "\b([0-9]{1,3}\.){3}[0-9]+\b") if [ -n "$url" ]; then getcomment=$(sqlite3 $filename "SELECT comment FROM iplist WHERE ip = '$url';") if [[ -z "${getcomment// }" ]]; then read -r -p "Unknown $url: " response getcomment=$(sqlite3 $filename "INSERT INTO iplist (ip,comment) VALUES ('$url','$response');") else read -r -p "Known $url: " response putcomment=$(sqlite3 $filename "UPDATE iplist SET comment = '$response' WHERE ip = '$url';") fi else echo " ... is not a valid url" fi } removeDB () { u=$1 url=$(echo $u | grep -oE "\b([0-9]{1,3}\.){3}[0-9]+\b") if [ -n "$url" ]; then getcomment=$(sqlite3 $filename "SELECT comment FROM iplist WHERE ip = '$url';") if [[ -z "${getcomment// }" ]]; then echo " ... is not known" else echo "$url : $getcomment" while true; do read -r -p " Do you really want to delete this Entry? [y/N] " response case $response in [yY][eE][sS]|[yY]) putcomment=$(sqlite3 $filename "DELETE FROM iplist WHERE ip = '$url';") ;; [nN][oO]|[nN]) break ;; esac done fi else echo " ... is not a valid url" fi } usage () { if [ $interactive -eq 0 ];then echo "$PROGNAME: usage: $PROGNAME [options]" echo " Mode 1: In a Pipe: ${bold}yourcommand | $PROGNAME [options]${reset}" echo " -s | --silent - no additional output" echo " -b | --bottom - Displays list of IPs on the bottom" echo " -n | --nocomment - Turns off prompt for comments at the scripts end" echo " -v | --verbose - Show all the outputs" echo " -h | --help - Displays this Message" echo " " echo " Mode 2: Interactive mode: ${bold} $PROGNAME ${reset}" echo " Displays the contents of the DB" echo " h | help - Displays usage help Message" else echo " h | help - Displays this Message" fi echo " d | delete [ip] - Delete IP(s)" echo " c | comment [ip] - Edit Comment of IP(s) or add new one(s)" echo " s | show - Show all IPs" echo " q | quit | exit - Quit the script" echo " " } # Process Parameters #started in a Posix shell with no arguments in interactive mode if [ $# -eq 0 ] && [ -t 0 ]; then interactive=1 #No arguments and pipes showDB #exit while true; do echo " " read -r -p "What do you want to do? [c x/d x/s/h/q] " response #holder && response=$holder ITER=0 comm="" for part in $response do if [ $ITER -eq 0 ];then comm=$part case $comm in q | quit | exit) exit ;; h | help) usage ;; s | show) showDB ;; esac else case $comm in c | comment) commentDB $part ;; d | delete) removeDB $part ;; *) usage >&2 exit 1 ;; esac fi ITER=$(expr $ITER + 1) done done exit fi #Any Other Posix shell call with arguments show help if [ -t 0 ]; then usage >&2 exit 1 fi #Process arguments when called in a pipe while [ -n "$1" ]; do case $1 in -b | --bottom) bottom=1 ;; -n | --nocomment) nocomment=1 ;; -s | --silent) silent=1 ;; -v | --verbose) verbose=1 ;; -h | --help) usage exit ;; *) usage >&2 exit 1 ;; esac shift done # Yourcode here #find current terminal #PPID=`ps -o ppid -p $$` #PPID=$$ if [ $verbose -eq 1 ]; then # echo parent PID $PPID echo parent PID $$ fi #PTTY=`ps -p $PPID -o tty --no-headers` PTTY=`ps -p $$ -o tty --no-headers` if [ $verbose -eq 1 ]; then echo parent TTY $PTTY fi #Empty arrays to be filled iparray=() niparray=() outarray=() #Create new table 'iplist' and drop error to stdout if it already exists tablenew=$(sqlite3 $filename "create table iplist (id INTEGER PRIMARYKEY,ip TEXT,name TEXT,comment TEXT);" 2>&1) if [[ -z "${tablenew// }" ]]; then if [ $verbose -eq 1 ]; then echo "$filename : New IP table created" fi else if [ $verbose -eq 1 ]; then echo "$filename : Table already exists" fi fi #Read all the piped output into arraylines #while read line; do #does toomuch field splitting ... better: while IFS= read -r line; do outarray+=("${line}") #check if there is an IPv4 put it to array foundip=$(echo $line | grep -oE "\b([0-9]{1,3}\.){3}[0-9]+\b") #check if it is already in if [[ ! " ${iparray[@]} " =~ " ${foundip} " ]]; then iparray+=($(echo $line | grep -oE "\b([0-9]{1,3}\.){3}[0-9]+\b")) fi done #step through array of ip's for u in "${iparray[@]}" do #check if IP $u is already in table getcomment=$(sqlite3 $filename "SELECT comment FROM iplist WHERE ip = '$u';") if [[ -z "${getcomment// }" ]]; then #getcomment comes back empty #colour the IP in red and add it to db with "empty" comment replacement="${red}" getcomment=$(sqlite3 $filename "INSERT INTO iplist (ip,comment) VALUES ('$u','empty');") niparray+=("${red}$u${reset} is yet unknown") else #if no usercomment has been set colour it tan if [[ "${getcomment// }" == "empty" ]]; then replacement="${tan}" niparray+=("${tan}$u${reset} is known but unidentified") else #use green and display comment replacement="${green}" niparray+=("${green}$u${reset} is $getcomment") fi fi replacement+=$u replacement+="${reset}" #Reconstruct the Piped content with replacements ITER=0 for x in "${outarray[@]}" do outarray[$ITER]="${x/$u/$replacement}" ITER=$(expr $ITER + 1) done done #Echo all outlines from arrays if [ $bottom -eq 0 ] && [ $silent -eq 0 ]; then echo " " for o in "${niparray[@]}" do echo "$o" done echo " " fi for v in "${outarray[@]}" do echo "$v" done if [ $bottom -eq 1 ] && [ $silent -eq 0 ]; then echo " " for o in "${niparray[@]}" do echo "$o" done echo " " fi #Ask for Comments if [ $nocomment -eq 0 ]; then while true; do IFS= read -r -p "Do you want to identify the IPs? [y/N] " holder && response=$holder case $response in [yY][eE][sS]|[yY]) echo "Enter New Comments or leave empty for unchanged comments" break ;; [nN][oO]|[nN]) exit ;; *) #exit 1 ;; esac shift done < /dev/$PTTY #Step through all ips in iparray and read new comments ITER=0 for v in "${iparray[@]}" do while true; do echo "${niparray[$ITER]}" IFS= read -r -p "$v : " holder && comm=$holder if [[ ! -z "${comm// }" ]]; then putcomment=$(sqlite3 $filename "UPDATE iplist SET comment = '$comm' WHERE ip = '$v';") break else break fi done < /dev/$PTTY ITER=$(expr $ITER + 1) done fi #fi #ask_confirm() &