Skip to content
This repository has been archived by the owner on Jun 19, 2019. It is now read-only.

Commit

Permalink
- prefix batctl commands with sudo if run as nonroot
Browse files Browse the repository at this point in the history
  • Loading branch information
spoonicorn committed Mar 9, 2015
1 parent 786a58f commit 42cadee
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions batman.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import subprocess
import json
import re
import os

class batman:
""" Bindings for B.A.T.M.A.N. advanced batctl tool
Expand Down Expand Up @@ -42,10 +43,21 @@ def vis_data_batadv_vis(self):
lines = output.splitlines()
return self.vis_data_helper(lines)

def add_sudo_if_nonroot(self, command_array ):
""" Adds "sudo" to the command array if the calling user is not root (uid != 0)
"""
if os.getuid() != 0:
command_array.insert(0, "sudo")

return command_array

def gateway_list(self):
""" Parse "batctl -m <mesh_interface> gwl -n" into an array of dictionaries.
"""
output = subprocess.check_output(["batctl","-m",self.mesh_interface,"gwl","-n"])

batctl_command = self.add_sudo_if_nonroot(["batctl","-m",self.mesh_interface,"gwl","-n"])

output = subprocess.check_output(batctl_command)
output_utf8 = output.decode("utf-8")
# TODO Parse information
lines = output_utf8.splitlines()
Expand All @@ -71,7 +83,10 @@ def gateway_list(self):
def gateway_mode(self):
""" Parse "batctl -m <mesh_interface> gw"
"""
output = subprocess.check_output(["batctl","-m",self.mesh_interface,"gw"])

batctl_command = self.add_sudo_if_nonroot(["batctl","-m",self.mesh_interface,"gw"])

output = subprocess.check_output(batctl_command)
elements = output.decode("utf-8").split()
mode = elements[0]
if mode == "server":
Expand Down

0 comments on commit 42cadee

Please sign in to comment.