@@ -48,7 +48,7 @@ def __repr__(self):
48
48
def _ip (self ):
49
49
ip_cmd = self .find_command ("ip" )
50
50
if self .namespace is not None :
51
- ip_cmd = f"{ ip_cmd } netns exec { self .namespace } { ip_cmd } "
51
+ ip_cmd = f"{ ip_cmd } -n { self .namespace } "
52
52
if self .family is not None :
53
53
ip_cmd = f"{ ip_cmd } -f { self .family } "
54
54
return ip_cmd
@@ -57,27 +57,107 @@ def _ip(self):
57
57
def exists (self ):
58
58
return self .run_test ("{} -V" .format (self ._ip )).rc == 0
59
59
60
- def addresses (self ):
60
+ def addresses (self , address = None , ifname = None , local = None ):
61
61
"""Return the addresses associated with interfaces"""
62
62
cmd = f"{ self ._ip } --json address show"
63
63
out = self .check_output (cmd )
64
- return json .loads (out )
64
+ j = json .loads (out )
65
+ o = []
66
+ if address is None and ifname is None and local is None :
67
+ # no filters, bail out early
68
+ return j
69
+ if address is not None :
70
+ [o .append (x ) for x in j if x ["address" ] == address ]
71
+ if ifname is not None :
72
+ [o .append (x ) for x in j if x ["ifname" ] == ifname ]
73
+ if local is not None :
74
+ for x in j :
75
+ for addr in x ["addr_info" ]: # multiple IPs in an interface
76
+ if addr ["local" ] == local :
77
+ o .append (x )
78
+ return o
65
79
66
80
def links (self ):
67
81
"""Return links and their state"""
68
82
cmd = f"{ self ._ip } --json link show"
69
83
out = self .check_output (cmd )
70
84
return json .loads (out )
71
85
72
- def routes (self ):
86
+ def routes (
87
+ self , table = "all" , device = None , scope = None , proto = None , src = None , metric = None
88
+ ):
73
89
"""Return the routes installed"""
74
- cmd = f"{ self ._ip } --json route show table all"
90
+ cmd = f"{ self ._ip } --json route show "
91
+ options = []
92
+ if table is not None :
93
+ options += ["table" , table ]
94
+ if device is not None :
95
+ options += ["dev" , device ]
96
+ if scope is not None :
97
+ options += ["scope" , scope ]
98
+ if proto is not None :
99
+ options += ["proto" , proto ]
100
+ if src is not None :
101
+ options += ["src" , src ]
102
+ if metric is not None :
103
+ options += ["metric" , metric ]
104
+
105
+ cmd += " " .join (options )
75
106
out = self .check_output (cmd )
76
107
return json .loads (out )
77
108
78
- def rules (self ):
109
+ def rules (
110
+ self ,
111
+ src = None ,
112
+ to = None ,
113
+ tos = None ,
114
+ fwmark = None ,
115
+ iif = None ,
116
+ oif = None ,
117
+ pref = None ,
118
+ uidrange = None ,
119
+ ipproto = None ,
120
+ sport = None ,
121
+ dport = None ,
122
+ ):
79
123
"""Return the rules our routing policy consists of"""
80
- cmd = f"{ self ._ip } --json rule show"
124
+ cmd = f"{ self ._ip } --json rule show "
125
+
126
+ options = []
127
+ if src is not None :
128
+ options += ["from" , src ]
129
+
130
+ if to is not None :
131
+ options += ["to" , to ]
132
+
133
+ if tos is not None :
134
+ options += ["tos" , tos ]
135
+
136
+ if fwmark is not None :
137
+ options += ["fwmark" , fwmark ]
138
+
139
+ if iif is not None :
140
+ options += ["iif" , iif ]
141
+
142
+ if oif is not None :
143
+ options += ["oif" , oif ]
144
+
145
+ if pref is not None :
146
+ options += ["pref" , pref ]
147
+
148
+ if uidrange is not None :
149
+ options += ["uidrange" , uidrange ]
150
+
151
+ if ipproto is not None :
152
+ options += ["ipproto" , ipproto ]
153
+
154
+ if sport is not None :
155
+ options += ["sport" , sport ]
156
+
157
+ if dport is not None :
158
+ options += ["dport" , dport ]
159
+
160
+ cmd += " " .join (options )
81
161
out = self .check_output (cmd )
82
162
return json .loads (out )
83
163
0 commit comments