我想tail -f logfile使用python的paramiko模块在远程机器上运行命令.到目前为止,我一直在尝试以下方式:
interface = paramiko.SSHClient()
#snip the connection setup portion
stdin, stdout, stderr = interface.exec_command("tail -f logfile")
#snip into threaded loop
print stdout.readline()
Run Code Online (Sandbox Code Playgroud)
我希望命令在必要时运行,但我有两个问题:
shutdown()在我通过时使用频道上的命令 - 但这看起来很混乱.有可能做一些像发送Ctrl-C到频道的标准输入的东西吗?readline() 块,如果我有一个非阻塞的输出方法,我可以避免线程 - 任何想法?我正在将Paramiko用于tail -f远程服务器上的文件.
以前,我们正在运行此通道ssh -t,但事实证明这种情况很脆弱,并且-t导致我们的远程调度系统出现问题.
我的问题是当脚本捕获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)
Run Code Online (Sandbox Code Playgroud)
该脚本Ctrl-C成功捕获了我的结尾.但是,它使tail -f进程在远程系统上运行.
client.close()和channel.close()似乎都没有终止它.
我可以在except块中发出什么命令来杀死它?
远程服务器正在运行Solaris 10.