Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
53 lines (45 sloc) 1.76 KB
import datetime, paramiko, os, zipfile
#Formatting Date and Creating Directory
formattedtime = today.strftime('%Y%m%d')
destination = '~/TestDir-%s'%formattedtime
if not os.path.exists(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)
for filename in sftp.listdir():
if filename.startswith('file_%s.csv'%formattedtime):
localpath= os.getcwd() + '/' + filename
print "Downloading files ==> " + filename
sftp.get(filename, localpath)
except IOError as e:
print e
# 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():
file_download('', 'neo', 22, 'ilovematrix')
currentfile = os.getcwd() + '/' + 'file_%s.csv'%formattedtime
shutil.copy(currentfile, destination)
except Exception as e:
print e
if __name__ == '__main__':