Netdev List
 help / color / mirror / Atom feed
* [PATCH iproute2-next] routel: handle errors from ip command
@ 2026-05-17 22:44 Stephen Hemminger
  0 siblings, 0 replies; only message in thread
From: Stephen Hemminger @ 2026-05-17 22:44 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger, Christian Iacobellis

When ip exits non-zero, routel crashed with a JSONDecodeError traceback
on top of ip's own diagnostic. Use subprocess.run with check=True,
propagate ip's exit status, handle a missing ip binary, and wrap the
JSON parse so malformed output gives a clean message.

While here, send diagnostics to stderr and drop the assert-as-control-
flow (stripped under python -O).

Fixes: 6d676ad93408 ("ip: rewrite routel in python")
Reported-by: Christian Iacobellis <Ciacobellis@agea.com.ar>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 ip/routel | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/ip/routel b/ip/routel
index 09a90126..51b02d21 100755
--- a/ip/routel
+++ b/ip/routel
@@ -13,7 +13,8 @@ import subprocess
 
 def usage():
     '''Print usage and exit'''
-    print("Usage: {} [tablenr [raw ip args...]]".format(sys.argv[0]))
+    print("Usage: {} [tablenr [raw ip args...]]".format(sys.argv[0]),
+          file=sys.stderr)
     sys.exit(64)
 
 
@@ -23,7 +24,7 @@ def main():
     try:
         opts, args = getopt.getopt(sys.argv[1:], "h46f:", ["help", "family="])
     except getopt.GetoptError as err:
-        print(err)
+        print(err, file=sys.stderr)
         usage()
 
     for opt, arg in opts:
@@ -36,14 +37,29 @@ def main():
         elif opt in ["-f", "--family"]:
             family = arg
         else:
-            assert False, "unhandled option"
+            raise RuntimeError("unhandled option " + opt)
 
     if not args:
         args = ['0']
 
     cmd = ['ip', '-f', family, '-j', 'route', 'list', 'table'] + args
-    process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
-    tbl = json.load(process.stdout)
+    try:
+        result = subprocess.run(cmd, stdout=subprocess.PIPE, check=True)
+    except FileNotFoundError:
+        print("{}: ip: command not found".format(sys.argv[0]),
+              file=sys.stderr)
+        sys.exit(1)
+    except subprocess.CalledProcessError as err:
+        # ip has already written its own diagnostic to stderr
+        sys.exit(err.returncode)
+
+    try:
+        tbl = json.loads(result.stdout)
+    except json.JSONDecodeError as err:
+        print("{}: cannot parse ip output: {}".format(sys.argv[0], err),
+              file=sys.stderr)
+        sys.exit(1)
+
     if family == 'inet':
         fmt = '{:15} {:15} {:15} {:8} {:8}{:<16} {}'
     else:
@@ -54,7 +70,7 @@ def main():
     print(fmt.format(*map(lambda x: x.capitalize(), keys)))
 
     for record in tbl:
-        fields = [record[k] if k in record else '' for k in keys]
+        fields = [record.get(k, '') for k in keys]
         print(fmt.format(*fields))
 
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-05-17 22:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-17 22:44 [PATCH iproute2-next] routel: handle errors from ip command Stephen Hemminger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox