/
oc-ports.sh
113 lines (106 loc) · 2.41 KB
/
oc-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
#!/bin/bash
howto(){
echo "Usage: oc-ports.sh -n <namespace> -p <pod> -a (all namespaces & pods)"
echo "Example: oc-ports.sh -n openshift-storage"
echo "Example: oc-ports.sh -n openshift-storage -p rook-ceph-operator-85d47cf975-l69r4"
echo "Example: oc-ports.sh -a"
}
tcp_state(){
case $listen in
01)
listen="TCP_ESTABLISHED"
;;
02)
listen="TCP_SYN_SENT"
;;
03)
listen="TCP_SYN_RECV"
;;
04)
listen="TCP_FIN_WAIT1"
;;
05)
listen="TCP_FIN_WAIT2"
;;
06)
listen="TCP_TIME_WAIT"
;;
07)
listen="TCP_CLOSE"
;;
08)
listen="TCP_CLOSE_WAIT"
;;
09)
listen="TCP_LAST_ACK"
;;
0A)
listen="TCP_LISTEN"
;;
0B)
listen="TCP_CLOSING"
;;
0C)
listen="TCP_NEW_SYN_RECV"
;;
*)
listen="UKNOWN"
;;
esac
}
getsocketdetails(){
format="%-15s%-15s%-15s%-15s%-12s%-20s%-50s%-30s\n"
sockets=`oc exec -q $pod -n $namespace -- grep -v "rem_address" /proc/net/tcp | awk '{ print $2":"$3":"$4":"$10 }'`
printf "$format" LocalAddr LocalPort RemoteAddr RemotePort Inode PortState Namespace Pod
printf "$format" --------- --------- ---------- ---------- --------- --------- --------- -----------
for socket in $(echo $sockets)
do
IFS=':' read -r localaddr localport remoteaddr remoteport listen inode <<< $socket
localaddr=$(printf "%d." $(echo $localaddr | sed 's/../0x& /g' | tr ' ' '\n' | tac) | sed 's/\.$/\n/')
localport=$(echo $((0x$localport)))
remoteaddr=$(printf "%d." $(echo $remoteaddr | sed 's/../0x& /g' | tr ' ' '\n' | tac) | sed 's/\.$/\n/')
remoteport=$(echo $((0x$remoteport)))
tcp_state
printf "$format" "$localaddr" "$localport" "$remoteaddr" "$remoteport" "$inode" "$listen" "$namespace" "$pod"
done
printf "$format" " "
}
all=0
while getopts n:p:ah option
do
case "${option}"
in
n) namespace=${OPTARG};;
p) pod=${OPTARG};;
a) all=1;;
h) howto; exit 0;;
\?) howto; exit 1;;
esac
done
if ([ -z "$namespace" ] && [ "$all" -eq "0" ]) then
howto
exit 1
fi
if ([ -z "$namespace" ] && [ -z "$pod" ] && [ "$all" -eq "1" ]);
then
for namespace in `oc get namespaces | egrep -v NAME | awk {'print $1'}`
do
for pod in `oc get pods -n $namespace | egrep -v NAME | grep Running | awk {'print $1'}`
do
getsocketdetails
done
done
elif ([ -z "$pod" ] && [ "$all" -eq "0" ]);
then
for pod in `oc get pods -n $namespace | egrep -v NAME | grep Running | awk {'print $1'}`
do
getsocketdetails
done
elif ([ "$all" -eq "0" ]);
then
getsocketdetails
else
howto
exit 1
fi
exit