From 9661da51e0500dfa1b92be77069841d09f6f6a84 Mon Sep 17 00:00:00 2001 From: derv82 Date: Sat, 10 Mar 2018 14:47:14 -0500 Subject: [PATCH] Restart NetworkManager if killed, using 'service' command. Previously only restarted network-manager if iface was put into monitor mode. Also tries systemctrl if 'service' fails. Should resolve #70 --- py/Aireplay.py | 4 +++- py/Airmon.py | 35 +++++++++++++++++++++++++++++++++-- py/Configuration.py | 3 +++ py/Process.py | 1 + 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/py/Aireplay.py b/py/Aireplay.py index 30b9959..6d5b799 100644 --- a/py/Aireplay.py +++ b/py/Aireplay.py @@ -394,7 +394,9 @@ if __name__ == '__main__': while aireplay.is_running(): from time import sleep sleep(0.1) - print aireplay.get_output() + stdout, stderr = aireplay.get_output() + print "STDOUT>", stdout + print "STDERR>", stderr ''' ''' diff --git a/py/Airmon.py b/py/Airmon.py index 794cdae..43f173f 100644 --- a/py/Airmon.py +++ b/py/Airmon.py @@ -13,6 +13,7 @@ import signal class Airmon(object): ''' Wrapper around the 'airmon-ng' program ''' base_interface = None + killed_network_manager = False def __init__(self): self.refresh() @@ -251,6 +252,8 @@ class Airmon(object): Color.pl('{!} {R}terminating {O}conflicting process' + ' {R}%s{O} (PID {R}%s{O})' % (pname, pid)) os.kill(int(pid), signal.SIGTERM) + if pname == 'NetworkManager': + Airmon.killed_network_manager= True @staticmethod def put_interface_up(iface): @@ -261,8 +264,36 @@ class Airmon(object): @staticmethod def start_network_manager(): Color.p("{!} {O}restarting {R}NetworkManager{O}...") - (out,err) = Process.call('systemctl start NetworkManager') - Color.pl(" {R}restarted{W}") + + if Process.exists('service'): + cmd = 'service network-manager start' + proc = Process(cmd) + (out, err) = proc.get_output() + if proc.poll() != 0: + Color.pl(" {R}Error executing {O}%s{W}" % cmd) + if out is not None and out.strip() != "": + Color.pl(" {O}STDOUT> %s{W}" % out) + if err is not None and err.strip() != "": + Color.pl(" {O}STDERR> %s{W}" % err) + else: + Color.pl(" {GR}restarted{W} ({C}%s{W})" % cmd) + return + + if Process.exists('systemctl'): + cmd = 'systemctl start NetworkManager' + proc = Process(cmd) + (out, err) = proc.get_output() + if proc.poll() != 0: + Color.pl(" {R}Error executing {O}%s{W}" % cmd) + if out is not None and out.strip() != "": + Color.pl(" {O}STDOUT> %s{W}" % out) + if err is not None and err.strip() != "": + Color.pl(" {O}STDERR> %s{W}" % err) + else: + Color.pl(" {GR}restarted{W} ({C}%s{W})" % cmd) + return + else: + Color.pl(" {R}can't restart NetworkManager: {O}systemctl{R} or {O}service{R} not found{W}") if __name__ == '__main__': Airmon.terminate_conflicting_processes() diff --git a/py/Configuration.py b/py/Configuration.py index 8d55dd1..c921029 100644 --- a/py/Configuration.py +++ b/py/Configuration.py @@ -322,7 +322,10 @@ class Configuration(object): if hasattr(Configuration, "interface") and Configuration.interface is not None and Airmon.base_interface is not None: Airmon.stop(Configuration.interface) Airmon.put_interface_up(Airmon.base_interface) + + if Airmon.killed_network_manager: Airmon.start_network_manager() + exit(code) @staticmethod diff --git a/py/Process.py b/py/Process.py index 51a2c67..ef78de1 100644 --- a/py/Process.py +++ b/py/Process.py @@ -114,6 +114,7 @@ class Process(object): self.pid.wait() if self.out is None: (self.out, self.err) = self.pid.communicate() + return (self.out, self.err) def poll(self): ''' Returns exit code if process is dead, otherwise "None" '''