blob: 118e832c7dfee2c5abb398ea55e131521a043fbf (
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
|
#!/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
exit
fi
#else
while [ -n "$1" ]; do
case $1 in
-b | --bottom) bottom=1
# filename=$1
;;
-n | --nocomment) nocomment=1
# 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() &
|