python used paramiko module and remote connection server to perform upload and download

paramiko module

paramiko python is written in a module, follow SSH2 protocols that enable encryption and authentication way to connect remote server.

Thus, if the need to use an SSH connection from one platform to another platform, a series of operations, one of the best tools paramiko.

By ssh linked server and execute the command you want, similar to XShell

ansible (remote batch management server) the underlying source code is actually implemented paramiko module

installation

pip3 install paramiko

use

Premise Notes: paramiko module that is supported by way of user name and password, and also supports public and private key of the server operating mode

1. User name Password ways:


# 执行命令

import paramiko
# 创建链接对象
ssh = paramiko.SSHClient()
# 允许链接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 链接服务器
ssh.connect(hostname='服务器ip地址',port='端口号',username='用户名',password='密码')

# 执行命令
stdin, stdout, stderr = ssh.exec_command('ip a')
"""
stdin  是用来输入额外的参数的   -y
stdout 命令的返回结果
stderr 错误的结果
"""
# 获取命令执行的结果
res = stdout.read()
print(res.decode('utf-8'))

# 关闭链接
ssh.close()

2. The public and private key way

First of all public and private key pair is generated on the windows

可以用openssl工具产生,也可以用Git Bash工具产生(需要安装git)
1.打开 Git Bash工具 输入命令 ssh-keygen
2.输入完命令一直按回车即可
3.在  c/user/用户名下的文件/.ssh 的目录中生成了两个密钥文件。id_rsa 为私钥,id_rsa.pub 为公钥
4.将公钥内容复制到你的linux服务器下的 /root/.ssh/authorized_keys文件中即可

Copy the private key file to the root directory of the project, I am here to rename a.txt


import paramiko

# 读取本地私钥
private_key = paramiko.RSAKey.from_private_key_file('a.txt')

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

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

Achieve upload and download files

1. The user name and password ways:

import paramiko

# 用户名和密码
transport = paramiko.Transport(('服务器ip地址', 22))
transport.connect(username='用户名', password='密码')

sftp = paramiko.SFTPClient.from_transport(transport)

# 上传文件
# sftp.put("a.txt", '/data/tmp.txt')  # 注意上传文件到远程某个文件下 文件必须存在

# 下载文件
sftp.get('/data/tmp.txt', 'hahaha.txt')  # 将远程文件下载到本地并重新命令
transport.close()

2. The public and private key ways:

import paramiko

private_key = paramiko.RSAKey.from_private_key_file('a.txt')

transport = paramiko.Transport(('服务器地址', 22))

transport.connect(username='用户名', pkey=private_key)

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

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

Run will be packaged and upload and download files to a single class

SSHProxy class package

If I wanted to link server executes three commands, and upload a file content

Most will operate on several servers linked several times, less efficient, code redundancy

We want to implement a class which includes the operation execution command and upload and download files

# 类的代码无需掌握 只需要会拷贝使用即可
import paramiko


class SSHProxy(object):
    # 这里的参数 你可以再加公钥私钥的形式
    def __init__(self, hostname, port, username, password):
        self.hostname = hostname
        self.port = port
        self.username = username
        self.password = password
        self.transport = None

    def open(self):  # 给对象赋值一个上传下载文件对象连接
        self.transport = paramiko.Transport((self.hostname, self.port))
        self.transport.connect(username=self.username, password=self.password)

    def command(self, cmd):  # 正常执行命令的连接  至此对象内容就既有执行命令的连接又有上传下载链接
        ssh = paramiko.SSHClient()
        ssh._transport = self.transport

        stdin, stdout, stderr = ssh.exec_command(cmd)
        result = stdout.read()
        return result

    def upload(self, local_path, remote_path):
        sftp = paramiko.SFTPClient.from_transport(self.transport)
        sftp.put(local_path, remote_path)
        sftp.close()

    def close(self):
        self.transport.close()


    def __enter__(self):  # 对象执行with上下文会自动触发
        self.open()
        return self  # 这里发挥上面with语法内的as后面拿到的就是什么 
    
    
    def __exit__(self, exc_type, exc_val, exc_tb):  # with执行结束自动触发
        self.close()
        

        

manual

"""

上述的封装操作在使用的使用 必须按照下面的顺序
obj = SSHProxy(...)

obj.open()  # 产生的对象必须要先执行open方法

obj.command('ls /')
obj.command('cat /data/tmp.txt')
obj.upload(...)
obj.upload(...)

obj.close()


利用文件操作的特性
f = open()
f.close()
嫌上述操作麻烦 利用with上下文做处理了
with open() as f:
    pass
    
as后面的值由__enter__方法返回值决定 返回什么就是什么


# 一旦对象被执行with会自动触发对象内部的__enter__方法  with结束之后还会自动触发__exit__方法

obj = SSHProxy(1,2,3,4)
with obj as f:
    pass
    

封装之后按照下面的方式使用即可
with SSHProxy(....) as obj:
    obj.command()
    obj.command()
    obj.upload()
    obj.upload()
    obj.command()
"""

Development: __enter__and __exit__use

class Foo(object):
    def __enter__(self):
        print('他进来了')
        return 123

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('他就这么出去了')

obj = Foo()
with obj as f:
    print(f)
    
"""
执行结果:
他进来了
123
他就这么出去了
"""

Summary: value back as the __enter__return value of the method decided to return to what is what,

Once an object is executed with automatically trigger an internal object __enter__will automatically triggered after the end of the method with __exit__method

Interview questions


# 面试题
"""
请在Context类中添加代码完成该类的实现(意思是怎么样修改才能实现以下代码并不会报错)
"""
class Context:
  pass
with Context() as ctx:
  ctx.do_something()

# 答案
class Context:
  def __enter__(self):
        return self
   
    def __exit__(self,*args,**kwargs):
        pass
  
  def do_something(self):
        pass

Guess you like

Origin www.cnblogs.com/guapitomjoy/p/12382084.html