diff options
| author | Nikolaus Gotsche <n@softwarefools.com> | 2017-12-07 00:40:59 +0100 |
|---|---|---|
| committer | Nikolaus Gotsche <n@softwarefools.com> | 2017-12-07 00:40:59 +0100 |
| commit | cbe8e795737c231475fa7c53756058b1dd649cf5 (patch) | |
| tree | 0a5c74640a9b18d72e7e0ec73e3d6d22758cde55 /fancyIP | |
| parent | 588edb924e50f302e25a51ce2b86c093d0c6fa97 (diff) | |
now
Diffstat (limited to 'fancyIP')
| -rwxr-xr-x[l---------] | fancyIP | 231 |
1 files changed, 230 insertions, 1 deletions
@@ -1 +1,230 @@ -/home/nick/scripts/niceip
\ No newline at end of file +#!/bin/bash + +# NiceIP (c) Nik 2017 + +PROGNAME=$(basename $0) +TITLE="NiceIP $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 + +# Functions +#e_known() { printf "${green}✔ %s${reset}\n" "$@" +#} +#e_unknown() { printf "${red}✖ %s${reset}\n" "$@" +#} +#ask_confirm() { +# printf "\n${bold}$1${reset}" +# read -p " (y/n) " -n 1 +# printf "\n" +#} + +containsElement () { + local e match="$1" + shift + for e; do [[ "$e" == "$match" ]] && return 0; done + return 1 +} + +showDB () { + getcomment=$(sqlite3 $filename "SELECT * FROM iplist;") + echo "$getcomment" +} + +usage () { + echo "$PROGNAME: usage: $PROGNAME [options]" + echo " Mode 1: In a Pipe: ${bold}yourcommand | $PROGNAME [options]${reset}" + echo " -b | --bottom - Displays list of IPs on the bottom" + echo " -n | --nocomment - Turns off prompt for comments at the scripts end" + echo " " + echo " Mode 2: Interactive mode: ${bold} $PROGNAME ${reset}" + echo " Displays the contents of the DB" +} + + +# Process Parameters +#started in a Posix shell with no arguments in interactive mode +if [ $# -eq 0 ] && [ -t 0 ]; then + echo "nada" + #No arguments and pipes + showDB + +else + + +while [[ -n $1 ]]; do + case $1 in + -b | --bottom) bottom=1 + shift + # filename=$1 + ;; + -n | --nocomment) nocomment=1 + shift + # filename=$1 + ;; + -h | --help) usage + exit + ;; + *) usage >&2 + exit 1 + ;; + esac + shift +done + +# Yourcode here + +#find current terminal +PPID=`ps -o ppid -p $$` +echo parent PID $PPID +PTTY=`ps -p $PPID -o tty --no-headers` +echo parent TTY $PTTY + +#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 + echo "$filename : New IP table created" +else + echo "$filename : Table already exists" +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") + #echo "${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") + #echo "$u is known but unidentified" + else + #use green and display comment + replacement="${green}" + niparray+=("${green}$u${reset} is $getcomment") + #echo "$u is $getcomment" + fi + fi + + replacement+=$u + replacement+="${reset}" + + ITER=0 + for x in "${outarray[@]}" + do + #outarray+="$x" + outarray[$ITER]="${x/$u/$replacement}" + ITER=$(expr $ITER + 1) + # echo "$x" + done +done + + +#Echo all outlines from arrays +if [ $bottom -eq 0 ]; then + echo " " + for o in "${niparray[@]}" + do + echo "$o" + done +fi + +echo " " + +for v in "${outarray[@]}" +do + echo "$v" +done + +if [ $bottom -eq 1 ]; then + echo " " + for o in "${niparray[@]}" + do + echo "$o" + done +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]) #echo "You said $response" + exit + #break + ;; + *) #usage >&2 + #exit 1 + ;; + esac + shift + done < /dev/$PTTY + + 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';") + + #echo "Not Empty :'$comm'" + break + else + #echo "Empty :'$comm'" + break + fi + done < /dev/$PTTY + ITER=$(expr $ITER + 1) + + done + +fi +#fi +#ask_confirm() & |
