1

I am preparing a code to access a remote server using ssh with Paramiko and to run some commands and see the output of stdout.

I have tested the code with a Ubuntu server and it worked perfectly, but when I tested the code with a different server (which is a Windows server and the interface to a telecommunication machine) the stdout is not read, The ("Successfully executed command on remote server") is printed but the following ("lines are read") is not printed So I concluded that the code is hanging at stdout=stdout.readlines() The code is copied below, can you please help me figuring out what could be the reason behind this failure?

I want also to add that if I use PuTTY to execute the commands on that server I get the correct output.

import paramiko
import os
user_name = "****"
passwd = "******"
ip = "*.*.*.*"

print ("Please wait creating ssh client ...")
ssh_client = paramiko.SSHClient()     #Create sshclient instance
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print ("Please wait, connecting to remote server")
ssh_client.connect(hostname=ip,username=user_name,password=passwd)
cmd="mml \n command2"
print ("Please wait, executing command on remote server")
stdin,stdout,stderr=ssh_client.exec_command(cmd)
print ("Successfully executed command on remote server")
stdout=stdout.readlines()
print ("lines are read")
stdout="".join(stdout)
ssh_client.close()
print ("Connection closed")
print (stdout)
os.system("pause")
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Y Hazi
  • 33
  • 4
  • Are you sure that no exception is thrown? + Do you get the problem with any command or with some specific commands only? + What kind of server is that? – Martin Prikryl Sep 16 '19 at 08:33
  • Hello, yes there is no exception just hanging without any output. This is a windows server communicating with another telecom machine via internal protocol, after login the first command is mml, (for man machine languague), then I can type some specific commands for this telecom provider. – Y Hazi Sep 16 '19 at 09:58

1 Answers1

0

The command that you are executing is interactive. When started, it waits for subcommands. While you execute it and wait for the command to finish (by calling readlines). What never happens.

You have to feed the subcommands to your command to have it do something.
See Pass input/variables to command/script over SSH using Python Paramiko.

You will also have to make the command quit (by sending a subcommand like exit/quit/bye).

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thank you for your feedback, do I have to add the subcommands in write.stdin: I have added the following lines: stdin.write("subcommand") and stdin.flush() before the stdout.stdout.readlines(), but still the same result,I feel like I have to read more about using paramiko but I don't know exactly which part I need to search. – Y Hazi Sep 16 '19 at 13:13
  • 1) You have missed the newline `+ '\n'`. 2) You have tell your command (`mml`) to close before you call `readlines`. Typically by sending `"exit\n"` or `"bye\n"` or `"quit\n"`. – Martin Prikryl Sep 16 '19 at 13:16
  • Now the stdout.readlines is executed, even if it gives empty output [] but I guess this is another problem, I believe is due to the location of the second ssh_client.exec_command("exit;") which erases the output of the mml command. – Y Hazi Sep 16 '19 at 13:54
  • I have added the modified code in my question above – Y Hazi Sep 16 '19 at 21:21
  • You have to send the `exit` to the `mml`, the same way you send the `command1`. – Martin Prikryl Sep 17 '19 at 05:20
  • Hi , I have executed the ("exit;") command using stdin.write() as suggested but then the code is hanging at the stdout.readlines() again. Then I tried to close the ssh_client before stdout.readlines() and this time the stdout.readlines() was executed but then print (stdout) is giving empty output [] – Y Hazi Sep 17 '19 at 09:08
  • Again, should there be the new-line? `"exit\n"` – Martin Prikryl Sep 17 '19 at 09:09
  • Yes, I have added the \n for a new line as follows: stdin.write("exit;\n") – Y Hazi Sep 17 '19 at 09:38
  • And does `exit` really work for `mml`? Did you test it? Also did you try using `readline` in a loop to see what output (or error) do you get back? – Martin Prikryl Sep 17 '19 at 09:45
  • Yes, the exit; commands works, I tested it using putty. I will try to use readline in a loop – Y Hazi Sep 17 '19 at 10:01
  • Hello I am close! I have removed the line: stdout=stdout.readline() and replaced it with a loop as follows: for line in stdout: print(stdout.readline()) and now it prints the output of the command but 1/2 the lines, I mean it prints the 1st 3rd 5th lines etc but not the 2nd 4th 6th lines – Y Hazi Sep 17 '19 at 10:35
  • Finally I got the desired output, I have simply used : for line in stdout: print (line), so I didn't read from stdout, a little weird but it worked :) Thanks a lot!! – Y Hazi Sep 17 '19 at 10:55
  • But your loop must still hang after the last line, doesn't it? – Martin Prikryl Sep 17 '19 at 11:47
  • Yes you are right, the last line of the output is : exit; I have added two prints after the print (line) but they are not printed , it seems that stdout is still waiting for another command... – Y Hazi Sep 17 '19 at 12:11