From 7322a14274984cde34252fd62431f45f664a8352 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Sun, 12 Apr 2015 18:57:44 +0200 Subject: [PATCH] batman: prefix sudo for batctl if not executed as root depends on proper sudo rule, like: mapuser ALL = NOPASSWD: /usr/sbin/batctl --- lib/batman.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/batman.py b/lib/batman.py index 5c48740..17f4db3 100644 --- a/lib/batman.py +++ b/lib/batman.py @@ -1,5 +1,6 @@ import subprocess import json +import os import re @@ -46,8 +47,10 @@ class Batman(object): Parse "batctl -m gwl -n" into an array of dictionaries. """ - output = subprocess.check_output( - ['batctl', '-m', self.mesh_interface, 'gwl', '-n']) + cmd = ['batctl', '-m', self.mesh_interface, 'gwl', '-n'] + if os.geteuid() > 0: + cmd.insert(0, 'sudo') + output = subprocess.check_output(cmd) output_utf8 = output.decode('utf-8') rows = output_utf8.splitlines() @@ -73,8 +76,10 @@ class Batman(object): Parse "batctl -m gw" return: tuple mode, bandwidth, if mode != server then bandwidth is None """ - output = subprocess.check_output( - ['batctl', '-m', self.mesh_interface, 'gw']) + cmd = ['batctl', '-m', self.mesh_interface, 'gw'] + if os.geteuid() > 0: + cmd.insert(0, 'sudo') + output = subprocess.check_output(cmd) chunks = output.decode("utf-8").split() return chunks[0], chunks[3] if 3 in chunks else None