Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # --------------------------------------------------------------------
  4. #import libraries
  5. # --------------------------------------------------------------------
  6. import paramiko as PM
  7. import os
  8. import datetime
  9.  
  10. # --------------------------------------------------------------------
  11. # Global Variables
  12. # --------------------------------------------------------------------
  13.  
  14. host = 'host IP address'
  15. port = 22
  16. username = 'Username'
  17. password = '*********'
  18.  
  19. # Variable Paths
  20.  
  21. localPath = '/shares/MILKLINK/fromML'
  22. remotePath = '/'
  23. logPath = '/shares/MILKLINK/logs/PPcfg02.log'
  24. SRCfiles = '/shares/MILKLINK/Milklink.cpy'
  25.  
  26. # --------------------------------------------------------------------
  27. # Create LOG FILE
  28. # --------------------------------------------------------------------
  29.  
  30. log = open(logPath, 'a')
  31. log.write(datetime.datetime.now().isoformat()+'n')
  32.  
  33. # Creating lockfile
  34.  
  35. if(os.path.isfile('LockSFTP')):
  36. log.write("LOCK FILE STILL EXISTS!")
  37. quit()
  38. else:
  39. os.system(">LockSFTP")
  40.  
  41. # --------------------------------------------------------------------
  42. # Remove all files from /formML/
  43. # --------------------------------------------------------------------
  44.  
  45. fileList = os.listdir(localPath)
  46. for fileName in fileList:
  47. try:
  48. os.remove(localPath+"/"+fileName)
  49. except OSError:
  50. log.write("%s could not be deletedn" % fileName)
  51.  
  52. # --------------------------------------------------------------------
  53. # Create SFTP CONNECTION
  54. # --------------------------------------------------------------------
  55.  
  56. log.write("Starting Connection...n")
  57. # SSH connection
  58. ssh_Connection = PM.Transport((host, port))
  59. ssh_Connection.connect(username = username, password = password)
  60.  
  61. # Creaat SFTP CLIENT SERVICES
  62. sftp = PM.SFTPClient.from_transport(ssh_Connection)
  63.  
  64. log.write("Connection Established...n")
  65.  
  66. remoteList = sftp.listdir(remotePath)
  67. fileList = os.listdir(SRCfiles)
  68. try:
  69. sftp.chdir(remotePath+'/tmp')
  70. except IOError:
  71. sftp.mkdir(remotePath+'/tmp')
  72. sftp.chdir(remotePath+'/tmp')
  73.  
  74. for fileName in fileList:
  75. if 'comphaulier.asc' not in remoteList:
  76. if 'Last' in fileName:
  77. continue
  78. else:
  79. sftp.put(SRCfiles+'/'+fileName, remotePath+'/tmp/'+fileName)
  80.  
  81. log.write(fileName+" Transferredn")
  82. else:
  83. log.write("Files Still Existn")
  84. log.close()
  85. quit()
  86.  
  87. checkList = sftp.listdir(remotePath)
  88.  
  89. if len(checkList) == 7:
  90. sftp.put(SRCfiles+'/LastFile.lst', remotePath+'/LastFile.lst')
  91. log.write("LastFile.lst Transferredn")
  92. else:
  93. log.write("Not all files transferred!!!n")
  94. quit()
  95.  
  96. sftp.close()
  97. ssh_Connection.close()
  98.  
  99. os.system("rm LockSFTP")
  100.  
  101. sftp.rename(remotePath+'/tmp/'+fileName, remotePath+fileName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement