@@ -100,17 +100,22 @@ def check_channel_forward_agent_request(self, channel):
100
100
logger .debug ("Forward agent key request for channel %s" % (channel ,))
101
101
return True
102
102
103
- def check_channel_exec_request (self , channel , cmd ):
103
+ def check_channel_exec_request (self , channel , cmd ,
104
+ encoding = 'utf-8' ):
104
105
logger .debug ("Got exec request on channel %s for cmd %s" % (channel , cmd ,))
105
106
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 )
107
112
gevent .spawn (self ._read_response , channel , process )
108
113
return True
109
114
110
115
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 )
114
119
channel .send_exit_status (process .returncode )
115
120
logger .debug ("Command finished with return code %s" , process .returncode )
116
121
# Let clients consume output from channel before closing
@@ -138,11 +143,13 @@ def listen(sock, fail_auth=False, ssh_exception=False,
138
143
"""
139
144
listen_ip , listen_port = sock .getsockname ()
140
145
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 )
142
148
return
143
149
try :
144
150
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 )
146
153
except Exception , e :
147
154
logger .error ('*** Listen failed: %s' % (str (e ),))
148
155
traceback .print_exc ()
@@ -179,7 +186,7 @@ def _handle_ssh_connection(transport, fail_auth=False,
179
186
while not channel .send_ready ():
180
187
gevent .sleep (.2 )
181
188
channel .close ()
182
-
189
+
183
190
def handle_ssh_connection (sock ,
184
191
fail_auth = False , ssh_exception = False ,
185
192
timeout = None ):
0 commit comments