2012-11-05 20:25:03 -05:00
|
|
|
# Copyright (C) 2012 Yipit, Inc <coders@yipit.com>
|
|
|
|
#
|
2012-11-05 20:26:47 -05:00
|
|
|
# This file is part of paramiko.
|
2012-11-05 20:25:03 -05:00
|
|
|
#
|
|
|
|
# Paramiko is free software; you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
# Software Foundation; either version 2.1 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
2012-11-05 20:26:47 -05:00
|
|
|
# Paramiko is distrubuted in the hope that it will be useful, but WITHOUT ANY
|
2012-11-05 20:25:03 -05:00
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
2012-11-05 20:26:47 -05:00
|
|
|
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
|
|
|
|
|
|
|
"""
|
|
|
|
L{ProxyCommand}.
|
|
|
|
"""
|
2012-11-05 20:25:03 -05:00
|
|
|
|
|
|
|
import os
|
2013-10-30 19:19:30 -04:00
|
|
|
import sys
|
2012-11-05 20:25:03 -05:00
|
|
|
from shlex import split as shlsplit
|
2012-11-06 02:10:13 -05:00
|
|
|
import signal
|
2012-11-05 20:25:03 -05:00
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
|
2012-11-06 02:10:05 -05:00
|
|
|
from paramiko.ssh_exception import ProxyCommandFailure
|
2012-11-05 20:25:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
class ProxyCommand(object):
|
|
|
|
"""
|
2012-11-05 20:26:47 -05:00
|
|
|
Wraps a subprocess running ProxyCommand-driven programs.
|
2012-11-05 20:25:03 -05:00
|
|
|
|
2012-11-05 20:26:47 -05:00
|
|
|
This class implements a the socket-like interface needed by the
|
|
|
|
L{Transport} and L{Packetizer} classes. Using this class instead of a
|
|
|
|
regular socket makes it possible to talk with a Popen'd command that will
|
|
|
|
proxy traffic between the client and a server hosted in another machine.
|
2012-11-05 20:25:03 -05:00
|
|
|
"""
|
|
|
|
def __init__(self, command_line):
|
|
|
|
"""
|
|
|
|
Create a new CommandProxy instance. The instance created by this
|
2012-11-05 20:26:47 -05:00
|
|
|
class can be passed as an argument to the L{Transport} class.
|
2012-11-05 20:25:03 -05:00
|
|
|
|
|
|
|
@param command_line: the command that should be executed and
|
2012-11-05 20:26:47 -05:00
|
|
|
used as the proxy.
|
2012-11-05 20:25:03 -05:00
|
|
|
@type command_line: str
|
|
|
|
"""
|
|
|
|
self.cmd = shlsplit(command_line)
|
|
|
|
self.process = Popen(self.cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
|
|
|
|
|
|
|
|
def send(self, content):
|
|
|
|
"""
|
|
|
|
Write the content received from the SSH client to the standard
|
|
|
|
input of the forked command.
|
|
|
|
|
|
|
|
@param content: string to be sent to the forked command
|
|
|
|
@type content: str
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.process.stdin.write(content)
|
2013-10-30 19:46:33 -04:00
|
|
|
except IOError:
|
|
|
|
e = sys.exc_info()[1]
|
2012-11-05 20:25:03 -05:00
|
|
|
# There was a problem with the child process. It probably
|
|
|
|
# died and we can't proceed. The best option here is to
|
|
|
|
# raise an exception informing the user that the informed
|
|
|
|
# ProxyCommand is not working.
|
2013-10-30 19:46:33 -04:00
|
|
|
raise ProxyCommandFailure(' '.join(self.cmd), e.strerror)
|
2012-11-05 20:25:03 -05:00
|
|
|
return len(content)
|
|
|
|
|
|
|
|
def recv(self, size):
|
|
|
|
"""
|
2012-11-05 20:26:47 -05:00
|
|
|
Read from the standard output of the forked program.
|
2012-11-05 20:25:03 -05:00
|
|
|
|
|
|
|
@param size: how many chars should be read
|
|
|
|
@type size: int
|
|
|
|
|
2012-11-05 20:26:47 -05:00
|
|
|
@return: the length of the read content
|
2012-11-05 20:25:03 -05:00
|
|
|
@rtype: int
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return os.read(self.process.stdout.fileno(), size)
|
2013-10-30 19:46:33 -04:00
|
|
|
except IOError:
|
|
|
|
e = sys.exc_info()[1]
|
|
|
|
raise ProxyCommandFailure(' '.join(self.cmd), e.strerror)
|
2012-11-05 20:25:03 -05:00
|
|
|
|
|
|
|
def close(self):
|
2012-11-06 02:10:13 -05:00
|
|
|
os.kill(self.process.pid, signal.SIGTERM)
|
2012-11-05 20:25:03 -05:00
|
|
|
|
|
|
|
def settimeout(self, timeout):
|
2012-11-05 20:26:47 -05:00
|
|
|
# Timeouts are meaningless for this implementation, but are part of the
|
|
|
|
# spec, so must be present.
|
2012-11-05 20:25:03 -05:00
|
|
|
pass
|