2016-12-03 1 views
0

Ich versuche, eine TCP-Verbindung von einem Python-Skript zu töten. tcpkill läuft, sondern gibt diese Nachricht auf beliebige Datenübertragung:tcpkill: schreiben: Operation nicht mit subprocess.Popen

tcpkill: write: Operation not permitted 
***.***.***.***:48868 > ***.***.***.***:6905: R 1868230658:1868230658(0) win 0 

Mein Python-Code ist:

@staticmethod 
    def kill_connection(ip, interface="eth0"): 
     def tcp_kill(ip, interface): 
      logging.info('Killing ' + ip + ' connection with tcpkill.') 
      p = subprocess.Popen('/sbin/tcpkill -i ' + interface + ' -9 ip host ' + ip, stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) 
      time.sleep(30) 
      p.terminate() 

     t = threading.Thread(target=tcp_kill, args=(ip, interface)) 
     t.daemon = True 
     t.start() 

Ich habe versucht, mit 'Shell' Option ist ein/aus, vor der Ausführung Funktion os Einstellung .setguid. Keine Chancen.

Das Ausführen von tcpkill funktioniert problemlos.

Was verursacht dies, und wie kann ich es richtig laufen lassen?

+0

führen Sie das Skript als root aus? –

+0

Ja, root führt das Skript – beremaran

+0

Bitte versuchen Sie, den tcpkill Befehl "von Hand" auszuführen, als root in Ihrer Bash Shell, und sehen Sie, ob das Ergebnis gleich –

Antwort

1

Sie sollten Ihren Befehl kill nehmen und ihn in ein Array statt in eine Zeichenfolge einfügen.

@staticmethod 
def kill_connection(ip, interface="eth0"): 
    def tcp_kill(ip, interface): 
     logging.info('Killing ' + ip + ' connection with tcpkill.') 
     p = subprocess.Popen(['/sbin/tcpkill', '-i ', interface, '-9', 'ip', 'host', ip], 
          stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) 
     time.sleep(30) 
     p.terminate() 

    t = threading.Thread(target=tcp_kill, args=(ip, interface)) 
    t.daemon = True 
    t.start()