summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikolaus Gotsche <n@softwarefools.com>2017-12-11 03:48:11 +0100
committerNikolaus Gotsche <n@softwarefools.com>2017-12-11 03:48:11 +0100
commited7a6f2cbf8b56eb643b33b80b155da429337cab (patch)
tree71e06fcf250e8fb088f3726ec3141199524dd946
parent28ffeaad854f329f8ac58a6faee3bc98439de24e (diff)
Interactive Mode
Comment Delete Smore PipeMode Verbose Silent
-rwxr-xr-xfancyIP172
1 files changed, 138 insertions, 34 deletions
diff --git a/fancyIP b/fancyIP
index 118e832..b6521e7 100755
--- a/fancyIP
+++ b/fancyIP
@@ -1,9 +1,9 @@
#!/bin/bash
-# NiceIP (c) Nik 2017
+# FancyIP (c) Nik 2017
PROGNAME=$(basename $0)
-TITLE="NiceIP $HOSTNAME"
+TITLE="FancyIP $HOSTNAME"
CURRENT_TIME=$(date +"%x %r %Z")
TIMESTAMP="Generated $CURRENT_TIME, by $USER"
interactive=
@@ -21,17 +21,11 @@ blue=$(tput setaf 38)
bottom=0
nocomment=0
+interactive=0
+silent=0
+verbose=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"
@@ -45,35 +39,137 @@ showDB () {
echo "$getcomment"
}
+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 () {
- 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"
+ 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 " "
- 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
- exit
+ 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) #echo "$part comments?"
+ commentDB $part
+ ;;
+ d | delete) #echo "delete shiti $part"
+ removeDB $part
+ ;;
+ *) usage >&2
+ exit 1
+ ;;
+ esac
+ fi
+ ITER=$(expr $ITER + 1)
+ #$ITER+=1
+ #shift
+ done
+ done
+ exit
fi
#else
+#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
- # filename=$1
;;
-n | --nocomment) nocomment=1
- # filename=$1
+ ;;
+ -s | --silent) silent=1
+ ;;
+ -v | --verbose) verbose=1
;;
-h | --help) usage
exit
@@ -88,11 +184,15 @@ done
# Yourcode here
#find current terminal
-PPID=`ps -o ppid -p $$`
-echo parent PID $PPID
+#PPID=`ps -o ppid -p $$ 2>/dev/null`
+PPID=$$
+if [ $verbose -eq 1 ]; then
+ echo parent PID $PPID
+fi
PTTY=`ps -p $PPID -o tty --no-headers`
-echo parent TTY $PTTY
-
+if [ $verbose -eq 1 ]; then
+ echo parent TTY $PTTY
+fi
#Empty arrays to be filled
iparray=()
niparray=()
@@ -101,9 +201,13 @@ 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"
+ if [ $verbose -eq 1 ]; then
+ echo "$filename : New IP table created"
+ fi
else
- echo "$filename : Table already exists"
+ if [ $verbose -eq 1 ]; then
+ echo "$filename : Table already exists"
+ fi
fi
#Read all the piped output into arraylines
@@ -148,39 +252,39 @@ do
replacement+=$u
replacement+="${reset}"
+ #Reconstruct the Piped content with replacements
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
+if [ $bottom -eq 0 ] && [ $silent -eq 0 ]; then
echo " "
for o in "${niparray[@]}"
do
echo "$o"
done
+ echo " "
fi
-echo " "
for v in "${outarray[@]}"
do
echo "$v"
done
-if [ $bottom -eq 1 ]; then
+if [ $bottom -eq 1 ] && [ $silent -eq 0 ]; then
echo " "
for o in "${niparray[@]}"
do
echo "$o"
done
+ echo " "
fi
#Ask for Comments