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

    简介:

    paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。跟常用软件xshell 、xftp功能一样,但是可以连接多台服务器,进行复杂的操作。

    安装 :

    pip install PyCrypto  (PyCrypto是使用Python编写的加密工具包)

    pip install paramiko /easy_install paramiko

    paramiko主要包含核心组件,一个是SSHClient类,另一个是SFTPClient类

    一、SSHClient类的主要函数:

    1、Connect方法

    Connect实现了远程SSH的连接并校验。

    Connect(self,hostname,port,username,password,pkey,key_filename,timeout,allow_agent,look_for_keys ,compress)

    参数含义:

    Hostname  str :主机ip

    Port  int :端口

    Username  str:用户名

    Password  str :密码

    Pkey  pkey: 秘钥

    Timeout  float: 超时时间

    Allow_agent  boool :当为flase时,禁用连到ssh代理

    Look_for_keys  bool : flase时,禁用在~/.ssh中搜索秘钥文件

    Compress    bool : true时打开压缩。

    2、exec_command方法

    远程命令执行的方法

    exec_command(self,command,bufsize=-1)

    参数:

    Command  str :命令串

    Bufsize  int:文件缓冲区大小,默认-1没有限制

    3、load_system_host_keys

    加载本地公秘钥校验文件,默认为~/.ssh/known_hosts

     load_system_host_keys(self,filename)

    fielname  str :制定远程主机公钥记录文件

    4、set_missing_host_key_policy

    连接主机没有本地主机秘钥或者HostKeys对象时策略,目前支持三种:AutoAddPolicy,RejectPolicy,WarningPolicy

    AutoAddPolicy:自动添加主机名以及主机秘钥

    RejectPolicy(默认):自动拒绝未知的主机名和秘钥

    WarningPolicy: 用于记录一个未知主机秘钥的Python警告

    ssh = paramiko.SSHClient()

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    5、简单案例

    # -*-coding:utf-8-*-

    import paramiko

    username = 'root'

    password = 'Aliyun6688xyz'

    hostname = '119.29.0.208'

    port = 22

    ssh = paramiko.SSHClient()

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

    ssh.connect(hostname,port,username, password)

    stdin, stdout, stderr = ssh.exec_command("uptime")

    print stdout.readlines()

    ssh.close()

    二、SFTPClient类——实现远程操作文件

    1、from_transport方法

    创建一个已经连通的sftp客户端通道

    2、put函数

    上传文件到服务器

    Put(self,localpath,remotepath,callback=none,confirm=none)

    Localpath  str : 上传文件本地源

    Remotepath  str : 远程路径

    Callback(function(int,int)) 获取已经接受的字节数以及总传输字节数。

    Confirm  bool : 文件上传完毕后是否调用start()函数,以便确认文件大小。

    3、get方法

    从远程主机端下载文件同put

    4、其他方法

    Mkdir 创建目录 sftp.mkdr(‘/home/user’,0755)

    Remove 删除主机端指定目录 sftp.remove(‘/home/user’)

    Rename 从命名服务端的文件或者目录 sftp.rename(“/home/test.sh”,”/home/newtest.sh”)

    Listdir  获取远程SFTP服务器端指定的目录列表,返回list形式  sftp.listdir(“/home”)

    Stat 获取远程主机指定文件信息 sftp.stat(“/home/test.sh”)

    5、简单测试代码

    t = paramiko.Transport((hostname,port))

    t.connect(username = username, password = password)

    sftp = paramiko.SFTPClient.from_transport(t)

    remotepath='/tmp/test.txt'

    localpath='/test.txt'

    sftp.get(localpath,remotepath)# get是获取函数,put函数是上传函数

    t.close()

  • 相关阅读:
    【leetcode】Binary Search Tree Iterator
    【leetcode】Palindrome Partitioning II
    【leetcode】Best Time to Buy and Sell Stock III
    【leetcode】Best Time to Buy and Sell Stock II
    【leetcode】Longest Consecutive Sequence
    【leetcode】Factorial Trailing Zeroes
    【leetcode】Simplify Path
    【leetcode】Generate Parentheses
    【leetcode】Combination Sum II
    【leetcode】Combination Sum
  • 原文地址:https://www.cnblogs.com/fuyuteng/p/8515273.html
Copyright © 2011-2022 走看看