温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python怎么封装SSHClient.py

发布时间:2021-12-30 17:01:53 来源:亿速云 阅读:260 作者:iii 栏目:编程语言

这篇文章主要讲解了“Python怎么封装SSHClient.py”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python怎么封装SSHClient.py”吧!

1、为了更方便采集信息系统以及数据库的信息,我做了该组件

2、为了让语句执行更顺畅,位置不发生错乱,暂时采用time.sleep()的方式解决

  1. #!/usr/bin/env python

  2. # coding:utf-8

  3. '''

  4. @author: Ryan Bai(白瑞钧)

  5. @license:

  6. @contact: brj880719@hotmail.com

  7. @file: SSHClient.py

  8. @create time: 2017/11/8 18:11

  9. @desc:

  10. '''

  11. import paramiko

  12. from paramiko.py3compat import u

  13. import time


  14. '''

  15. @attention: : ssh客户端使用

  16. @author: 白瑞钧

  17. @param date:

  18. '''

  19. class SSHClient(object):


  20.     '''

  21.     @attention: 关闭 ssh 链接

  22.     @author: 白瑞钧

  23.     @param ssh: ssh链接

  24.     '''

  25.     def close(self, ssh):

  26.         ssh.close()



  27.     '''

  28.     @attention: 创建 ssh 链接

  29.     @author: 白瑞钧

  30.     @param v_username: 用户名

  31.     @param v_password: 密码

  32.     @param v_ip: IP

  33.     @param v_port: 端口号

  34.     '''

  35.     def sshConnection(self, v_username, v_password, v_ip, v_port=22):

  36.         # 创建SSH对象

  37.         ssh = paramiko.SSHClient()


  38.         # 把要连接的机器添加到known_hosts文件中

  39.         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())


  40.         # 连接服务器

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


  42.         return ssh

  43.     # endregion



  44.     '''

  45.     @attention: 执行单条命令

  46.     @author: 白瑞钧

  47.     @param ssh: ssh链接

  48.     @param v_cmd: 需要执行的命令

  49.     '''

  50.     def sshExecByOne(self, ssh, v_cmd):

  51.         # 执行

  52.         stdin, stdout, stderr = ssh.exec_command(v_cmd)

  53.         result = stdout.read()

  54.         

  55.         if not result:

  56.             result = stderr.read()


  57.         return result.decode()



  58.     '''

  59.     @attention: 执行命令集

  60.     @author: 白瑞钧

  61.     @param ssh: ssh链接

  62.     @param l_cmd: 需要执行的命令集

  63.     @param exec_wait: 执行命令间隔时间

  64.     @param exit_wait: 退出等待时间

  65.     '''

  66.     def sshExecByMany(self, s, l_cmd, exec_wait, exit_wait):

  67.         ssh = s.invoke_shell()

  68.         # 执行

  69.         for v_cmd in l_cmd:

  70.             ssh.send(v_cmd)

  71.             ssh.send('\n')

  72.             time.sleep(exec_wait)

  73.             if v_cmd=='exit':

  74.                 time.sleep(exit_wait)


  75.         result = u(ssh.recv(9999))


  76.         return result



  77. if __name__ == '__main__':


  78.     getClient = SSHClient()

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

  80.     l_cmd = ['sudo su - ',

  81.              'su - oracle',

  82.             'sqlplus / as sysdba',

  83.             u'select * from dual;',

  84.             'exit',

  85.              'df -h',

  86.              'exit']

  87.     result = getClient.sshExecByMany(ssh, l_cmd, 1, 1)

  88.     print(result)

  89.     getClient.close(ssh)


  90.     # getClient = SSHClient()

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

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

  93.     # print(result)

  94.     # getClient.close(ssh)

感谢各位的阅读,以上就是“Python怎么封装SSHClient.py”的内容了,经过本文的学习后,相信大家对Python怎么封装SSHClient.py这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI