diff options
author | Viacheslav Hletenko <v.gletenko@vyos.io> | 2022-07-28 23:03:41 +0000 |
---|---|---|
committer | Viacheslav Hletenko <v.gletenko@vyos.io> | 2022-07-28 23:03:41 +0000 |
commit | 14418b6d80ef4652a44d9280baf369c8e3c429fd (patch) | |
tree | 59355848e24d6506b413f2b320e0ce0975e67e76 /python | |
parent | b33de0bac8172f3571ffc6d46b87de0c906e1b48 (diff) | |
download | vyos-1x-14418b6d80ef4652a44d9280baf369c8e3c429fd.tar.gz vyos-1x-14418b6d80ef4652a44d9280baf369c8e3c429fd.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.')
Diffstat (limited to 'python')
-rw-r--r-- | python/vyos/util.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py index bee5d7aec..b86b1949c 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'): """ |