سؤال

I have been searching and fooling around with this problem for 2 days now. Firstly, some context in the form of (summarised) code.

def setService(self, ...
    ssh_client = self.conn.get_ssh_client(hostname, username=username, password=password)
    setCommand = str('service ' + service_name + ' ' + status)
    stdin, stdout, stderr = ssh_client.exec_command(setCommand)
    # time.sleep(2)
    return ...

Secondly. The whole codeset uses the same code, and everything works except for this "service foobar stop" and "service foobar start" command. It causes a Read Error (in ssh/auth.log) and does not actually effect the command. All other commands using this setup works fine (we do about 10 different commands). It happens on all target machines, from both dev machines, so I am ruling out ssh configs.

But, if I add any time delaying code after the exec_command(in the comment position), it works. A sleep(2), or a loop doing some debug printing, makes it work fine. Read Errors disappear from the auth.log and service start/stop as they should. Removing the sleep, or whatever it may be, breaks it again.

We "hack" fixed it by leaving a sleep in there, but I do not understand completely why it happens, or why stalling in the function fixes it.

Are we returning too quickly, before the exec was finished on the remote side? I do not think so, it seems to be blocking (returning into stdin, stderr, stdout).

Any advice on this would be highly appreciated.

هل كانت مفيدة؟

المحلول

Note: exec_command(command) is non-blocking..

I usually try to read the output from the buffer(which consumes some time - before returning), or I use a time.sleep which you've used in this case.

If you use(should) stdout.read()/readlines(), it forces your script to return the output in the stdout buffer, and in turn wait for exec_command to finish.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top