Pexpect pxssh不终止以超级用户特权启动的程序

问题描述

背景

我正在使用pexpect库登录两台远程计算机。一个称为源,另一个称为目标。目标计算机用于运行程序,该程序处理在目标计算机的特定接口上接收到的UDP数据。源计算机用于通过特定接口将pcap文件(包含UDP数据)发送到目标计算机。我编写了一个python脚本,该脚本基本上可以自动执行此过程。该脚本可以正常运行,即源计算机发送数据,目标计算机上的程序接收数据并正确处理。我还添加了一个信号处理程序函数,该函数用于在用户提供SIGINT(Ctrl + C)信号时逐一停止所有进程(由python脚本启动)。这就是问题所在。

问题

要从源计算机发送pcap文件,请使用命令sudo tcpreplay-edit -i interface-name --pps=100 pcap-file.pcap tcpreplay-edit命令需要超级用户权限。目标计算机上的程序无需使用 sudo 即可运行,因为它不需要超级用户特权。当提供SIGINT信号并且python脚本移至信号处理函数时。在此功能中,我首先通过向目标计算机发送命令pkill -9 program-name来杀死目标计算机上的程序。这将停止目标计算机上的程序。之后,我通过发送命令sudo pkill -9 tcpreplay-edit来杀死 tcpreplay-edit 。执行此命令后,我使用命令ps -ef | grep tcpreplay-edit| grep -v "grep" | grep -v "sudo" | wc -l检查源计算机上是否还有任何 tcpreplay-edit 实例仍在运行。这将返回 0 ,这意味着没有运行tcpreplay-edit的实例,但是如果我使用 htop 在源计算机上进行检查,则表明 tcpreplay-edit 进程仍在运行。我无法弄清楚是什么原因阻止了 tcpreplay-edit 进程停止。我添加了用于杀死 tcpreplay-edit程序的那部分代码。

def signal_handler(sig,frame):
    source.sendline('ps -ef | grep tcpreplay-edit| grep -v "grep" | grep -v "sudo" | wc -l')
    no_of_instances = source.expect(["0","1",pexpect.EOF])
    print "Number of Tcpreplay instances running: " + str(no_of_instances)
    while no_of_instances != 0:
        source.sendline('sudo pkill -9 tcpreplay-edit')
        command_output = source.expect(["[sudo]",pexpect.EOF])
        if command_output == 0:  # send password
            time.sleep(2)
            source.sendline(source_password)
            time.sleep(2)
        time.sleep(1)
        source.sendline('ps -ef | grep tcpreplay-edit| grep -v "grep" | grep -v "sudo" | wc -l')
        time.sleep(2)
        no_of_instances = source.expect(["0",pexpect.EOF])
        print "Number of Tcpreplay instances running: " + str(no_of_instances)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...