-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathports.sh
177 lines (169 loc) · 4.4 KB
/
ports.sh
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
lib.ports() {
case "$1" in
# Return a free port number
free )
lib ports.free
;;
--help | -h | help )
cat << HELP
View ports in use:
$(lib Text color green lib ports)
Filter ports in use by keyword:
$(lib Text color green lib ports '--<keyword> <value>')
Examples:
$(lib Text color orange 'lib ports --port 80')
$(lib Text color orange 'lib ports --pid 1000')
$(lib Text color orange 'lib ports --command mongod')
$(lib Text color orange 'lib ports --protocol tcp')
$(lib Text color orange 'lib ports --ipv 6')
Filter ports in use by column:
$(lib Text color green 'lib ports --columns <column,...>')
Examples:
$(lib Text color orange 'lib ports --columns port')
$(lib Text color orange 'lib ports --columns port,pid')
Column can be one of the following:
command
pid
port
protocol
Find a free port:
$(lib Text color green 'lib ports free')
HELP
;;
* )
# Get list of free ports
local netstat="$(netstat 2>/dev/null -tulpn)";
local lines=();
local columns=( $(lib Array after --columns "$@" | lib Text split ,) );
if ! lib >/dev/null Regex match '\-\-' "$@" ||
lib >/dev/null Regex match '\-\-columns' "$@"; then
for (( i=1; i<=$(echo "$netstat" | wc -l); i++ ))
do
lines+=($i);
done
else
# Get pid
local pid="$(lib Array after --pid "$@")";
if [ -n "$pid" ]; then
while read line
do
lines+=( $line );
done << BASH
$(echo "$netstat" | grep -n $pid | cut -d: -f1)
BASH
fi
# Get port number
local port="$(lib Array after --port "$@")";
if [ -n "$port" ]; then
while read line
do
lines+=( $line );
done << BASH
$(echo "$netstat" | grep -n ':'$port | cut -d: -f1)
BASH
fi
# Get command
local command="$(lib Array after --command "$@")";
if [ -n "$command" ]; then
while read line
do
lines+=( $line );
done << BASH
$(echo "$netstat" | grep -n /$command | cut -d: -f1)
BASH
fi
# Get protocol
local protocol="$(lib Array after --protocol "$@")";
if [ -n "$protocol" ]; then
while read line
do
lines+=( $line );
done << BASH
$(echo "$netstat" | grep -n $protocol | cut -d: -f1)
BASH
fi
# Get IP version
local ipv="$(lib Array after --ipv "$@")";
if lib >/dev/null isANumber "$ipv"; then
case "$ipv" in
4 )
while read line
do
lines+=( $line );
done << BASH
$(echo "$netstat" | egrep -n '(tcp\s)|(udp\s)' | cut -d: -f1)
BASH
;;
6 )
while read line
do
lines+=( $line );
done << BASH
$(echo "$netstat" | egrep -n '(tcp6)|(udp6)' | cut -d: -f1)
BASH
;;
esac
fi
fi
local liner=$((1));
while read line
do
if lib >/dev/null Regex match ':' "$line"; then
if lib >/dev/null Array has $liner "${lines[@]}"; then
local protocol="$(echo "$line" | cut -d' ' -f1)";
local local_address="$(echo "$line" | awk '{ print $4 }')";
local local_ip="$(echo "$local_address" | cut -d: -f1)";
local local_port="$(lib Text split ':' "$local_address" | lib Array end)";
local foreign_address="$(echo "$line" | awk '{ print $5 }')";
local foreign_ip="$(echo "$foreign_address" | cut -d: -f1)";
local foreign_port="$(echo "$foreign_address" | cut -d: -f2)";
local pid="$(echo "$line" | awk '{ print $7 }' | cut -d/ -f1)";
local command="$(echo "$line" | awk '{ print $7 }' | cut -d/ -f2)";
if [ ${#columns[@]} -eq 0 ]; then
echo "$line";
else
print="";
for column in "${columns[@]}"
do
case "$column" in
pid )
if lib >/dev/null isANumber "$pid"; then
print="$print$pid ";
else
print="${print}-- ";
fi
;;
port )
if lib >/dev/null isANumber "$local_port"; then
print="$print$local_port ";
else
print="${print}-- ";
fi
;;
command )
if [ -n "$command" ]; then
print="$print$command ";
else
print="${print}-- ";
fi
;;
protocol )
if [ -n "$protocol" ]; then
print="$print$protocol ";
else
print="${print}-- ";
fi
;;
esac
done
echo "$print";
fi
fi
fi
(( liner++ ));
done << BASH
$netstat
BASH
;;
esac
}