1
# Copyright (C) 2012 Yipit, Inc <coders@yipit.com>
3
# This file is part of paramiko.
5
# Paramiko is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU Lesser General Public License as published by the Free
7
# Software Foundation; either version 2.1 of the License, or (at your option)
10
# Paramiko is distrubuted in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15
# You should have received a copy of the GNU Lesser General Public License
16
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
17
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21
from shlex import split as shlsplit
23
from select import select
27
from paramiko.ssh_exception import ProxyCommandFailure
28
from paramiko.util import ClosingContextManager
31
class ProxyCommand(ClosingContextManager):
33
Wraps a subprocess running ProxyCommand-driven programs.
35
This class implements a the socket-like interface needed by the
36
`.Transport` and `.Packetizer` classes. Using this class instead of a
37
regular socket makes it possible to talk with a Popen'd command that will
38
proxy traffic between the client and a server hosted in another machine.
40
Instances of this class may be used as context managers.
42
def __init__(self, command_line):
44
Create a new CommandProxy instance. The instance created by this
45
class can be passed as an argument to the `.Transport` class.
47
:param str command_line:
48
the command that should be executed and used as the proxy.
50
# NOTE: subprocess import done lazily so platforms without it (e.g.
51
# GAE) can still import us during overall Paramiko load.
52
from subprocess import Popen, PIPE
53
self.cmd = shlsplit(command_line)
54
self.process = Popen(self.cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE,
58
def send(self, content):
60
Write the content received from the SSH client to the standard
61
input of the forked command.
63
:param str content: string to be sent to the forked command
66
self.process.stdin.write(content)
68
# There was a problem with the child process. It probably
69
# died and we can't proceed. The best option here is to
70
# raise an exception informing the user that the informed
71
# ProxyCommand is not working.
72
raise ProxyCommandFailure(' '.join(self.cmd), e.strerror)
77
Read from the standard output of the forked program.
79
:param int size: how many chars should be read
81
:return: the string of bytes read, which may be shorter than requested
86
while len(buffer) < size:
88
if self.timeout is not None:
89
elapsed = (time.time() - start)
90
if elapsed >= self.timeout:
91
raise socket.timeout()
92
select_timeout = self.timeout - elapsed
95
[self.process.stdout], [], [], select_timeout)
96
if r and r[0] == self.process.stdout:
98
self.process.stdout.fileno(), size - len(buffer))
100
except socket.timeout:
102
# Don't raise socket.timeout, return partial result instead
104
raise # socket.timeout is a subclass of IOError
106
raise ProxyCommandFailure(' '.join(self.cmd), e.strerror)
109
os.kill(self.process.pid, signal.SIGTERM)
113
return self.process.returncode is not None
117
# Concession to Python 3 socket-like API
120
def settimeout(self, timeout):
121
self.timeout = timeout