Skip to content

Instantly share code, notes, and snippets.

@kdheepak
Created January 2, 2019 21:01
Show Gist options
  • Save kdheepak/c18f030494fea16ffd92d95c93a6d40d to your computer and use it in GitHub Desktop.
Save kdheepak/c18f030494fea16ffd92d95c93a6d40d to your computer and use it in GitHub Desktop.
paramiko non blocking example
import sys
import paramiko
import os
import select
import getpass
import time
rpi = {
"username": getpass.getuser(),
"hostname": "hostname.of.server.here"
}
command = "cd /path/to/folder/ && python test_worker.py"
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(**rpi)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(command)
def worker(stdout, stderr):
# Wait for the command to terminate
print("Status of worker is {}".format(stdout.channel.exit_status_ready()))
while not stdout.channel.exit_status_ready():
time.sleep(1)
print("Status of worker is {}".format(stdout.channel.exit_status_ready()))
if stdout.channel.recv_ready():
# Only print data if there is data to read in the channel
print("Worker stdout.channel.recv_ready: {}".format(stdout.channel.recv_ready()))
rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
if len(rl) > 0:
# Print data from stdout
print("Output: {}".format(stdout.channel.recv(1024).decode("utf-8")))
print("Reading output from master")
worker(ssh_stdout, ssh_stderr)
print("Finished reading output from master")
import time
import sys
i = 0
while True:
time.sleep(5)
i = i + 5
print("At time {}".format(i))
if i > 15:
sys.exit(0)
@rocojorge
Copy link

I have a question, if these code is non-blocking example, when you invoke a shell and use it with send() a recv() instead of exec_command and stdout, is the shell that I have generated a blocking socket or a non-blocking socket? I'm very lost right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment