...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package utils
17
18 import (
19 "bytes"
20 "context"
21
22 "github.com/retailnext/iptables_exporter/iptables"
23
24 "github.com/chaos-mesh/chaos-mesh/pkg/bpm"
25 )
26
27 func GetIptablesContentByNetNS(pid uint32) (tables iptables.Tables, err error) {
28 return getIptablesContent(true, pid)
29 }
30
31 func getIptablesContent(enterNS bool, pid uint32) (tables iptables.Tables, err error) {
32 builder := bpm.DefaultProcessBuilder("iptables-save", "-c")
33 if enterNS {
34 builder = builder.SetNS(pid, bpm.NetNS)
35 }
36
37 out, err := builder.Build(context.TODO()).CombinedOutput()
38 if err != nil {
39 return nil, err
40 }
41
42 return iptables.ParseIptablesSave(bytes.NewReader(out))
43 }
44