Netdev List
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
	Christian Iacobellis <Ciacobellis@agea.com.ar>
Subject: [PATCH iproute2-next] routel: handle errors from ip command
Date: Sun, 17 May 2026 15:44:37 -0700	[thread overview]
Message-ID: <20260517224437.164919-1-stephen@networkplumber.org> (raw)

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


                 reply	other threads:[~2026-05-17 22:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260517224437.164919-1-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=Ciacobellis@agea.com.ar \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox