Paramiko 和 exec_command - 杀死远程进程? [英] Paramiko and exec_command - killing remote process?

查看:84
本文介绍了Paramiko 和 exec_command - 杀死远程进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Paramiko tail -f 远程服务器上的文件.

I'm using Paramiko to tail -f a file on a remote server.

以前,我们通过 ssh -t 运行它,但事实证明这是不稳定的,并且 -t 导致我们的远程调度系统出现问题.

Previously, we were running this via ssh -t, but that proved flaky, and the -t caused issues with our remote scheduling system.

我的问题是如何在脚本捕获到 SIGINT 时杀死 tail?

My question is how to kill tail when the script catches a SIGINT?

我的脚本(基于 python paramiko 模块中长时间运行的 ssh 命令(以及如何结束它们))

#!/usr/bin/env python2
import paramiko
import select

client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('someserver', username='victorhooi', password='blahblah')
transport = client.get_transport()
channel = transport.open_session()

channel.exec_command("tail -f /home/victorhooi/macbeth.txt")
while True:
    try:
        rl, wl, xl = select.select([channel],[],[],0.0)
        if len(rl) > 0:
            # Must be stdout
            print channel.recv(1024)
    except KeyboardInterrupt:
        print("Caught control-C")
        client.close()
        channel.close()
        exit(0)

脚本成功捕获了我的 Ctrl-C,然后结束.但是,它让 tail -f 进程在远程系统上运行.

The script catches my Ctrl-C successfully, and ends. However, it leaves the tail -f process running on the remote system,.

client.close() 和 channel.close() 似乎都没有终止它.

Neither client.close() nor channel.close() seem to terminate it.

我可以在 except 块中发出什么命令来杀死它?

What command can I issue in the except block to kill it?

远程服务器运行的是 Solaris 10.

The remote server is running Solaris 10.

推荐答案

虽然这不是最有效的方法,但应该有效.在你 CTRL+C 之后;在 KeyboardInterrupt 处理程序中,您可以 exec_command("killall -u %s tail" % uname) 像这样:

While not the most efficient method, this should work. After you CTRL+C; In the KeyboardInterrupt handler you could exec_command("killall -u %s tail" % uname) like so:

#!/usr/bin/env python2

import paramiko
import select

import time
ltime = time.time()

# Or use random:
# import random
# ltime = random.randint(0, 500)

uname = "victorhooi"
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('someserver', username=uname, password='blahblah')
transport = client.get_transport()
channel = transport.open_session()

channel.exec_command("tail -%df /home/victorhooi/macbeth.txt" % ltime)
while True:
    try:
        rl, wl, xl = select.select([channel],[],[],0.0)
        if len(rl) > 0:
            # Must be stdout
            print channel.recv(1024)
    except KeyboardInterrupt:
        print("Caught control-C")
        channel.close()
        try:
            # open new socket and kill the proc..
            client.get_transport().open_session().exec_command("kill -9 `ps -fu %s | grep 'tail -%df /home/victorhooi/macbeth.txt' | grep -v grep | awk '{print $2}'`" % (uname, ltime))
        except:
            pass
    
        client.close()
        exit(0)

这会杀死任何名为 tail 的开放进程.如果您不想关闭 tail 的打开,这可能会导致问题,如果是这种情况,您可以 grep 一个 ps,得到pid 和 kill -9 它.

This would kill any open processes named tail. That may cause issues though if you have tails open that you dont want to close, if thats the case you could grep a ps, get the pid and kill -9 it.

首先,在跟随之前设置tail从文件末尾读取n行.将 n 设置为像 time.time() 这样的唯一数字,因为 tail 不关心该数字是否大于文件中的行数,即大数from time.time() 应该不会引起问题并且是唯一的.然后在 ps:

First, set tail to read n lines from end of file before following. set n to a unique nuber like time.time(), since tail doesn't care if that number is larger then the number of lines in the file, the large number from time.time()shouldnt cause issues and will be unique. Then grep for that unique number in the ps:

   client.get_transport().open_session().exec_command("kill -9 `ps -fu %s | grep 'tail -%df /home/victorhooi/macbeth.txt' | grep -v grep | awk '{print $2}'`" % (uname, ltime))

这篇关于Paramiko 和 exec_command - 杀死远程进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆