Package libssh2 :: Module channel

[frames] | no frames]

# Source Code for Module libssh2.channel

      1  #
      2  # pylibssh2 - python bindings for libssh2 library
      3  #
      4  # Copyright (C) 2010 Wallix Inc.
      5  #
      6  # This library is free software; you can redistribute it and/or modify it
      7  # under the terms of the GNU Lesser General Public License as published by the
      8  # Free Software Foundation; either version 2.1 of the License, or (at your
      9  # option) any later version.
     10  #
     11  # This library is distributed in the hope that it will be useful, but WITHOUT
     12  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     13  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
     14  # details.
     15  #
     16  # You should have received a copy of the GNU Lesser General Public License
     17  # along with this library; if not, write to the Free Software Foundation, Inc.,
     18  # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     19  #
     20  """
     21  Abstraction for libssh2 L{Channel} object
     22  """
     23
     24 -class ChannelException(Exception):
     25      """
     26      Exception raised when L{Channel} actions fails.
     27      """
     28      pass
     29
     30 -class Channel(object):
     31      """
     32      Channel object
     33      """
     34 -    def __init__(self, _channel):
     35          """
     36          Creates a new channel object with the given _channel.
     37
     38          @param _channel: low level channel object
     39          @type _channel: L{_libssh2.Channel}
     40          """
     41          self._channel = _channel
     42          self.closed = False
     43          self.flushed = False
     44
     45 -    def close(self):
     46          """
     47          Closes the active channel.
     48
     49          @return: 0 on success or negative on failure
     50          @rtype: int
     51          """
     52          self.closed = True
     53          return self._channel.close()
     54
     55 -    def eof(self):
     56          """
     57          Checks if the remote host has sent a EOF status.
     58
     59          @return: 1 if the remote host has sent EOF otherwise 0
     60          @rtype: int
     61          """
     62          return self._channel.eof()
     63
     64 -    def execute(self, command):
     65          """
     66          Executes command on the channel.
     67
     68          @param command: message data
     69          @type command: str
     70
     71          @return: 0 on success or negative on failure
     72          @rtype: int
     73          """
     74          self.closed = True
     75          return self._channel.execute(command)
     76
     77 -    def exit_status(self):
     78          """
     79          Gets the exit code raised by the process running on the remote host.
     80
     81          @return: the exit status reported by remote host or 0 on failure
     82          @rtype: int
     83          """
     84          return self._channel.exit_status()
     85
     86 -    def flush(self):
     87          """
     88          Flushs the read buffer on the channel.
     89
     90          @return: 0 on sucess or negative on failure
     91          @rtype: int
     92          """
     93          self.flushed = True
     94          return self._channel.flush()
     95
     96 -    def poll(self, timeout, nfds):
     97          """
     98          Polls for activity on the channel.
     99
    100          @param timeout: remaining timeout
    101          @type timeout: int
    102          @param nfds: number of fds to poll
    103          @type nfds: int
    104
    105          @return: number of fds with interesting events or negative on failure
    106          @rtype: int
    107          """
    108          return self._channel.poll(timeout, nfds)
    109
    110 -    def poll_read(self, extended):
    111          """
    112          Checks if data is available on the channel.
    113
    114          @param extended: if message channel datas is extended
    115          @type extended: int
    116
    117          @return: 1 when data is available or 0 otherwise
    118          @rtype: int
    119          """
    120          return self._channel.poll_read(extended)
    121
    122 -    def pty(self, term="vt100"):
    123          """
    124          Requests a pty with term type on the channel.
    125
    126          @param term: terminal emulation type (vt100, ansi, etc...)
    127          @type term: str
    128
    129          @return: 0 on success or negative on failure
    130          @rtype: int
    131          """
    132          return self._channel.pty(term)
    133
    134 -    def pty_resize(self, width, height):
    135          """
    136          Requests a pty resize of the channel with given width and height.
    137
    138          @param width: terminal width
    139          @type width: int
    140          @param height: terminal height
    141          @type height: int
    142
    143          @return: 0 on success or negative on failure
    144          @rtype: int
    145          """
    146          return self._channel.pty_resize(width, height)
    147
    148 -    def read(self, size=4096):
    149          """
    150          Reads size bytes on the channel.
    151
    152          @param size: size of the buffer storage
    153          @type size: int
    154
    155          @return: bytes readed or negative on failure
    156          @rtype: str
    157          """
    158          return self._channel.read(size)
    159
    160 -    def send_eof(self):
    161          """
    162          Sends EOF status on the channel to remote server.
    163
    164          @return: 0 on success or negative on failure
    165          @rtype: int
    166          """
    167          self._channel.send_eof()
    168
    169 -    def setblocking(self, mode=1):
    170          """
    171          Sets blocking mode on the channel. Default mode is blocking.
    172
    173          @param mode: blocking (1) or non blocking (0) mode
    174          @rtype: int
    175          """
    176          return self._channel.setblocking(mode)
    177
    178 -    def setenv(self, name, value):
    179          """
    180          Sets envrionment variable on the channel.
    181
    182          @param name: envrionment variable name
    183          @type name: str
    184          @param value: envrionment variable value
    185          @type value: str
    186
    187          @return: 0 on success or negative on failure
    188          @rtype: int
    189          """
    190          return self._channel.setenv(name, value)
    191
    192 -    def shell(self):
    193          """
    194          Requests a shell on the channel.
    195
    196          @return: 0 on success or negative on failure
    197          @rtype: int
    198          """
    199          return self._channel.shell()
    200
    201 -    def window_read(self, read_avail, window_size_initial):
    202          """
    203          Checks the status of the read window on the channel.
    204
    205          @param read_avail: window limit to read
    206          @type read_avail: int
    207          @param window_size_initial: window initial size defined
    208          @type window_size_initial: int
    209
    210          @return: the number of bytes which the remote end may send
    211          without overflowing the window limit
    212          @rtype: int
    213          """
    214          return self._channel.window_read(read_avail, window_size_initial)
    215
    216 -    def write(self, message):
    217          """
    218          Writes data on the channel.
    219
    220          @param message: data to write
    221          @type message: str
    222
    223          @return: 0 on sucess or failure
    224          @rtype: int
    225          """
    226          return self._channel.write(message)
    227
    228 -    def x11_req(self, display):
    229          """
    230          Requests a X11 Forwarding on the channel.
    231
    232          @param display: screen number
    233          @param display: int
    234
    235          @return: 0 on success or negative on failure
    236          @rtype: int
    237          """
    238          return self._channel.x11_req(display)
    239