zoukankan      html  css  js  c++  java
  • Python-09-paramiko模块

    开发堡垒机之前,先来学习一下Python的paramiko模块,该模块基于SSH用于连接远程服务器并执行相关操作。

    SSHClient

    用于连接远程服务器并执行基本命令

    基于用户名密码连接

    import paramiko
    
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    # 连接服务器
    ssh.connect(hostname='192.168.92.200', port=22, username='root', password='666')
    
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command('df')
    
    # 获取命令结果
    result = stdout.read()
    
    # 关闭连接
    ssh.close()
    

    SSHClient 封装 Transport

    import paramiko
    
    transport = paramiko.Transport(('hostname', 22))
    transport.connect(username='wupeiqi', password='123')
    
    ssh = paramiko.SSHClient()
    ssh._transport = transport
    
    stdin, stdout, stderr = ssh.exec_command('df')
    print stdout.read()
    
    transport.close()
    

    基于公钥密钥连接

    import paramiko
    
    private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
    
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    # 连接服务器
    ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', key=private_key)
    
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command('df')
    
    # 获取命令结果
    result = stdout.read()
    
    # 关闭连接
    ssh.close()
    

    SSHClient 封装 Transport

    import paramiko
    
    private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
    
    transport = paramiko.Transport(('hostname', 22))
    transport.connect(username='wupeiqi', pkey=private_key)
    
    ssh = paramiko.SSHClient()
    ssh._transport = transport
    
    stdin, stdout, stderr = ssh.exec_command('df')
    
    transport.close()
    

    SFTPClient

    用于连接远程服务器并执行上传下载

    基于用户名密码上传下载

    import paramiko
    
    transport = paramiko.Transport(('hostname',22))
    transport.connect(username='wupeiqi',password='123')
    
    sftp = paramiko.SFTPClient.from_transport(transport)
    
    # 将location.py 上传至服务器 /tmp/test.py
    sftp.put('/tmp/location.py', '/tmp/test.py')
    
    # 将remove_path 下载到本地 local_path
    sftp.get('remove_path', 'local_path')
    
    transport.close()
    

    基于公钥密钥上传下载

    import paramiko
    
    private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
    
    transport = paramiko.Transport(('hostname', 22))
    transport.connect(username='wupeiqi', pkey=private_key )
    
    sftp = paramiko.SFTPClient.from_transport(transport)
    
    # 将location.py 上传至服务器 /tmp/test.py
    sftp.put('/tmp/location.py', '/tmp/test.py')
    
    # 将remove_path 下载到本地 local_path
    sftp.get('remove_path', 'local_path')
    
    transport.close()
    

    Demo

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import paramiko
    import uuid
    
    class Haproxy(object):
    
        def __init__(self):
            self.host = '172.16.103.191'
            self.port = 22
            self.username = 'wupeiqi'
            self.pwd = '123'
            self.__k = None
    
        def create_file(self):
            file_name = str(uuid.uuid4())
            with open(file_name,'w') as f:
                f.write('sb')
            return file_name
    
        def run(self):
            self.connect()
            self.upload()
            self.rename()
            self.close()
    
        def connect(self):
            transport = paramiko.Transport((self.host,self.port))
            transport.connect(username=self.username,password=self.pwd)
            self.__transport = transport
    
        def close(self):
    
            self.__transport.close()
    
        def upload(self):
            # 连接,上传
            file_name = self.create_file()
    
            sftp = paramiko.SFTPClient.from_transport(self.__transport)
            # 将location.py 上传至服务器 /tmp/test.py
            sftp.put(file_name, '/home/wupeiqi/tttttttttttt.py')
    
        def rename(self):
    
            ssh = paramiko.SSHClient()
            ssh._transport = self.__transport
            # 执行命令
            stdin, stdout, stderr = ssh.exec_command('mv /home/wupeiqi/tttttttttttt.py /home/wupeiqi/ooooooooo.py')
            # 获取命令结果
            result = stdout.read()
    
    
    ha = Haproxy()
    ha.run()
    

    原文链接

  • 相关阅读:
    netcore保持活动状态超时设置
    使用 .net core 自定义项目模板
    Prometheus(普罗米修斯)——适合k8s和docker的监控系统
    移动端调试神器vConsole--腾讯的
    .netcore项目部署到linux的docker里后,速度异常的慢
    GIT删除本地tag和远程tag
    .netcore里使用StackExchange.Redis TimeOut 情况解决方法
    asp.netcore Log4Net连接kafka的方法
    asp.netcore 高并发下使用HttpClient的方法
    [离散时间信号处理学习笔记] 7. z变换
  • 原文地址:https://www.cnblogs.com/huyuedong/p/5883158.html
Copyright © 2011-2022 走看看