I have written scripts that can download files from regular FTP (unencrypted) sites using BASH and/or Python. This always left me wondering how would I do same thing if it was a SFTP (FTP via SSH) site. Since recently I bought couple of VPS (virtual private servers) so decided to set my own SFTP server and give this a shot. After lots of researching and reading I was able to download successfully from a SFTP site. I used Python’s Paramiko module to connect to server and retrieve files.
I have uploaded it to my github and sharing here as well. Feel free to modify and share it. Please let me know if something is not clear and I will be happy to assist.
#!/usr/bin/pythonimportdatetime,paramiko,os,zipfile#Formatting Date and Creating Directorytoday=datetime.date.today()-datetime.timedelta(days=3)formattedtime=today.strftime('%Y%m%d')destination='~/TestDir-%s'%formattedtimeifnotos.path.exists(destination):os.mkdir(destination)# This function downloads the file using Paramiko# and saves in specfied directorydeffile_download(hostname,username,port,password):# mykey = paramiko.RSAKey.from_private_key_file('~/My-ssh.priv') # This is when password less login is setuppassword=password# This is used when password is used to login host=hostnameusername=usernameport=porttransport=paramiko.Transport((host,port))# transport.connect(username = username, pkey = mykey) # This is when password less login is setuptransport.connect(username=username,password=password)# This is used when password is used to loginsftp=paramiko.SFTPClient.from_transport(transport)sftp.chdir('outgoing')forfilenameinsftp.listdir():try:iffilename.startswith('file_%s.csv'%formattedtime):localpath=destination+'/'+filenameprint"Downloading files ==> "+filenamesftp.get(filename,localpath)exceptIOErrorase:printesftp.close()transport.close()# This function calls the file_download function # and moves the files to required directory. If # using shutil.move() then it copies permissions # also which is not desirable always. defmain():try:file_download('www.blah.com','neo',22,'ilovematrix')currentfile=os.getcwd()+'/'+'file_%s.csv'%formattedtimeshutil.copy(currentfile,destination)os.remove(currentfile)exceptExceptionase:printeif__name__=='__main__':main()