Changes to the Python SSH tool Paramiko

Source: Internet
Author: User
Tags ack sin stdin

Hundreds of devices are often managed using the Paramiko tool. The main is to fetch files from the top every day, as a backup.

Today we found that the program has been running for 10 hours before it is over, just go up and see what happened.


Check the logs and find out where the files are on one server. Manually ssh login up, executed a LS command to get stuck,

It turned out to be a problem with this server's hard drive. No wonder you can't get a file.


But think about it, the program should be in a period of time to read the data out of time to exit the AH, how can be stuck there. Find the part that executed the command.

Sin, sout, serr = Ssh.exec_command (' tar-zc0/data/important-file.txt ')

This statement is past, but behind

Sout.read (10240)

The statement stuck there all the time and stopped moving. It seems that sout is not set to timeout. Check the source code of Paramiko. Look, in the client.py.

def exec_command (self, Command, bufsize=-1)

Chan = Self._transport.open_session ()

Chan.exec_command (command)

stdin = Chan.makefile (' WB ', bufsize)

stdout = Chan.makefile (' RB ', bufsize)

stderr = Chan.makefile_stderr (' RB ', bufsize)

Return stdin, stdout, stderr


That stout is chan.makefile () out. Keep watching makefile.

def makefile (self, *params):

Return Channelfile (* ([Self] + list (params))

There is nothing to see here, focus on the Channelfile read method, the Read method is found. Channelfile inherited the Bufferedfile, as the document writes, this

Channelfile is a File-like object, so the _read method should be the actual read to call the method. Look at the code

def _read (self, size):

return Self.channel.recv (size)

Very simply, it is the Recv method of the channel (the channel is the first parameter of the Makefile, self, which is the Chan of Transport.open_session).

Or go back to the channel, recv method

def recv (self, nbytes):

Try

out = Self.in_buffer.read (nbytes, Self.timeout)

Except Pipetimeout, E:

Raise Socket.timeout ()


ACK = Self._check_add_window (len (out))

# No need to hold the channel lock when sending this

If ack > 0:

m = Message ()

M.add_byte (Chr (msg_channel_window_adjust))

M.add_int (Self.remote_chanid)

M.add_int (ACK)

Self.transport._send_user_message (M)


Return out

From the bold part can be seen, the original recv can be set to timeout, but Paramiko default is not set. OK, set the timeout to OK.

Modified the Paramiko code, run a bit, the program is not stuck in that place, in addition to the problem of the server, the rest are normally taken.

Modify the method as follows, modify the client.py Exec_command methods as follows

def exec_command (self, command, bufsize=-1, timeout = None):

Chan = Self._transport.open_session ()

If timeout is not None:

Chan.settimeout (Timeout)

Chan.exec_command (command)

stdin = Chan.makefile (' WB ', bufsize)

stdout = Chan.makefile (' RB ', bufsize)

stderr = Chan.makefile_stderr (' RB ', bufsize)

Return stdin, stdout, stderr

The blackbody portion is the added part. And in the place of the call, instead

Sin, sout, serr = Ssh.exec_command (' tar-zc0/data/important-file.txt ', timeout = 20.0)


Changes to the Python SSH tool Paramiko

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.