paramiko模块-远程连接

paramiko模块

  • paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作;
  • 值得一说的是,fabric和ansible内部的远程管理就是使用的paramiko来现实。
  • Copy from this BLOG

下载安装

pip3 install paramiko  # 在python3中

SSHClient

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

1-基于用户名密码连接

import paramiko


ssh = paramiko.SSHClient()	# 创建SSH对象

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 允许连接不在know_hosts文件中的主机

# 连接服务器
ssh.connect(hostname='120.92.84.249', 
            port=22, 
            username='root', 
            password='xxx')


stdin, stdout, stderr = ssh.exec_command('df') # 执行命令

result = stdout.read() # 获取命令结果
print(result.decode('utf-8'))

ssh.close() # 关闭连接

补充:SSHClient 封装 Transport

import paramiko

transport = paramiko.Transport(('120.92.84.249', 22))
transport.connect(username='root', password='xxx')

ssh = paramiko.SSHClient()
ssh._transport = transport

stdin, stdout, stderr = ssh.exec_command('df')
res=stdout.read()
print(res.decode('utf-8'))

transport.close()

2-基于公钥密钥连接

客户端文件名:id_rsa

服务端必须有文件名:authorized_keys(在用ssh-keygen时,必须制作一个authorized_keys,可以用ssh-copy-id来制作)

import paramiko

private_key = paramiko.RSAKey.from_private_key_file('/tmp/id_rsa')

# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='120.92.84.249', port=22, username='root', pkey=private_key)

# 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
result = stdout.read()
print(result.decode('utf-8'))
# 关闭连接
ssh.close()

SFTPClient

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

1-基于用户名密码上传下载

mport paramiko
 
transport = paramiko.Transport(('120.92.84.249',22))
transport.connect(username='root',password='xxx')
 
sftp = paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put('/tmp/location.py', '/etc/test.py')

# 将remove_path 下载到本地 local_path
sftp.get('remove_path', 'local_path')
 
transport.close()

2-基于公钥密钥上传下载

import paramiko

private_key = paramiko.RSAKey.from_private_key_file('/tmp/id_rsa')

transport = paramiko.Transport(('120.92.84.249', 22))
transport.connect(username='root', pkey=private_key )

sftp = paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put('/tmp/id_rsa', '/tmp/a.txt')

# 将remove_path 下载到本地 local_path
sftp.get('remove_path', 'local_path')

transport.close()

合并

import paramiko
import uuid


class Haproxy(object):

    def __init__(self):
        self.host = '172.16.103.191'
        self.port = 22
        self.username = 'root'
        self.pwd = '123'
        self.__k = None
        self.__transport = self.connect()

    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)
        return 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/root/tttttttttttt.py')

    def rename(self):

        ssh = paramiko.SSHClient()
        ssh._transport = self.__transport
        # 执行命令
        stdin, stdout, stderr = ssh.exec_command('mv /home/root/tttttttttttt.py /home/root/ooooooooo.py')
        # 获取命令结果
        result = stdout.read()


ha = Haproxy()
ha.run()
posted @ 2020-05-16 15:24  the3times  阅读(145)  评论(0编辑  收藏  举报