@@ -48,7 +48,7 @@ def __repr__(self):
4848 def _ip (self ):
4949 ip_cmd = self .find_command ("ip" )
5050 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 } "
5252 if self .family is not None :
5353 ip_cmd = f"{ ip_cmd } -f { self .family } "
5454 return ip_cmd
@@ -57,27 +57,107 @@ def _ip(self):
5757 def exists (self ):
5858 return self .run_test ("{} -V" .format (self ._ip )).rc == 0
5959
60- def addresses (self ):
60+ def addresses (self , address = None , ifname = None , local = None ):
6161 """Return the addresses associated with interfaces"""
6262 cmd = f"{ self ._ip } --json address show"
6363 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
6579
6680 def links (self ):
6781 """Return links and their state"""
6882 cmd = f"{ self ._ip } --json link show"
6983 out = self .check_output (cmd )
7084 return json .loads (out )
7185
72- def routes (self ):
86+ def routes (
87+ self , table = "all" , device = None , scope = None , proto = None , src = None , metric = None
88+ ):
7389 """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 )
75106 out = self .check_output (cmd )
76107 return json .loads (out )
77108
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+ ):
79123 """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 )
81161 out = self .check_output (cmd )
82162 return json .loads (out )
83163
0 commit comments