Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does paramiko Channel.recv() exactly work?

I'm having a hard time understanding how the recv() function works.

http://docs.paramiko.org/en/1.13/api/channel.html#paramiko.channel.Channel.recv

I understand this is receiving a chunk a data each time you call the function, but can someone elaborate on the structure or size of this data? Lets say I send a command date, I notice:

  • 1st read gets: "date"
  • 2nd read gets: actual response (Mon Jun 9 12:04:17 CDT 2014)
  • 3rd read gets: prompt

But how does this handle debugging messages that appear randomly on the terminal?

Does the previous pattern hold true as long as the actual response is less than maximum bytes (nbytes)?

What happens if it exceeds nbytes?

As per request, I've added a snippet of the code below:

while reads<maxReads:
   resp = self.__chan.recv(maxBytes)
   print resp
   self.__buffer += resp
   if resp.endswith('$ ') or resp.endswith('# '):
      break
   reads += 1
like image 780
user3388884 Avatar asked Jun 09 '14 17:06

user3388884


1 Answers

Channel recv() corresponds to a socket.recv(), it does not have any specific structure or size, it just reads whatever data was sent from the remote server, not exceeding maxBytes.

You commonly use recv() in a loop until you get a piece of data that you are waiting for:

def _wait_for_data(self, options, verbose=False):
    chan = self.chan
    data = ""
    while True:
        x = chan.recv(1024)
        if len(x) == 0:
            self.log("*** Connection terminated\r")
            sys.exit(3)
        data += x
        if verbose:
            sys.stdout.write(x)
            sys.stdout.flush()
        for i in range(len(options)):
            if re.search(options[i], data):
                return i
    return -1
like image 97
João Pinto Avatar answered Oct 20 '22 05:10

João Pinto