My l3g3nd

There is no spoon

SFTP File Download Script

| Comments

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python

import datetime, paramiko, os, zipfile

#Formatting Date and Creating Directory
today=datetime.date.today()-datetime.timedelta(days=3)
formattedtime = today.strftime('%Y%m%d')   
destination = '~/TestDir-%s'%formattedtime
if not os.path.exists(destination):
  os.mkdir(destination)

# This function downloads the file using Paramiko
# and saves in specfied directory

def file_download(hostname, username,port, password):
# mykey = paramiko.RSAKey.from_private_key_file('~/My-ssh.priv')  # This is when password less login is setup
  password = password                                             # This is used when password is used to login      
  host = hostname
  username = username
  port = port
  transport = paramiko.Transport((host, port))
# transport.connect(username = username, pkey = mykey)        # This is when password less login is setup
  transport.connect(username = username, password = password) # This is used when password is used to login
  sftp = paramiko.SFTPClient.from_transport(transport)
  sftp.chdir('outgoing')
  for filename in sftp.listdir():
      try:
          if filename.startswith('file_%s.csv'%formattedtime):
              localpath= destination + '/' + filename
              print "Downloading files ==> " + filename
              sftp.get(filename, localpath)   
      except IOError as e:
          print e
  sftp.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. 

def main():
  try:  
      file_download('www.blah.com', 'neo', 22, 'ilovematrix')
      currentfile = os.getcwd() + '/' + 'file_%s.csv'%formattedtime    
      shutil.copy(currentfile, destination)   
      os.remove(currentfile)
  except Exception as e:
      print e

if __name__ == '__main__':
  main()

Comments