zoukankan      html  css  js  c++  java
  • paramiko模块介绍

    paramiko模块
     1. 安装paramiko(windows和linux一样,执行执行即可)
      python3自带了pip3。直接使用pip3为python3安装paramiko。
      如果使用pip install则是安装到python 2.7上。
      pip3 install paramiko
      
     
     2.  SSHClient:
      用于连接远程服务器并执行基本命令:
      
      1>. 基于用户密码的连接:
      
      命令执行:
      
       import paramiko
       # 创建SSH对象
       ssh = paramiko.SSHClient()
       # 允许连接不在know_hosts文件中的主机
       ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
       # 连接服务器
       ssh.connect(hostname = "", port=22, username="", password="")
       # 执行命令, stdout, stderr不会同时有结果
       stdin, stdout, strerr = ssh.exec_command("df")
       # 获取命令结果
       result = stdout.read()
       print(result.decode())
       # 关闭连接
       ssh.close()
      
       三元输出:
       out, err = stdout.read(),stderr.read()
       res = out if out else err
       
      
      文件传输:
       import paramiko
       transport = paramiko.Transport(("hostname", "port"))
       transport.connect(username="", password="")
       
       sftp = paramiko.SSHClient.from_transport(transport)
       
       sftp.put("local file", "Destination")
       sftp.get("Destination", "local_file")
       transport.close()
      
      
      2>  基于密钥的方式连接:
       
       密钥:
       RSA  -非对称密钥验证;
       
       公钥: public key
       私钥:  private key
       
       computer1  连接    computer2
       192.168.137.10   =>     192.168.137.11
       私钥        公钥
       
       
       生成密码对:
       
        [brace@localhost ~]$ ssh-keygen
        Generating public/private rsa key pair.
        Enter file in which to save the key (/home/brace/.ssh/id_rsa):
        Enter passphrase (empty for no passphrase):
        Enter same passphrase again:
        Your identification has been saved in /home/brace/.ssh/id_rsa.
        Your public key has been saved in /home/brace/.ssh/id_rsa.pub.
        The key fingerprint is:
        SHA256:BjFv66HsycKgluMwQCYmPdaGLnLcgslc2MkoPQGMflI brace@localhost.localdomain
        The key's randomart image is:
        +---[RSA 2048]----+
        |+..   o          |
        |.+=E.  +         |
        |=*O=o . o        |
        |XO.*   o .       |
        |*oB .   S        |
        |oo.. . + .       |
        |o..o  o .        |
        |o=  oo .         |
        |o..  .+          |
        +----[SHA256]-----+
        [brace@localhost ~]$
       将公钥拷贝到远程服务器上 ssh-copy-id "root@192.168.137.11"
       
        [brace@localhost .ssh]$ ssh-copy-id -i id_rsa.pub brace@192.168.137.11
        /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
        The authenticity of host '192.168.137.11 (192.168.137.11)' can't be established.
        ECDSA key fingerprint is SHA256:w1CGC6ZjemS46PaMLtRzLFhE+Zw5Ckwv0PKcMYAOJis.
        ECDSA key fingerprint is MD5:51:07:dc:99:cc:65:75:59:d1:1d:d4:82:b1:6d:b8:82.
        Are you sure you want to continue connecting (yes/no)? yes
        /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
        /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
        brace@192.168.137.11's password:
        
        Number of key(s) added: 1
        
        Now try logging into the machine, with:   "ssh 'brace@192.168.137.11'"
        and check to make sure that only the key(s) you wanted were added.
       
       命令执行:
        import paramiko
        private_key =paramiko.RSAKey.from_private_key_file("/home/brace/.ssh/id_rsa")
        # 创建SSH对象
        ssh = paramiko.SSHClient()
        # 允许连接不在know_hosts文件中的主机
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        # 连接服务器
        ssh.connect(hostname="192.168.137.10", port=22, username="brace", pkey=private_key)
        #ssh.connect(hostname="192.168.137.10", port=22, username="brace", password="123456")
        # 执行命令,
        stdin, stdout, stderr = ssh.exec_command("df")
        # 获取命令结果
        result = stdout.read()
        print(result.decode())
        # 关闭连接
        ssh.close()
       
       
       文件传输:
        import paramiko
        
        private_key =paramiko.RSAKey.from_private_key_file("/home/brace/.ssh/id_rsa")
        
        transport = paramiko.Transport(("hostname", "port"))
        transport.connect(username="", pkey=private_key)
        
        sftp = paramiko.SSHClient.from_transport(transport)
        
        sftp.put("local file", "Destination")
        sftp.get("Destination", "local_file")
        transport.close()
  • 相关阅读:
    别再为了this发愁了------JS中的this机制
    offsetLeft,Left,clientLeft的区别
    下拉菜单的实现classList.add() classList.remove() class属性的添加和删除
    for循环
    html的基本数据类型(数字,字符串, 列表, 字典)
    定时器setInterval, innerText获取文本, charAt()获取单个字符串, substring(1, content.length)获取范围内的字符串, 实现字符串的滚动效果
    定时器 setInterval(‘function()’, 2000)
    parseInt 和 parseFloat 实现字符串转换为数字
    javarscript在HTML中的调用方式 (直接调用 和文件调用)
    input文本框 放上图片img 通过padding relative和absolute 的实现
  • 原文地址:https://www.cnblogs.com/brace2011/p/9291701.html
Copyright © 2011-2022 走看看