| 144 | |
| 145 | |
| 146 | class RemoteProcess(object): |
| 147 | |
| 148 | def __init__(self, channel): |
| 149 | """This constructor should not be called from outside this module. The 'channel' |
| 150 | is created by the SSH client. |
| 151 | """ |
| 152 | self.channel = channel |
| 153 | self.stdout = channel.makefile("rb") |
| 154 | self.stderr = channel.makefile_stderr("rb") |
| 155 | |
| 156 | def poll(self): |
| 157 | """Returns the exit status of the process if the processes has completed, returns |
| 158 | None otherwise. |
| 159 | """ |
| 160 | if self.channel.exit_status_ready(): |
| 161 | return self.channel.recv_exit_status() |
| 162 | |
| 163 | def wait(self): |
| 164 | """Wait for the process to complete.""" |
| 165 | while self.poll() is None: |
| 166 | time.sleep(0.1) |
| 167 | |
| 168 | def communicate(self): |
| 169 | self.wait() |
| 170 | return self.stdout.read(), self.stderr.read() |
| 171 | |
| 172 | @property |
| 173 | def returncode(self): |
| 174 | return self.poll() |
| 175 | |
| 176 | def __del__(self): |
| 177 | self.channel.close() |