I have following code from here with little modification:
#!/usr/bin/env python
import paramiko
import select
server = "192.168.100.100"
port = 22
name = "root"
password = "pass"
def main():
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
client.connect(server, port, name, password)
channel = client.get_transport().open_session()
channel.exec_command("/tmp/test.sh")
while True:
if channel.exit_status_ready():
break
r, w, x = select.select([channel], [], [], 0.0)
if len(r) > 0:
print channel.recv(1024)
if __name__ == "__main__":
main()
Where test.sh has following content:
#!/usr/bin/env bash
while true
do
echo "Message"
sleep 1
done
So after executing python script CPU usage by script goes up to 100%. It means this select function does not wait until one or more file descriptors are ready for some kind of I/O. As far as knew this is busy loop problem where 'while ... loop' will iterate continuously even data for read is not presented. How I can make it asynchronous read of remote output?