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.

1
  • Try using transport and set keep alive option. Using this you can keep pinging remote after given interval. For more details, please refer blog.rajnath.dev/ssh-python Commented Apr 11, 2021 at 10:48

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

how do I exit after the command is finished...the while True will just keep on reading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.