@@ -100,17 +100,22 @@ def check_channel_forward_agent_request(self, channel):
100100 logger .debug ("Forward agent key request for channel %s" % (channel ,))
101101 return True
102102
103- def check_channel_exec_request (self , channel , cmd ):
103+ def check_channel_exec_request (self , channel , cmd ,
104+ encoding = 'utf-8' ):
104105 logger .debug ("Got exec request on channel %s for cmd %s" % (channel , cmd ,))
105106 self .event .set ()
106- process = gevent .subprocess .Popen (cmd , stdout = gevent .subprocess .PIPE , shell = True )
107+ _env = os .environ
108+ _env ['PYTHONIOENCODING' ] = encoding
109+ process = gevent .subprocess .Popen (cmd , stdout = gevent .subprocess .PIPE ,
110+ stdin = gevent .subprocess .PIPE ,
111+ shell = True , env = _env )
107112 gevent .spawn (self ._read_response , channel , process )
108113 return True
109114
110115 def _read_response (self , channel , process ):
111- for line in process .stdout :
112- channel . send ( line . decode ( 'ascii' ))
113- process . communicate ( )
116+ ( output , _ ) = process .communicate ()
117+ for line in output :
118+ channel . send ( line )
114119 channel .send_exit_status (process .returncode )
115120 logger .debug ("Command finished with return code %s" , process .returncode )
116121 # Let clients consume output from channel before closing
@@ -138,11 +143,13 @@ def listen(sock, fail_auth=False, ssh_exception=False,
138143 """
139144 listen_ip , listen_port = sock .getsockname ()
140145 if not sock :
141- logger .error ("Could not establish listening connection on %s:%s" , listen_ip , listen_port )
146+ logger .error ("Could not establish listening connection on %s:%s" ,
147+ listen_ip , listen_port )
142148 return
143149 try :
144150 sock .listen (100 )
145- logger .info ('Listening for connection on %s:%s..' , listen_ip , listen_port )
151+ logger .info ('Listening for connection on %s:%s..' , listen_ip ,
152+ listen_port )
146153 except Exception , e :
147154 logger .error ('*** Listen failed: %s' % (str (e ),))
148155 traceback .print_exc ()
@@ -179,7 +186,7 @@ def _handle_ssh_connection(transport, fail_auth=False,
179186 while not channel .send_ready ():
180187 gevent .sleep (.2 )
181188 channel .close ()
182-
189+
183190def handle_ssh_connection (sock ,
184191 fail_auth = False , ssh_exception = False ,
185192 timeout = None ):
0 commit comments