summaryrefslogtreecommitdiff
path: root/network-topology-scanner
blob: d2929be6a9e2c3d0ecd7941bab00101fd3688730 (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
#!/bin/bash

hosts=(
  'router 192.168.0.13'
  'rpi3 192.168.1.3'
)

tmp_dir=$(mktemp -d)
trap 'rm -rf --one-file-system "${tmp_dir:?}"' EXIT

for host in "${hosts[@]}"; do
  name="${host% *}"
  addr="${host##* }"
  printf '"%s" [fontcolor="#800000"];\n' "${name}" >> "${tmp_dir}/print-content"
  ssh "${addr}" ip -o addr | \
    sed -n '
      s/\s*\\.*$//
      s/\s\+/ /g
      / scope link/d
      s/^[0-9]\+: //
      /^lo /d
      h
      s/^\(\S\+\) .*$/'"${name}"' \1/
      w '"${tmp_dir}"'/interfaces
      s/^\(.\+\) \(\S\+\)$/"\0" [label = "\2", fontcolor="#00ff00"];/
      w '"${tmp_dir}/print-content"'
      g
      s/^\(\S\+\) .*$/"'"${name}"'" -> "'"${name}"' \1";/
      w '"${tmp_dir}/print-content"'
      g
      s/^\(\S\+\) \S\+ \(\S\+\) .*$/"'"${name}"' \1" -> "\2";/
      w '"${tmp_dir}/print-content"'
      g
      s/^\S\+ \(\S\+\) \(\S\+\) .*$/\1 \2/
      w '"${tmp_dir}"'/ips
    '
  for file in ips interfaces print-content; do
    cat "${tmp_dir}/${file}" >> "${tmp_dir}/all-${file}"
  done
  ssh "${addr}" 'sudo iptables-save | sed "s/^/4 /"; sudo ip6tables-save | sed "s/^/6 /"' 2>/dev/null | \
    sed '
      s/^/'"${name}"' /
    ' >> \
    "${tmp_dir}/iptables"
done

while read -r version address; do
  case ${version} in
    inet)
      printf '"%s" [fontcolor="#000080"];\n' "${address}" >> "${tmp_dir}/adress-types"
    ;;
    inet6)
      printf '"%s" [fontcolor="#0000ff"];\n' "${address}" >> "${tmp_dir}/adress-types"
    ;;
  esac
  subnet_mask=${address##*/}
  if [ -z "${subnet_mask}" ] || [ "${subnet_mask}" = "${address}" ]; then
    continue;
  fi
  case ${version} in
    inet)
      printf '"%s" [fontcolor="#000080"];\n' "${address}" >> "${tmp_dir}/adress-types"
      if [ "${subnet_mask}" = 32 ]; then
        continue
      fi
      base_address=$(
        sipcalc "${address}" | \
          sed '
            s/^Network address\s\+- \+\(\S\+\)\s*$/\1/
            t
            s/^Network mask (bits)\s\+- \+\([0-9]\+\)\s*$/\1/
            t
            d
          ' | \
          sed '
            N
            s,\n,/,
          '
      )
    ;;
    inet6)
      if [ "${subnet_mask}" = 128 ]; then
        continue
      fi
      base_address=$(
        sipcalc "${address}" | \
          sed -n '
            s/^Subnet\s\+prefix\s\+(masked)\s\+-\s\+\(\S\+\)\s*$/\1/
            T
            s,0\(:0\)*/,:/,
            p
          '
      )
    ;;
    *)
      continue
    ;;
  esac
  printf '%s %s\n' \
    "${address}" \
    "${base_address}"
done \
  < "${tmp_dir}/all-ips" \
  | sort -u \
  | sort -k2,2 -k1,1 \
  | uniq -Df1 \
  | sed '
    s/^\(\S\+\) \(\S\+\)$/"\1" -> "\2";\n/
  ' \
  | tee -a "${tmp_dir}/all-print-content" \
  | sed -n '
    s/^".*" -> "\(\S\+\)";$/\1/
    T
    /:/ {
      s/.*/"\0" [fontcolor="#a0a000"];/
      p
    }
    /\./ {
      s/.*/"\0" [fontcolor="#808000"];/
      p
    }
  ' \
  >> "${tmp_dir}/adress-types"

cat "${tmp_dir}/adress-types" >> "${tmp_dir}/all-print-content"

grep '\S\+ [46] :FORWARD ACCEPT ' "${tmp_dir}/iptables" | \
  while read -r host ipver _; do
    grep "^${host} " "${tmp_dir}/all-interfaces"  
  done | \
  sort -u > \
  "${tmp_dir}/ifs-routed"
join -j1 "${tmp_dir}/ifs-routed" "${tmp_dir}/ifs-routed" \
  | grep -v ' \(\S\+\) \1$' \
  | sed '
    s/^\(\S\+\)\( .*\)$/\1 4\2\n\1 6\2/
  ' \
  > "${tmp_dir}/routes"

grep -vwF "$(
  grep -vwF -- '-m conntrack' "${tmp_dir}/iptables" | \
    grep -v '^\S\+ 4 .*/32 ' | \
    grep -v '^\S\+ 6 .*/128 ' | \
    sed -n '
      s/^\(\S\+\) \([46]\) \(.* \)\?-A FORWARD \(.* \)\?-i \(\S\+\) \(.* \)\?-j DROP/\1 \2 \5/
      T
      p
    '
)" "${tmp_dir}/routes" \
  > "${tmp_dir}/routes.new"
mv "${tmp_dir}/routes.new" "${tmp_dir}/routes"

grep -vwF -- '-m conntrack' "${tmp_dir}/iptables" | \
  grep -v '^\S\+ 4 .*/32 ' | \
  grep -v '^\S\+ 6 .*/128 ' | \
  sed '
    s/^\(\S\+\) \([46]\) \(.* \)\?-A FORWARD \(.* \)\?-i \(\S\+\) \(.* \)\?-o \(\S\+\) \(.* \)\?-j ACCEPT/\1 \2 \5 \7/
    t
    s/^\(\S\+\) \([46]\) \(.* \)\?-A FORWARD \(.* \)\?-o \(\S\+\) \(.* \)\?-i \(\S\+\) \(.* \)\?-j ACCEPT/\1 \2 \7 \5/
    t
    d
  ' \
  >> "${tmp_dir}/routes"

sed '
  s/^\(\S\+ \S\+\) \(\S\+\) \(\S\+\)$/f \1 \2 \3\nr \1 \3 \2/
' "${tmp_dir}/routes" \
  | while read -r dir host ip from to; do
    printf '%s %s %s %s %s %s\n' \
      "${dir}" \
      "${from}" \
      "${to}" \
      "${host}" \
      "${ip}" \
      "$(
        printf '%s\n' "${from}" "${to}" | \
          sort | \
          sha512sum | \
          awk '{print $1}'
      )"
  done \
  | sort -k2,6 -k1,1 \
  > "${tmp_dir}/routes.new"

uniq -uf1 "${tmp_dir}/routes.new" | tee /dev/stderr \
  | grep '^f ' \
  > "${tmp_dir}/routes"

uniq -df1 "${tmp_dir}/routes.new" \
  | sed 's/^[fr] /b /' \
  | sort -k4,6 -k1,3 \
  | uniq -f3 \
  >> "${tmp_dir}/routes"
rm "${tmp_dir}/routes.new"

sed '
  s/^f \(\S\+\) \(\S\+\) \(\S\+\) 4 \S\+$/"\3 \1" -> "\3 \2" [weight=0, color="#808080"];\n/
  t
  s/^f \(\S\+\) \(\S\+\) \(\S\+\) 6 \S\+$/"\3 \1" -> "\3 \2" [weight=0, color="#000000"];\n/
  t
  s/^b \(\S\+\) \(\S\+\) \(\S\+\) 4 \S\+$/"\3 \1" -> "\3 \2" [dir=both, weight=0, color="#808080"];\n/
  t
  s/^b \(\S\+\) \(\S\+\) \(\S\+\) 6 \S\+$/"\3 \1" -> "\3 \2" [dir=both, weight=0, color="#000000"];\n/
  t
  d
' "${tmp_dir}/routes" \
  >> "${tmp_dir}/all-print-content"

sort -u "${tmp_dir}/all-print-content" | cat -n

{
  echo 'digraph network {'
  echo 'rankdir=LR;'
  sort -u "${tmp_dir}/all-print-content"
    echo '}'
} | \
  dot -Tpng -onetwork.png