tcpkill:write:subprocess.Popen

时间:2016-12-03 18:26:49

标签: python linux unix subprocess

我正在尝试从Python脚本中删除TCP连接。 tcpkill运行,但在任何数据传输时给出该消息:

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

我的Python代码是:

    @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()

我尝试使用'shell'选项打开/关闭,将预执行功能设置为os.setguid。没有机会。

手动运行tcpkill工作正常。

导致这种情况的原因是什么,以及如何让它正确运行?

1 个答案:

答案 0 :(得分:1)

您将需要使用kill命令并将其放入数组而不是字符串。

    @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()