python中想要實現sftp的話能夠使用paramiko模塊python
官網http://www.paramiko.org/ 其中文檔有很詳細的說明shell
paramiko很強大,經過python實現SSH協議。能夠作到ssh遠程登陸,sftp上傳下載文件。app
sftp的實現ssh
1.經過密碼口令認證ide
#!/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.經過密鑰認證
utf-8
#!/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實現更加方便rem