chiark / gitweb /
* We need to add the local network rule as well.
[source-route-watch] / source-route-watch.sh
1 #! /bin/bash
2 set -ex
3
4 while true
5 do
6     stdbuf -oL ip monitor route | (
7         while read X
8         do
9             read -t 1 -N 100000 || true # clear stdin
10             for int in $( sed -n '/# local/,$ { /^[^#]/ s/.* // p }' /etc/iproute2/rt_tables )
11             do
12                 for proto in 4 6
13                 do
14                     tab=$(ip -"$proto" route list table "$int" match default |
15                                  sed 's/\(dev [^ ]*\) .*/\1/')
16                     def=$(ip -"$proto" route list match default |
17                                  sed -n '/dev '"$int"'/ { 
18                                      s/\(dev [^ ]*\) .*/\1/
19                                      p
20                                      }')
21                     if [ "x$def" != "x$tab" ]
22                     then
23                         ip -"$proto" route flush table "$int" || true
24                         ip -"$proto" route add $def table "$int" || true
25                         ip -"$proto" route add $(
26                             ip -"$proto" route list proto kernel |
27                                  sed -n '/dev '"$int"'/ { 
28                                      s/\(dev [^ ]*\) .*/\1/
29                                      p
30                                      q
31                                      }'
32                            ) table "$int" || true
33                         ip -"$proto" rule del table "$int" || true
34                         ip -"$proto" rule add from $(
35                             ip -"$proto" addr show "$int" scope global | sed -n '
36                              /inet/ {
37                                  s/.*inet[^ ]* //
38                                  s/ .*//
39                                  p
40                                  }') table "$int"
41                     fi
42                 done
43             done
44         done
45     )
46 done