summaryrefslogtreecommitdiff
path: root/fancyIP
blob: b6521e78447083447a545a30ecca3726ce380bb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/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 () {
   getcomment=$(sqlite3 $filename "SELECT * FROM iplist;")
    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 () {
    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)        #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
                            ;;
        -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 $$ 2>/dev/null` 
PPID=$$ 
if [ $verbose -eq 1 ]; then 
    echo parent PID $PPID 
fi
PTTY=`ps -p $PPID -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")
        #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}"
    
    #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])      #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() &