Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Python encapsulate SSHClient.py

Shulou Source: shulou.com Published: 2022-06-02 04:43:05 11月14日 Update

This article mainly explains "how Python encapsulates SSHClient.py". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Python how to package SSHClient.py".

1. In order to make it more convenient to collect information from the information system and database, I have made this component.

2. In order to make the statement execution more smoothly and avoid misplacement, we temporarily adopt the method of time.sleep () to solve the problem.

#! / usr/bin/env python

# coding:utf-8

''

@ author: Ryan Bai (Bai Ruijun)

@ license:

@ contact: brj880719@hotmail.com

@ file: SSHClient.py

Create time: 18:11 on 2017-11-8

@ desc:

''

Import paramiko

From paramiko.py3compat import u

Import time

''

@ attention:: ssh client uses

@ author: Bai Ruijun

@ param date:

''

Class SSHClient (object):

''

@ attention: close the ssh link

@ author: Bai Ruijun

@ param ssh: ssh link

''

Def close (self, ssh):

Ssh.close ()

''

@ attention: create a ssh link

@ author: Bai Ruijun

@ param v_username: user name

@ param v_password: password

@ param v_ip: IP

@ param v_port: Port number

''

Def sshConnection (self, v_username, v_password, v_ip, v_port=22):

# create SSH object

Ssh = paramiko.SSHClient ()

# add the machine to be connected to the known_hosts file

Ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())

# Connect to the server

Ssh.connect (hostname=v_ip, port=v_port, username=v_username, password=v_password)

Return ssh

# endregion

''

@ attention: execute a single command

@ author: Bai Ruijun

@ param ssh: ssh link

@ param v_cmd: command to be executed

''

Def sshExecByOne (self, ssh, v_cmd):

# execution

Stdin, stdout, stderr = ssh.exec_command (v_cmd)

Result = stdout.read ()

If not result:

Result = stderr.read ()

Return result.decode ()

''

@ attention: execute command set

@ author: Bai Ruijun

@ param ssh: ssh link

@ param l_cmd: set of commands to be executed

@ param exec_wait: interval between execution of commands

@ param exit_wait: exit wait time

''

Def sshExecByMany (self, s, l_cmd, exec_wait, exit_wait):

Ssh = s.invoke_shell ()

# execution

For v_cmd in l_cmd:

Ssh.send (v_cmd)

Ssh.send ('\ n')

Time.sleep (exec_wait)

If vs. cmds. exitals:

Time.sleep (exit_wait)

Result = u (ssh.recv (9999)

Return result

If _ _ name__ = ='_ _ main__':

GetClient = SSHClient ()

Ssh = getClient.sshConnection ('sys_admin',' XSW@1qaz', '10.82.28.219')

L_cmd = ['sudo su -'

'su-oracle'

'sqlplus / as sysdba'

U'select * from dual;'

'exit'

'df-h'

'exit']

Result = getClient.sshExecByMany (ssh, l_cmd, 1,1)

Print (result)

GetClient.close (ssh)

# getClient = SSHClient ()

# ssh = getClient.sshConnection ('sys_admin',' XSW@1qaz', '10.82.28.219')

# result = getClient.sshExecByOne (ssh,'pwd')

# print (result)

# getClient.close (ssh)

Thank you for your reading, the above is the content of "how Python encapsulates SSHClient.py". After the study of this article, I believe you have a deeper understanding of how Python encapsulates SSHClient.py, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Commands links encapsulation learning information content time command set smooth confusion location slogans customers clients passwords objects that is ideas situations data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Xiaomi MariaDB vpn Shulou Information