Use paramiko control switch

Brief introduction

paramiko python is written in a module, follow SSH2 protocols that enable encryption and authentication way to connect remote server.

The use of the language of this python can run cross-platform, so the python all supported platforms, such as Linux,

Solaris, BSD, MacOS X,

Windows, etc., paramiko can support. Thus, if the need to use an SSH connection from one platform to another platform, a series of operations, one of the best tools paramiko.

In addition, due to the frequent switches support ssh, then use paramiko control switch into reality.

installation

pip installation

pip install paramiko

If you do not pycrypto installed, you need to install pycrypto library
use

paramiko provides a variety of connections, the connection switch using ssh manner and we send a command to achieve the purpose of controlling the switch.

control method

There are usually two control modes, use SSHClient and use terminal, these two differences are:

(1) Use SSHClient transmission command, ssh automatically closed channel

(2) use the terminal to send commands, ssh channel does not shut down automatically

Conventional control flow

Use paramiko control switch

SSHClient use process

Use paramiko control switch

Note: obtaining data exec_command return, you can get all-time

terminal use process

Use paramiko control switch

Note: The data obtained from the logic returns, you can only specify the amount of data received by recv, encountered a large amount of data when receiving a circular manner can be used while adding a timeout judgment, so that the terminal program is more complete, the code is as follows. :
Use paramiko control switch

Of course, you can control the added terminator encountered string received specific terminator, the receiver is circulated out.

important point

Control of the switch, if a logic contains an operation, the control can be used directly SSHClient; but in fact, the operation of the switch is often required to perform a series of commands, if you still use SSHClient will appeared to be inadequate, although SSHClient you can send multiple commands at one time, but at the same time send commands will cause some commands are executed successfully. Therefore, using the terminal to send commands, more appropriate, control it will be more convenient.
Use paramiko control switch

Use paramiko control switch
Articles from the public number: Rui Jiang cloud computing

Rui Jiang Yunguan website link: https://www.eflycloud.com/home?from=RJ0024

Guess you like

Origin blog.51cto.com/13475644/2401837