0

all ,

Please help me with the following issue.

I have a linux device, to which I connect via SSH using PARAMIKO module in a python script.

The issue is that basically, if I send a command to the device, link "ping google.com" the output will be only shown after the command is finished ( in this case never) so I have to use "ping -n 2 google.com for example) and the output will be shown only after those 2 pings to google.com were completed.

The thing is that not all applications support a timeout parameter, and for example I am not able to use the "tail -f /file" because the tailing will never finish so the output will be never printed, nor to get other live traces which are partially printed by the device. The script is now used in this way.

elif action == "cmd":
            cmd = command
            (stdin, stdout, stderr) = ssh.exec_command(cmd)
            out = stdout.readline()
            if out:
                print(out.replace("â", "").replace("ââ", ""))     # only for case "systemctl --failed"
            else:
                print(stderr.read().decode('ISO-8859-1'))
        else:
            print("Your action: {} is invalid".format(action))
            sys.exit(11)

1 Answers1

-1

I'm a novice and unfortunately, I can't test this right now, but the output of the command should write to stdout even if the output takes some time. If you move on to the next command and read stdout before the device has been able to write to it then you won't see your output. if you put in a sleep command and then read the stdout, does it show the results of your ping?

from time import sleep
stdin, stdout, stderr = ssh.exec_command('ping 8.8.8.8')
sleep(5)
output = stdout.readlines()
print(output) 
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 20 '22 at 13:22
  • I believe OP wants to read the output continuously. Not wait for the command to finish and read the complete output. – Martin Prikryl Jan 20 '22 at 14:42