简体   繁体   English

Paramiko 频道发送请求以远程启动跟踪

[英]Paramiko channel send request to start trace remotely

I am new to this concept so please help!我是这个概念的新手,所以请帮忙!

I am using Paramiko Channel to execute a command and start a trace.我正在使用 Paramiko Channel 执行命令并开始跟踪。 I want the trace to continue running until I send a stop request to trace.我希望跟踪继续运行,直到我发送停止请求进行跟踪。 If I use channel.send(cmd) it starts and stops the trace whereas I want that the trace should be continue and only stop when I send any stop request as I have other actions to perform before stopping the trace.如果我使用 channel.send(cmd) 它会启动和停止跟踪,而我希望跟踪应该继续并且仅在我发送任何停止请求时停止,因为在停止跟踪之前我还有其他操作要执行。

Note: I am doing SSH in one machine with Paramiko and starting a new channel in that machine to ssh into one more machine to start the trace there.注意:我在一台带有 Paramiko 的机器上做 SSH 并在该机器上启动一个新通道到 ssh 到另一台机器上开始跟踪。 I tried exec_command but it closes the socket.我尝试了 exec_command 但它关闭了套接字。

Thing I want to know:我想知道的事情:

  1. How can I start the trace and not let the command stop it before returning我怎样才能开始跟踪,而不是让命令在返回之前停止它
  2. How to stop the trace after I perform my actions after starting the trace.如何在开始跟踪后执行我的操作后停止跟踪。

The code I am using:我正在使用的代码:

    chan = sshClient.get_transport().open_session() 
    chan.get_pty()
    chan.invoke_shell()
    chan.send(cmdline +'\n')
    while not sshClient.recv_ready():
        print("Waiting")
    resp = chan.recv(9999)
    print(resp)

Thanks for your help in advance!提前感谢您的帮助!

The following example shows that in the while loop that goes for-ever you execute a command and then print the output.下面的示例显示,在永远运行的 while 循环中,您执行一个命令,然后打印 output。 After this is finished it sends again a command完成后,它再次发送命令

import base64
import paramiko
import time

key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
client = paramiko.SSHClient()
client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
client.connect('ssh.example.com', username='strongbad', password='thecheat')
while True: 
  time.sleep(5)
  stdin, stdout, stderr = client.exec_command('ls')
  for line in stdout:
     print('... ' + line.strip('\n'))
client.close()

It is possible that you need to send a newline \n after each command so that client.exec_command('ls') becomes -> client.exec_command('ls\n')您可能需要在每个命令之后发送换行符\n以便client.exec_command('ls')变为 -> client.exec_command('ls\n')

Though it's late, putting it across:虽然已经晚了,但还是把它放在一边:

exec_command will close the channel as soon as command is executed. exec_command将在命令执行后立即关闭通道。

You may try using something like:您可以尝试使用类似的东西:

# Get a client
chan = client.invoke_shell()
time.sleep(1) ## You may test with smaller time as well & see what fits best
chan.send('<command>\r')
output = chan.recv(4096)
print(output)
while <Event_is_not_yet_triggered> :
    time.sleep(10)
chan.send('\x03') # Send Ctrl+C
time.sleep(1)
output = chan.recv(4096)
client.close()

In the end it really depends on the server to which you are connected allows.最后,它实际上取决于您所连接的服务器允许。 Above will work with most of them.以上将适用于其中的大多数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM