Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
re6stnet
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Xiaowu Zhang
re6stnet
Commits
b767267a
Commit
b767267a
authored
May 13, 2016
by
Julien Muchembled
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
draft: add script to check kernel routing cache
parent
b12ace8c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
102 additions
and
0 deletions
+102
-0
draft/watch-rtcache
draft/watch-rtcache
+102
-0
No files found.
draft/watch-rtcache
0 → 100755
View file @
b767267a
#!/usr/bin/python
import
bisect
,
smtplib
,
socket
,
sqlite3
,
struct
,
subprocess
,
sys
from
email.mime.text
import
MIMEText
from
email.utils
import
formatdate
net
=
'2001:67c:1254:'
def
binFromIp
(
ip
):
ip
=
socket
.
inet_pton
(
socket
.
AF_INET6
,
ip
)
ip1
,
ip2
=
struct
.
unpack
(
'>QQ'
,
ip
)
return
bin
(
ip1
)[
2
:].
rjust
(
64
,
'0'
)
+
bin
(
ip2
)[
2
:].
rjust
(
64
,
'0'
)
def
ipFromBin
(
ip
):
ip
+=
'0'
*
(
128
-
len
(
ip
))
return
socket
.
inet_ntop
(
socket
.
AF_INET6
,
struct
.
pack
(
'>QQ'
,
int
(
ip
[:
64
],
2
),
int
(
ip
[
64
:],
2
)))
def
ip6route
(
*
args
):
r
=
{}
for
route
in
subprocess
.
check_output
((
'ip'
,
'-6'
,
'-o'
,
'r'
)
+
args
).
splitlines
():
route
=
route
.
split
()
unreachable
=
route
[
0
]
==
'unreachable'
dst
=
route
[
unreachable
]
if
dst
.
startswith
(
net
):
try
:
dst
,
n
=
dst
.
split
(
'/'
)
except
ValueError
:
n
=
128
r
[
binFromIp
(
dst
)[:
int
(
n
)]]
=
None
\
if
unreachable
else
(
route
[
2
],
route
[
4
])
return
sorted
(
r
.
iteritems
())
def
lookup
(
r
,
dst
):
i
=
bisect
.
bisect
(
r
,
(
dst
,))
if
i
==
len
(
r
)
or
dst
<
r
[
i
][
0
]:
i
-=
1
if
dst
.
startswith
(
r
[
i
][
0
]):
return
r
[
i
][
1
]
def
via_str
(
via
):
return
'unreachable'
if
via
is
None
else
'%s (%s)'
%
via
class
Invalid
(
dict
):
def
__init__
(
self
):
before
=
ip6route
()
cache
=
ip6route
(
'l'
,
'cached'
)
after
=
ip6route
()
for
dst
,
via
in
cache
:
expected
=
lookup
(
before
,
dst
)
if
expected
!=
via
!=
lookup
(
after
,
dst
):
self
[
ipFromBin
(
dst
)]
=
expected
,
via
def
__str__
(
self
):
return
"
\
n
"
.
join
(
"%s: expected %s, got %s"
%
(
ip
,
via_str
(
expected
),
via_str
(
via
))
for
ip
,
(
expected
,
via
)
in
sorted
(
self
.
iteritems
()))
if
__name__
==
"__main__"
:
if
len
(
sys
.
argv
)
>
1
:
from
time
import
time
,
sleep
db
,
smtp
,
to
=
sys
.
argv
[
1
:]
db
=
sqlite3
.
connect
(
db
)
db
.
execute
(
"CREATE TABLE IF NOT EXISTS invalid (ip, start, end)"
)
host
=
socket
.
gethostname
()
t
=
0
while
1
:
now
=
time
()
t
+=
60
if
now
<
t
:
sleep
(
t
-
now
)
now
=
time
()
else
:
t
=
now
invalid
=
Invalid
()
with
db
:
for
ip
,
in
db
.
execute
(
"SELECT ip FROM invalid WHERE end IS NULL"
):
try
:
del
invalid
[
ip
]
except
KeyError
:
db
.
execute
(
"UPDATE invalid SET end=? WHERE ip=? and end IS NULL"
,
(
int
(
now
),
ip
))
if
not
invalid
:
continue
db
.
executemany
(
"INSERT INTO invalid VALUES (?, ?, NULL)"
,
(
(
ip
,
int
(
now
))
for
ip
in
invalid
))
msg
=
MIMEText
(
str
(
invalid
))
msg
[
'Subject'
]
=
"%s invalid routes in cache"
%
len
(
invalid
)
msg
[
'From'
]
=
host
msg
[
'Date'
]
=
formatdate
(
now
)
try
:
s
=
smtplib
.
SMTP
(
smtp
)
s
.
sendmail
(
None
,
to
,
msg
.
as_string
())
s
.
close
()
except
socket
.
error
:
pass
else
:
invalid
=
Invalid
()
if
invalid
:
print
(
invalid
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment