WEEK9:Python 多线程、进程

  • 数据库操作与paramiko模块
    • 该模块基于ssh用于连接远程服务器并执行相关操作
      • SSHClient #用于连接远程服务器并执行基本命令
        • 基于用户名和密码连接
           1 #########执行命令
           2 import paramiko
           3 # 创建SSH对象
           4 ssh = paramiko.SSHClient()
           5 # 允许连接不在know_hosts文件中的主机
           6 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
           7 # 连接服务器
           8 ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', password='123')
           9 # 执行命令
          10 stdin, stdout, stderr = ssh.exec_command('df')
          11 # 获取命令结果
          12 result = stdout.read()
          13 # 关闭连接
          14 ssh.close()
          15 
          16 #########SSHClient封装Transport
          17 import paramiko
          18 transport = paramiko.Transport(('hostname', 22))
          19 transport.connect(username='wupeiqi', password='123')
          20 ssh = paramiko.SSHClient()
          21 ssh._transport = transport
          22 stdin, stdout, stderr = ssh.exec_command('df')
          23 print(stdout.read())
          24 transport.close()
        • 基于公钥密钥连接
           1 #########执行命令
           2 import paramiko
           3 private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
           4 # 创建SSH对象
           5 ssh = paramiko.SSHClient()
           6 # 允许连接不在know_hosts文件中的主机
           7 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
           8 # 连接服务器
           9 ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', key=private_key)
          10 # 执行命令
          11 stdin, stdout, stderr = ssh.exec_command('df')
          12 # 获取命令结果
          13 result = stdout.read()
          14 # 关闭连接
          15 ssh.close()
          16 
          17 #########SSHClient 封装 Transport
          18 import paramiko
          19 private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
          20 transport = paramiko.Transport(('hostname', 22))
          21 transport.connect(username='wupeiqi', pkey=private_key)
          22 ssh = paramiko.SSHClient()
          23 ssh._transport = transport
          24 stdin, stdout, stderr = ssh.exec_command('df')
          25 transport.close()
          26 
          27 #########基于私钥字符串进行连接
          28 import paramiko
          29 from io import StringIO
          30 
          31 key_str = """-----BEGIN RSA PRIVATE KEY-----
          32 MIIEpQIBAAKCAQEAq7gLsqYArAFco02/55IgNg0r7NXOtEM3qXpb/dabJ5Uyky/8
          33 NEHhFiQ7deHIRIuTW5Zb0kD6h6EBbVlUMBmwJrC2oSzySLU1w+ZNfH0PE6W6fans
          34 H80whhuc/YgP+fjiO+VR/gFcqib8Rll5UfYzf5H8uuOnDeIXGCVgyHQSmt8if1+e
          35 7hn1MVO1Lrm9Fco8ABI7dyv8/ZEwoSfh2C9rGYgA58LT1FkBRkOePbHD43xNfAYC
          36 tfLvz6LErMnwdOW4sNMEWWAWv1fsTB35PAm5CazfKzmam9n5IQXhmUNcNvmaZtvP
          37 c4f4g59mdsaWNtNaY96UjOfx83Om86gmdkKcnwIDAQABAoIBAQCnDBGFJuv8aA7A
          38 ZkBLe+GN815JtOyye7lIS1n2I7En3oImoUWNaJEYwwJ8+LmjxMwDCtAkR0XwbvY+
          39 c+nsKPEtkjb3sAu6I148RmwWsGncSRqUaJrljOypaW9dS+GO4Ujjz3/lw1lrxSUh
          40 IqVc0E7kyRW8kP3QCaNBwArYteHreZFFp6XmtKMtXaEA3saJYILxaaXlYkoRi4k8
          41 S2/K8aw3ZMR4tDCOfB4o47JaeiA/e185RK3A+mLn9xTDhTdZqTQpv17/YRPcgmwz
          42 zu30fhVXQT/SuI0sO+bzCO4YGoEwoBX718AWhdLJFoFq1B7k2ZEzXTAtjEXQEWm6
          43 01ndU/jhAasdfasdasdfasdfa3eraszxqwefasdfadasdffsFIfAsjQb4HdkmHuC
          44 OeJrJOd+CYvdEeqJJNnF6AbHyYHIECkj0Qq1kEfLOEsqzd5nDbtkKBte6M1trbjl
          45 HtJ2Yb8w6o/q/6Sbj7wf/cW3LIYEdeVCjScozVcQ9R83ea05J+QOAr4nAoGBAMaq
          46 UzLJfLNWZ5Qosmir2oHStFlZpxspax/ln7DlWLW4wPB4YJalSVovF2Buo8hr8X65
          47 lnPiE41M+G0Z7icEXiFyDBFDCtzx0x/RmaBokLathrFtI81UCx4gQPLaSVNMlvQA
          48 539GsubSrO4LpHRNGg/weZ6EqQOXvHvkUkm2bDDJAoGATytFNxen6GtC0ZT3SRQM
          49 WYfasdf3xbtuykmnluiofasd2sfmjnljkt7khghmghdasSDFGQfgaFoKfaawoYeH
          50 C2XasVUsVviBn8kPSLSVBPX4JUfQmA6h8HsajeVahxN1U9e0nYJ0sYDQFUMTS2t8
          51 RT57+WK/0ONwTWHdu+KnaJECgYEAid/ta8LQC3p82iNAZkpWlGDSD2yb/8rH8NQg
          52 9tjEryFwrbMtfX9qn+8srx06B796U3OjifstjJQNmVI0qNlsJpQK8fPwVxRxbJS/
          53 pMbNICrf3sUa4sZgDOFfkeuSlgACh4cVIozDXlR59Z8Y3CoiW0uObEgvMDIfenAj
          54 98pl3ZkCgYEAj/UCSni0dwX4pnKNPm6LUgiS7QvIgM3H9piyt8aipQuzBi5LUKWw
          55 DlQC4Zb73nHgdREtQYYXTu7p27Bl0Gizz1sW2eSgxFU8eTh+ucfVwOXKAXKU5SeI
          56 +MbuBfUYQ4if2N/BXn47+/ecf3A4KgB37Le5SbLDddwCNxGlBzbpBa0=
          57 -----END RSA PRIVATE KEY-----"""
          58 
          59 private_key = paramiko.RSAKey(file_obj=StringIO(key_str))
          60 transport = paramiko.Transport(('10.0.1.40', 22))
          61 transport.connect(username='wupeiqi', pkey=private_key)
          62 ssh = paramiko.SSHClient()
          63 ssh._transport = transport
          64 stdin, stdout, stderr = ssh.exec_command('df')
          65 result = stdout.read()
          66 transport.close()
          67 print(result)
      • SFTPClient #用于连接远程服务器并执行上传下载
        • 基于用户名和密码上传下载
          1 import paramiko
          2 transport = paramiko.Transport(('hostname',22))
          3 transport.connect(username='wupeiqi',password='123')
          4 sftp = paramiko.SFTPClient.from_transport(transport)
          5 # 将location.py 上传至服务器 /tmp/test.py
          6 sftp.put('/tmp/location.py', '/tmp/test.py')
          7 # 将remove_path 下载到本地 local_path
          8 sftp.get('remove_path', 'local_path')
          9 transport.close()
        • 基于公钥密钥上传下载
           1 import paramiko
           2 private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
           3 transport = paramiko.Transport(('hostname', 22))
           4 transport.connect(username='wupeiqi', pkey=private_key )
           5 sftp = paramiko.SFTPClient.from_transport(transport)
           6 # 将location.py 上传至服务器 /tmp/test.py
           7 sftp.put('/tmp/location.py', '/tmp/test.py')
           8 # 将remove_path 下载到本地 local_path
           9 sftp.get('remove_path', 'local_path')
          10 transport.close()

           

上一篇:IE浏览器,ajax提示错误“no transport”


下一篇:Cats Transport