In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.