4

I'm looking for a way to execute a script on a remote server through paramiko and receive the output back as it is written to stdout. These scripts can run for an hour or so, but as the task is being executed, I want to retrieve the log messages printed out to stdout. How do I do this?

If it is not possible in paramiko, is there any other module which can help me do this.

abc def foo bar
  • 2,360
  • 6
  • 30
  • 41
  • Try using transport and set keep alive option. Using this you can keep pinging remote after given interval. For more details, please refer https://blog.rajnath.dev/ssh-python/ – Raj Apr 11 '21 at 10:48

1 Answers1

4

Get hold of the transport and generate your own channel. The channel can be used to execute a command, and you can use it in a select statement to find out when data can be read:

#!/usr/bin/env python
import paramiko
import select
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('host.example.com')
transport = client.get_transport()
channel = transport.open_session()
channel.exec_command(COMMAND)
while True:
  rl, wl, xl = select.select([channel],[],[],0.0)
  if len(rl) > 0:
      # Must be stdout
      print channel.recv(1024)

Source: https://stackoverflow.com/a/9470642/286937

Community
  • 1
  • 1
Mayur
  • 3,063
  • 10
  • 42
  • 55