python中想要实现sftp的话可以使用paramiko模块
paramiko很强大,通过python实现SSH协议。可以做到ssh远程登录,sftp上传下载文件。
sftp的实现
1.通过密码口令认证#!/usr/bin/python
# -*- coding:utf-8 -*-
import paramiko
remotedir = "/app/ftpuser/"
hostname = "192.168.1.100"
port = 22
user = "ftpuser"
password = "123456"
if __name__=="__main__":
t = paramiko.Transport((hostname,port))
t.connect(username=user,password=password)
sftp = paramiko.SFTPClient.from_transport(t)
print sftp.listdir(remotedir)
t.close
2.通过密钥认证#!/usr/bin/python
# -*- coding:utf-8 -*-
import paramiko
remotedir = "/app/ftpuser/"
hostname = "192.168.1.100"
port = 22
user = "ftpuser"
pkey_file = "/home/aa/.ssh/id_rsa"
if __name__=="__main__":
key = paramiko.RSAKey.from_private_key_file(pkey_file)
t = paramiko.Transport((hostname,port))
t.connect(username=user,pkey=key)
sftp = paramiko.SFTPClient.from_transport(t)
print sftp.listdir(remotedir)
t.close
看,用python实现sftp客户端访问的实现很简单。相比较shell脚本而言,在一些业务逻辑处理方面用python实现更加方便
原文地址:http://10452483.blog.51cto.com/10442483/1735593