Example #1
0
 def do_makefile(*args):
     if ch.channel_file is None:
         ch.channel_file = Channel.makefile(ch, *args)
         _override_file_close(ch.channel_file)
         return ch.channel_file
     else:
         raise FileExistsError('We allow only one file per channel')
Example #2
0
def shell(channel: paramiko.Channel):
  stdin: paramiko.ChannelFile = channel.makefile_stdin("wb")
  stdout: paramiko.ChannelFile = channel.makefile("r")
  stderr: paramiko.ChannelFile = channel.makefile_stderr("r")
  print("Tip: F12 + I to show connection info, F12+C to close connection")

  stdoutReader = Thread(target=__buffered_reader, name="stdoutReader", args=(stdout, sys.stdout))
  stderrReader = Thread(target=__buffered_reader, name="stderrReader", args=(stderr, sys.stderr))
  stdinWriter = Thread(target=__input_handler, name="stdinWriter", args=(sys.stdin, stdin, channel))
  sizeHandler = Thread(target=__window_size_change_handler, name="TerminalSizeWatchdog", args=(channel,))

  sizeHandler.setDaemon(True)
  stdoutReader.setDaemon(True)
  stderrReader.setDaemon(True)
  stdinWriter.setDaemon(True)

  orig_sigint = signal.getsignal(signal.SIGINT)
  try:
    signal.signal(signal.SIGINT, _sigint)

    sizeHandler.start()
    stderrReader.start()
    stdoutReader.start()
    stdinWriter.start()

    stdoutReader.join()
  finally:
    print("Closing ssh session...")
    try:
      channel.close()
    except:
      pass
    signal.signal(signal.SIGINT, orig_sigint)
Example #3
0
def _override_makefile(ch: Channel):
    # makefile for channels should work the same way as for sockets, but that's
    # not the case. In particular, if you close the channel the file remains
    # unusable, which is bad.
    # When doing makefile on a channel, we store a pointer to the file, so that
    # when trying to close the channel we can check if the file is still open
    # and leave the closure pending. We override the close method in the file
    # as to execute the pending closure if needed.
    ch.channel_file = None

    def do_makefile(*args):
        if ch.channel_file is None:
            ch.channel_file = Channel.makefile(ch, *args)
            _override_file_close(ch.channel_file)
            return ch.channel_file
        else:
            raise FileExistsError('We allow only one file per channel')

    ch.makefile = do_makefile
Example #4
0
 def __init__(self, chan: Channel):
     self.chan = chan
     # need that U. no universal new line = pressing enter might not stop sending data..
     self.chanfile = chan.makefile('rU')