【问题标题】:tcpkill: write: Operation not permitted with subprocess.Popentcpkill:写:不允许使用 subprocess.Popen 进行操作
【发布时间】:2017-04-18 11:29:52
【问题描述】:

我正在尝试从 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 可以正常工作。

是什么原因造成的,我怎样才能让它正常运行?

【问题讨论】:

  • 您是否以 root 身份运行脚本?
  • 是的,root 运行脚本
  • 请尝试在你的 bash shell 中以 root 身份“手动”运行 tcpkill 命令,看看结果是否相同
  • 手动运行效果很好,我忘了添加这个信息
  • 嗯...不知道,这并不明显。在 Linux 或 MacOS 上运行?

标签: python linux unix subprocess


【解决方案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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 2018-09-07
    • 2020-01-24
    相关资源
    最近更新 更多