码迷,mamicode.com
首页 > 编程语言 > 详细

python paramiko利用sftb上传文件到远程

时间:2018-08-22 17:02:15      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:stderr   cal   send   toad   初始   hang   shc   from   aaa   

import paramiko
import os
class Linux(object):
  # 通过IP, 用户名,密码,超时时间初始化一个远程Linux主机
  def __init__(self, ip, username, password, timeout=30):
    self.ip = ip
    self.username = username
    self.password = password
    self.timeout = timeout
    # transport和chanel
    self.t = ‘‘
    self.chan = ‘‘
    # 链接失败的重试次数
    self.try_times = 3

  # 调用该方法连接远程主机
  def connect(self):
    pass

  # 断开连接
  def close(self):
    pass

  # 发送要执行的命令
  def send(self, cmd):
    pass

  # get单个文件
  def sftp_get(self, remotefile, localfile):
    t = paramiko.Transport(sock=(self.ip, 22))
    t.connect(username=self.username, password=self.password)
    sftp = paramiko.SFTPClient.from_transport(t)
    sftp.get(remotefile, localfile)
    t.close()

  # put单个文件
  def sftp_put(self, localfile, remotefile):
    t = paramiko.Transport(sock=(self.ip, 22))
    t.connect(username=self.username, password=self.password)
    sftp = paramiko.SFTPClient.from_transport(t)
    sftp.put(localfile, remotefile)
    t.close()


  # ------获取本地指定目录及其子目录下的所有文件------
  def __get_all_files_in_local_dir(self, local_dir):
    # 保存所有文件的列表
    all_files = list()
    # 获取当前指定目录下的所有目录及文件,包含属性值
    files = os.listdir(local_dir)
    for x in files:
      # local_dir目录中每一个文件或目录的完整路径
      filename = os.path.join(local_dir, x)
      # 如果是目录,则递归处理该目录
      if os.path.isdir(x):
        all_files.extend(self.__get_all_files_in_local_dir(filename))
      else:
        all_files.append(filename)
    return all_files

  def sftp_put_dir(self, local_dir, remote_dir):
    ssh = paramiko.SSHClient()

    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(self.ip, port=22, username=self.username, password=self.password)
    stdin, stdout, stderr = ssh.exec_command("cd /home/zmm/SNWA_space/ad_world/media;mkdir {}".format("aaa/bbb"))
    print stdout.readlines()
    ssh.close()
    t = paramiko.Transport(sock=(self.ip, 22))

    t.connect(username=self.username, password=self.password)
    ssh = paramiko.SSHClient()

    sftp = paramiko.SFTPClient.from_transport(t)
    # 去掉路径字符穿最后的字符‘/‘,如果有的话
    if remote_dir[-1] == ‘/‘:
      remote_dir = remote_dir[0:-1]
    # 获取本地指定目录及其子目录下的所有文件
    all_files = self.__get_all_files_in_local_dir(local_dir)
    # 依次put每一个文件
    for x in all_files:
      filename = os.path.split(x)[-1]
      remote_filename = remote_dir + ‘/‘ + filename
      print u‘Put文件%s传输中...‘ % filename
      sftp.put(x, remote_filename)

 

if __name__ == ‘__main__‘:
  remote_path = r‘/home/zmm/SNWA_space/‘
  local_path = r‘hitb-ams2018/data.yml‘

  host = Linux(‘192.168.1.201‘, ‘zmm‘, ‘zhangmiaomiao‘)

  # 将远端remote_path目录中的所有文件get到本地local_path目录
  # host.sftp_get_dir(remote_path, local_path)
  # # 将本地local_path目录中的所有文件put到远端remote_path目录
  host.sftp_put_dir(remote_path, local_path)

python paramiko利用sftb上传文件到远程

标签:stderr   cal   send   toad   初始   hang   shc   from   aaa   

原文地址:https://www.cnblogs.com/zamo-7/p/9517710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!