librelist archives

« back to archive

Paramiko behavior after fork

Paramiko behavior after fork

From:
C. R. Oldham
Date:
2014-08-14 @ 22:14
Greetings,

I’m having trouble using paramiko after forking a new process. The actual 
application that I’m working with is very large, but I think I boiled this
down to a simple test case, code below.

When I run this the child process sticks after printing “I am the 
child–exec” whether or not I include the atfork() call.

What am I missing?

  
import sys, os, paramiko
import logging
import time
logging.basicConfig(level=logging.INFO)

  
if len(sys.argv) < 4:
    print "args missing"
    sys.exit(1)
  
hostname = sys.argv[1]
password = sys.argv[2]
command = sys.argv[3]
  
username = "testuser"
port = 22
  
try:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.WarningPolicy)
    client.connect(hostname, port=port, username=username, password=password)
     

    pid = os.fork()
    if pid == 0:

        time.sleep(5)
        print "I am the child--exec"
        stdin, stdout, stderr = client.exec_command(command)
        print "I am the child--done"
        print stdout.read(),
    else:
           client.get_transport.atfork()
  
finally:
    client.close()

Paramiko behavior after fork

From:
C. R. Oldham
Date:
2014-08-14 @ 23:28
could not decode message