0

i'm trying to run a python script from host to some clients via subprocess.popen. The command is sort of fire and forget and the process in the clients should run for an unlimited time untill i kill it. the problem is - when i run this line in python the process run on the clients for an hour and then suddenly stops after 1 hour and 2 minutes :

subprocess.Popen(["rsh {} {} {}".format(ipClient,command,args)], shell=True)

where "command" is the path and the command in the clients. and when i simply run rsh 'ip' 'command' 'args' in the shell it works as expected and does not stop suddenly.

any idea?

Daniel
  • 42,087
  • 4
  • 55
  • 81
aa1331
  • 11
  • 2
  • Does not solve the problem, but you should use `subprocess.Popen(["rsh", ipClient, command, args])`. Does python do anything in parallel? – Daniel Aug 25 '15 at 07:00
  • can you provide the reported error? – amirouche Aug 25 '15 at 07:03
  • there is no reported error, the process in the client just stops – aa1331 Aug 25 '15 at 07:06
  • are you sure you do not use `stdout=PIPE`? What happens if you run a remote process that generates *a lot* of output? Have you tried to redirect stdin/stdout/stderr to DEVNULL? Drop unnecessary `shell=True`, use a list args instead. – jfs Aug 25 '15 at 07:13
  • thanks, trying now -will see in an hour if stops. – aa1331 Aug 25 '15 at 07:50
  • again stops after 1 hour and 2 minutes... what possibly could it be? – aa1331 Aug 25 '15 at 08:59
  • could you run `["ssh", host, "python -c 'for b in range(97, 127): print(chr(b)*10**7)'"]` and `["ssh", host, "python -c 'for b in range(97, 127): print(chr(b)); import time;time.sleep(300)'"]` to see whether it works in your environment? – jfs Aug 25 '15 at 12:35

1 Answers1

0

While subprocess.Popen might work for wrapping ssh access, this is not the preferred way to do so.

I recommend using paramiko.

import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(server, username=user,password=password)
...
ssh_client.close()

And If you want to simulate a terminal, as if a user was typing:

chan=self.ssh_client.invoke_shell()

def exec_cmd(cmd):
    """Gets ssh command(s), execute them, and returns the output"""
    prompt='bash $' # the command line prompt in the ssh terminal
    buff=''
    chan.send(str(cmd)+'\n')
    while not chan.recv_ready():
        time.sleep(1)
    while not buff.endswith(prompt):
        buff+=self.chan.recv(1024)
    return buff[:len(prompt)]

Example usage: exec_cmd('pwd')

If you don't know the prompt in advance, you can set it with:

chan.send('PS1="python-ssh:"\n')
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
  • as far as I know `paramiko` hangs if the output exceeds several MB. – jfs Aug 25 '15 at 07:15
  • Then it's probably a server-side disconnect, try this configuration : http://go2linux.garron.me/linux/2011/02/limit-idle-ssh-sessions-time-avoid-unattended-ones-clientaliveinterval-clientalivecoun/ – Uri Goren Aug 25 '15 at 11:04