TiledViz
Loading...
Searching...
No Matches
nft_clean_rules.py
1#!/usr/bin/env python3
2import sys,os,stat
3import argparse
4import re
5
6import nftables
7
8def parse_args(argv):
9 parser = argparse.ArgumentParser("Use as root to destroy TiledViz connectiondock nft rules during debug.")
10 parser.add_argument('--all', action='store_true',
11 help='Destroy all TiledViz chains.',default=False)
12 parser.add_argument('-n', '--connectionnumber',
13 help='Docker connectiondock number to be closed.')
14 args = parser.parse_args(argv[1:])
15 return args
16
17args = parse_args(sys.argv)
18
19nft = nftables.Nftables()
20
21if (args.all):
22 print("destroy chain ip filter TILEDVIZ")
23 nft.cmd("destroy chain ip filter TILEDVIZ")
24
25 rc, output, error = nft.cmd("list table ip filter")
26 #print(f"{rc}\n\n {output}\n\n {error}\n\n")
27 matches = re.findall(r'connectiondock[0-9]+',output)
28 print(matches)
29 for i in matches :
30 destroychain=f"destroy chain ip filter {i}"
31 print(destroychain)
32 nft.cmd(destroychain)
33else:
34 id=int(args.connectionnumber)
35 ConnectName=f"connectiondock{id}"
36
37 print(f"remove jump connectiondock {ConnectName} rule in TILEDVIZ chain")
38 nft.set_handle_output("True")
39 rc, output, error = nft.cmd("list table ip filter")
40 #print(f"{rc}\n\n {output}\n\n {error}\n\n")
41 matches = re.findall("jump " + ConnectName + " # handle [0-9]+",output)
42 print(f"{matches}")
43 if (len(matches) > 0):
44 handle_num = re.sub("jump " + ConnectName + " # handle ","", matches[0])
45 strdelete=f"delete rule ip filter TILEDVIZ handle {handle_num}"
46 print(strdelete)
47 nft.cmd(strdelete)
48 else:
49 print(f"connection {ConnectName} not detected!")
50
51 strdestroy=f"destroy chain ip filter {ConnectName}"
52 print(strdestroy)
53 nft.cmd(strdestroy)
54