Set the command execution timeout value in the python paramiko Module

Source: Internet
Author: User
Tags ssh server

Paramiko is often used to manage hundreds of devices. However, due to the server itself or the network, sometimes the return value cannot be returned, and the program will wait there for a long time, at this time, you need to set a timeout value. The code for executing the command in the paramiko module is as follows:

Stdin, stdout, stderr = s.exe c_command (command)

There is only one parameter in the module. By default, paramiko does not set a timeout value.

In fact, paramiko itself can set the timeout value in this place, but this option is not available by default. You need to modify the source code of paramiko in the installation directory to support it, this interface is available in the code. The reason why he does not have this timeout value is that it takes a long time for the developer to execute some commands, such as compressing large files, if the timeout value is set, the command execution may be interrupted, leaving the interface without setting the timeout value. However, if we use this module to operate multiple devices in batches, sometimes the timeout value is necessary.

The method for modifying the paramiko source code is as follows:

Find the C: \ Python27 \ Lib \ site-packages \ paramiko directory. The following is a client. py file. Find this code in the file:

def exec_command(self, command, bufsize=-1):    """    Execute a command on the SSH server.  A new L{Channel} is opened and    the requested command is executed.  The command's input and output    streams are returned as python C{file}-like objects representing    stdin, stdout, and stderr.    @param command: the command to execute    @type command: str    @param bufsize: interpreted the same way as by the built-in C{file()} function in python    @type bufsize: int    @return: the stdin, stdout, and stderr of the executing command    @rtype: tuple(L{ChannelFile}, L{ChannelFile}, L{ChannelFile})    @raise SSHException: if the server fails to execute the command    """    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

To:

def exec_command(self, command, bufsize=-1,timeout = None):    """    Execute a command on the SSH server.  A new L{Channel} is opened and    the requested command is executed.  The command's input and output    streams are returned as python C{file}-like objects representing    stdin, stdout, and stderr.    @param command: the command to execute    @type command: str    @param bufsize: interpreted the same way as by the built-in C{file()} function in python    @type bufsize: int    @return: the stdin, stdout, and stderr of the executing command    @rtype: tuple(L{ChannelFile}, L{ChannelFile}, L{ChannelFile})    @raise SSHException: if the server fails to execute the command    """    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

It mainly changes two places:

1. Add a timeout = None when defining def exec_command (self, command, bufsize =-1, timeout = None;

2. Add a judgment under chan = self. _ transport. open_session ().

If timeout is not None:

Chan. settimeout (timeout)


The code for executing commands using the paramiko module is as follows:

Stdin, stdout, stderr = s.exe c_command (command, timeout = 10)

In this way, there is a timeout value. The timeout value for executing the command is 10 s.


This article from the "Wang Wei" blog, please be sure to keep this source http://wangwei007.blog.51cto.com/68019/1212492

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.