• python之旅十【第十篇】paramiko模块


    paramiko模块介绍

    ssh的远程连接

    基于用户名密码的连接

     1 import paramiko
     2   
     3 # 创建SSH对象
     4 ssh = paramiko.SSHClient()
     5 # 允许连接不在know_hosts文件中的主机
     6 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     7 # 连接服务器
     8 ssh.connect(hostname='x.x.x.x', port=22, username='xiaoli, password='123456')
     9   
    10 # 执行命令
    11 stdin, stdout, stderr = ssh.exec_command('df')
    12 # 获取命令结果
    13 result = stdout.read()
    14   
    15 # 关闭连接
    16 ssh.close()
    View Code

    基于封装的transport的连接

     1 import paramiko
     2 
     3 #1 创建transport对象
     4 transport = paramiko.Transport(('x.x.x.x', 22))
     5 transport.connect(username='xiaoli', password='123456')
     6 
     7 ssh = paramiko.SSHClient()
     8 ssh._transport = transport
     9 
    10 stdin, stdout, stderr = ssh.exec_command('df')
    11 print stdout.read()
    12 
    13 transport.close()
    View Code

    基于公钥密钥连接

     1 跟密码连接差不多,只是密码的地方变成了私钥
     2 
     3 import paramiko
     4  
     5 #私钥路径
     6 private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
     7  
     8 # 创建SSH对象
     9 ssh = paramiko.SSHClient()
    10 # 允许连接不在know_hosts文件中的主机
    11 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    12 # 连接服务器
    13 ssh.connect(hostname='x.x.x.x, port=22, username='xiaoli, key=private_key)
    14  
    15 # 执行命令
    16 stdin, stdout, stderr = ssh.exec_command('df')
    17 # 获取命令结果
    18 result = stdout.read()
    19  
    20 # 关闭连接
    21 ssh.close()
    View Code

    基于封装的transport的连接

     1 同理,跟上边的差不多
     2 
     3 import paramiko
     4 
     5 private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
     6 
     7 transport = paramiko.Transport(('x.x.x.x', 22))
     8 transport.connect(username='xiaoli', pkey=private_key)
     9 
    10 ssh = paramiko.SSHClient()
    11 ssh._transport = transport
    12 
    13 stdin, stdout, stderr = ssh.exec_command('df')
    14 
    15 transport.close()
    View Code

     SFTP的上传下载

    基于用户名密码的上传下载,只支持transport形式的连接然后实现上传下载

     1 简单小例
     2 
     3 import paramiko
     4  
     5 transport = paramiko.Transport(('x.x.x.x',22))
     6 transport.connect(username='xiaoli',password='123456')
     7  
     8 sftp = paramiko.SFTPClient.from_transport(transport)
     9 # 将location.py 上传至服务器 /tmp/test.py
    10 sftp.put('/tmp/location.py', '/tmp/test.py')
    11 # 将remove_path 下载到本地 local_path
    12 sftp.get('remove_path', 'local_path')
    13  
    14 transport.close()
    View Code

    基于密钥的上传下载

     1 import paramiko
     2  
     3 private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
     4  
     5 transport = paramiko.Transport(('x.x.x.x.', 22))
     6 transport.connect(username='xiaoli', pkey=private_key )
     7  
     8 sftp = paramiko.SFTPClient.from_transport(transport)
     9 # 将location.py 上传至服务器 /tmp/test.py
    10 sftp.put('/tmp/location.py', '/tmp/test.py')
    11 # 将remove_path 下载到本地 local_path
    12 sftp.get('remove_path', 'local_path')
    13  
    14 transport.close()
    View Code

    一个简单的上传下载,文件

     1 #!/usr/bin/env python
     2 #_*_coding:utf-8_*_
     3 
     4 #上传以及重命名
     5 import paramiko
     6 import uuid
     7 class Haproxy(object):
     8     def __init__(self):
     9         self.host = 'x.x.x.x.'
    10         self.port = 22
    11         self.username = 'root'
    12         self.pwd = '123456'
    13     def create_file(self):
    14         filename = str(uuid.uuid4())
    15         with open(filename,'w') as f:
    16             f.write('sb')
    17         return filename
    18     def connect(self):
    19         transport = paramiko.Transport((self.host,self.port))
    20         transport.connect(username=self.username,password=self.pwd)
    21         self.__transport = transport
    22     def close(self):
    23         self.__transport.close()
    24     def run(self):
    25         self.connect()
    26         self.upload()
    27         self.rename()
    28         self.close()
    29 
    30     def upload(self):
    31         file_name = self.create_file()
    32         sftp = paramiko.SFTPClient.from_transport(self.__transport)
    33         sftp.put(file_name,'/home/zdk/tttttt.py')
    34 
    35 
    36     def rename(self):
    37         ssh = paramiko.SSHClient()
    38         ssh._transport = self.__transport
    39         stdin, stdout ,stderr = ssh.exec_command('mv /home/zdk/tttttt.py /home/zdk/aaaaaaa.py')
    40         result =  stdout.read()
    41         ssh.close()
    42 a = Haproxy()
    43 a.run()
    View Code

     简单堡垒机实现

  • 相关阅读:
    HTML5游戏引擎Phaser初体验
    React+Node.js+Express+mongoskin+MongoDB
    React的一个简单示例
    在线白板,基于socket.io的多人在线协作工具
    使用node-webkit开发exe窗口程序
    使用Node.js的socket.io模块开发实时web程序
    dota BP练习工具开发:一个C/S多用户程序
    SQL注入之延迟盲注
    CTFHUB之gopher协议实现SSRF
    python的蟒蛇绘制
  • 原文地址:https://www.cnblogs.com/Dicky-Zhang/p/8017937.html
Copyright © 2020-2023  润新知