1

The following script unable to execute any output. I think there might be problem in - ssh.exec_command("iperf3 -c 192.168.234.2"). Can anyone help me to sort out the problem? While using iperf instead of iperf3 then it executes correctly.

import sys
import time
import select
import paramiko



class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.log = open("log.txt", "a")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

    def flush(self):
        #this flush method is needed for python 3 compatibility.
        #this handles the flush command by doing nothing.
        #you might want to specify some extra behavior here.
        pass

sys.stdout = Logger()
sys.stdout.write('.')
host = '169.254.115.1'
i = 1

#
# Try to connect to the host.
# Retry a few times if it fails.
#
while True:
    print ('Trying to connect to %s (%i/2)' % (host, i))

    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host, port=22, username='user', password='user')
        print ("Connected to %s" % host)
        break
    except paramiko.AuthenticationException:
        print ("Authentication failed when connecting to %s") % host
        sys.exit(1)
    except:
        print ("Could not SSH to %s, waiting for it to start" % host)
        i += 1
        time.sleep(2)

    # If we could not connect within time limit
    if i == 2:
        print ("Could not connect to %s. Giving up") % host
        sys.exit(1)

# Send the command (non-blocking)
stdin, stdout, stderr = ssh.exec_command("iperf3 -c 192.168.234.2")


# Wait for the command to terminate

while not stdout.channel.exit_status_ready():

    # Only print data if there is data to read in the channel
    if stdout.channel.recv_ready():
        rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
        if len(rl) > 0:
            # Print data from stdout
            print (stdout.channel.recv(1024)),
#
# Disconnect from the host
#
print ("Command done, closing SSH connection")
ssh.close()
  • It looks like you're reading the process's standard output. What about its standard error? Is it writing anything to standard error? – Kenster Jan 26 '17 at 21:46
  • Not really..There is no standard error..While using only iperf command, it gives the following output - .Trying to connect to 169.254.115.1 (1/2) Connected to 169.254.115.1 b'------------------------------------------------------------\nServer listening on TCP port 5001\nTCP window size: 85.3 KByte (default)\n------------------------------------------------------------\n' Command done, closing SSH connection – Abdullah Al Mamun Jan 27 '17 at 01:47

0 Answers0