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 introduces the knowledge of "how to encapsulate remotely connected components in Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
#! / usr/bin/env python# coding:utf-8'''@author: Ryan Bai (Bai Ruijun) @ license:@contact: brj880719@hotmail.com@file: SSHClient.py@create time: 2017-11-8 18:11@attention: ssh client uses @ desc:'''import paramikofrom paramiko.py3compat import uimport timeclass SSHClient (object):''@ attention: close the ssh link @ author: Bai Ruijun @ param ssh: ssh link''def close (self Ssh): ssh.close ()''@ attention: create the 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 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): # execute 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 s: ssh link @ param l_cmd: command set to be executed @ param exec_wait: execute Command interval @ param exit_wait: exit wait time''def sshExecByMany (self) S, l_cmd, exec_wait Exit_wait): ssh = s.invoke_shell () # execute for v_cmd in l_cmd: ssh.send (v_cmd) ssh.send ('\ n') time.sleep (exec_wait) if vested cmdIn exitals: time.sleep (exit_wait) result = u ( Ssh.recv (9999) return resultif _ _ 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-hype, '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) "how Python encapsulates remotely connected components" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.