summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorViacheslav Hletenko <v.gletenko@vyos.io>2022-07-28 23:03:41 +0000
committerViacheslav Hletenko <v.gletenko@vyos.io>2023-01-16 07:24:35 +0000
commita0b0670f46128b0be657e50b5751d23b5f602373 (patch)
tree3162826312e442daa5ef8190148dac1ea914b713 /python
parent598dcad7237d49861ca6216deea254b7c942241f (diff)
downloadvyos-1x-a0b0670f46128b0be657e50b5751d23b5f602373.tar.gz
vyos-1x-a0b0670f46128b0be657e50b5751d23b5f602373.zip
vyos.util: T4575: Add new wrapper "rc_cmd"
It is useful to have both a return code and output of the command Add a new wrapper "rc_cmd" that returns both % rc_cmd('uname') (0, 'Linux') % rc_cmd('ip link show dev fake') (1, 'Device "fake" does not exist.') (cherry picked from commit 14418b6d80ef4652a44d9280baf369c8e3c429fd)
Diffstat (limited to 'python')
-rw-r--r--python/vyos/util.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 67ec3ecc6..5c6409341 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -164,6 +164,27 @@ def cmd(command, flag='', shell=None, input=None, timeout=None, env=None,
return decoded
+def rc_cmd(command, flag='', shell=None, input=None, timeout=None, env=None,
+ stdout=PIPE, stderr=STDOUT, decode='utf-8'):
+ """
+ A wrapper around popen, which returns the return code
+ of a command and stdout
+
+ % rc_cmd('uname')
+ (0, 'Linux')
+ % rc_cmd('ip link show dev eth99')
+ (1, 'Device "eth99" does not exist.')
+ """
+ out, code = popen(
+ command, flag,
+ stdout=stdout, stderr=stderr,
+ input=input, timeout=timeout,
+ env=env, shell=shell,
+ decode=decode,
+ )
+ return code, out
+
+
def call(command, flag='', shell=None, input=None, timeout=None, env=None,
stdout=PIPE, stderr=PIPE, decode='utf-8'):
"""