#!/usr/bin/python """ Script to demonstrate how combination of subprocess & rsync causes a telnetlib connection to fail. David Antliff, Nov 2010 """ import telnetlib import subprocess import time # ******** change this to False to avoid the crash crash = True # Use Google's SMTP server as a demonstration host = "smtp.gmail.com" port = 25 # connect to a remote host with telnetlib t = telnetlib.Telnet() t.open(host, port) time.sleep(1) print("telnet", t.read_eager()) # THE FOLLOWING LINE DOES NOT SEEM TO CAUSE A PROBLEM process = subprocess.Popen(["ssh", "-V"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = process.communicate()[0] retcode = process.returncode print("ssh", retcode, output) # query the telnet server t.write("EHLO\r\n") time.sleep(1) print("telnet", t.read_eager()) if crash: # THE FOLLOWING LINE KILLS THE telnetlib SOCKET process = subprocess.Popen(["rsync", "--version"], stdout=subprocess.PIPE) output = process.communicate()[0] retcode = process.returncode print("rsync", retcode, output[0:60]) # this will now result in a crash because the connection is lost: print("telnet", t.read_eager()) t.write("QUIT\r\n") t.close()