paramiko.proxy

Package paramiko :: Module proxy |
[frames] | no frames]

# Source Code for Module paramiko.proxy

     1  # Copyright (C) 2012  Yipit, Inc <coders@yipit.com>
     2  #
     3  # This file is part of paramiko.
     4  #
     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)
     8  # any later version.
     9  #
    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
    13  # details.
    14  #
    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.
    18
    19  """
    20  L{ProxyCommand}.
    21  """
    22
    23  import os
    24  from shlex import split as shlsplit
    25  import signal
    26  from subprocess import Popen, PIPE
    27
    28  from paramiko.ssh_exception import ProxyCommandFailure
    29
    30
    31 -class ProxyCommand(object):
    32      """
    33      Wraps a subprocess running ProxyCommand-driven programs.
    34
    35      This class implements a the socket-like interface needed by the
    36      L{Transport} and L{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.
    39      """
    40 -    def __init__(self, command_line):
    41          """
    42          Create a new CommandProxy instance. The instance created by this
    43          class can be passed as an argument to the L{Transport} class.
    44
    45          @param command_line: the command that should be executed and
    46              used as the proxy.
    47          @type command_line: str
    48          """
    49          self.cmd = shlsplit(command_line)
    50          self.process = Popen(self.cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    51
    52 -    def send(self, content):
    53          """
    54          Write the content received from the SSH client to the standard
    55          input of the forked command.
    56
    57          @param content: string to be sent to the forked command
    58          @type content: str
    59          """
    60          try:
    61              self.process.stdin.write(content)
    62          except IOError, e:
    63              # There was a problem with the child process. It probably
    64              # died and we can't proceed. The best option here is to
    65              # raise an exception informing the user that the informed
    66              # ProxyCommand is not working.
    67              raise BadProxyCommand(' '.join(self.cmd), e.strerror)
    68          return len(content)
    69
    70 -    def recv(self, size):
    71          """
    72          Read from the standard output of the forked program.
    73
    74          @param size: how many chars should be read
    75          @type size: int
    76
    77          @return: the length of the read content
    78          @rtype: int
    79          """
    80          try:
    81              return os.read(self.process.stdout.fileno(), size)
    82          except IOError, e:
    83              raise BadProxyCommand(' '.join(self.cmd), e.strerror)
    84
    85 -    def close(self):
    86          os.kill(self.process.pid, signal.SIGTERM)
    87
    88 -    def settimeout(self, timeout):
    89          # Timeouts are meaningless for this implementation, but are part of the
    90          # spec, so must be present.
    91          pass
    92