无法使用 Paramiko 远程执行 tcpdump

Unable to execute tcpdump remotely with Paramiko

我正在尝试使用 Paramiko 远程 运行 tcpdump。

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip_machine, username='root', password=password)
    my_command = 'sudo timeout 10 tcpdump -i eth0 ip -w somefile'
    stdin, stdout, stderr = ssh.exec_command(my_command)
    channel = stdout.channel
    channel.recv_exit_status()
    ssh.close()

程序立即退出,尽管有 timeout 命令和 recv_exit_status。 tcpdump 的输出文件已创建,但它是空的。

在您的环境中,您无法通过 paramiko 连接 运行 sudo

既然您是以 root 的身份连接,您就没有理由 运行 sudo。试试这个:

my_command = 'timeout 10 tcpdump -i eth0 ip -w somefile'

如果您有权限并且应该使用带有 sudo 的命令并且没有安全问题,您可以配置 sudo 以在不需要 tty 的情况下执行命令。 在centos上配置文件是/etc/sudoers,注释下面几行

Defaults requiretty 替换为

#Defaults requiretty