#!/usr/bin/python
# coding=utf-8
import paramiko
import os
def sftp_upload(host, port, username, password, local, remote):
sf = paramiko.Transport((host, port))
sf.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(sf)
try:
if os.path.isdir(local): # 判断本地参数是目录还是文件
for f in os.listdir(local): # 遍历本地目录
sftp.put(os.path.join(local + f), os.path.join(remote + f)) # 上传目录中的文件
else:
sftp.put(local, remote) # 上传文件
except Exception as e:
print('upload exception:', e)
sf.close()
def sftp_download(host, port, username, password, local, remote):
sf = paramiko.Transport((host, port))
sf.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(sf)
try:
if os.path.isdir(local): # 判断本地参数是目录还是文件
for f in sftp.listdir(remote): # 遍历远程目录
python自动化运维-SFTP上传
最新推荐文章于 2024-05-06 23:09:46 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)