ftp sync

From cm3l1k1, 4 Years ago, written in Python, viewed 658 times.
URL http://paste.security-portal.cz/view/4b0ddcd3 Embed
Download Paste or View Raw
  1. #!/usr/bin/env python
  2. #
  3. # desc: Script for mirroring stage3 and portage files
  4. # author: Martin Cmelik & script examples over Internet
  5. #
  6. #
  7. # ToDo: download newer files only, delete old/unused files
  8. #
  9. #
  10. # -*- coding: utf-8 -*-
  11.  
  12.  
  13. import sys
  14. import ftplib
  15. import os
  16. from ftplib import FTP
  17.  
  18. ftp=FTP("ftp.fi.muni.cz")
  19. ftp.login("anonymous","info@securix.org")
  20.  
  21. #
  22. # FUNCTIONS
  23. #
  24.  
  25. # remove lines with # from file
  26. def processConf(s):
  27.         "Remove #comments from string."
  28.         s = map(lambda x: x.split("#")[0], s.splitlines())
  29.         return filter(lambda x: x.strip() != "", s)
  30.  
  31. def downloadFiles(path,destination,filematch):
  32.     try:
  33.         ftp.cwd(path)
  34.         #clone path to destination
  35.         os.chdir(destination)
  36.         os.mkdir(destination[0:len(destination)-1]+path)
  37.         print destination[0:len(destination)-1]+path+" built"
  38.     except OSError:
  39.         #folder already exists at destination
  40.         pass
  41.     except ftplib.error_perm:
  42.         #invalid entry (ensure input form: "/dir/folder/something/")
  43.         print "error: could not change to "+path
  44.         sys.exit("ending session")
  45.  
  46.     # get file list
  47.     filelist=ftp.nlst(filematch)
  48.  
  49.     for file in filelist:
  50.         try:
  51.             #this will check if file is folder:
  52.             ftp.cwd(path+file+"/")
  53.             #if so, explore it:
  54.             downloadFiles(path+file+"/",destination)
  55.         except ftplib.error_perm:
  56.             #not a folder with accessible content
  57.             #download & return
  58.             os.chdir(destination[0:len(destination)-1]+path)
  59.             #possibly need a permission exception catch:
  60.             lf = open(os.path.join(destination,file),"wb")
  61.             ftp.retrbinary("RETR "+file, lf.write, 8*1024)
  62.             print file + " downloaded"
  63.     return
  64.  
  65. #
  66. # DOWNLOAD FILES
  67. #
  68.  
  69. # sync Portage files
  70. source="/pub/linux/gentoo/releases/snapshots/current/"
  71. dest="/releases/snapshots/current/"
  72. filematch="portage-latest.tar.bz2*"
  73. downloadFiles(source,dest,filematch)
  74.  
  75. # sync amd64 Stage3 LATEST file
  76. source="/pub/linux/gentoo/releases/amd64/autobuilds/"
  77. dest="/releases/amd64/autobuilds/"
  78. filematch="latest-stage3-amd64-hardened.txt"
  79. downloadFiles(source,dest,filematch)
  80.  
  81. LATEST_STAGE3 = processConf(os.path.join(dest,filematch))
  82.  
  83. # sync amd64 Stage3 files
  84. source="/pub/linux/gentoo/releases/amd64/autobuilds/" + LATEST_STAGE3
  85. dest="/releases/amd64/autobuilds/" + LATEST_STAGE3
  86. filematch="stage3-amd64-hardened-*"
  87. downloadFiles(source,dest,filematch)
  88.  
  89. # sync x86 Stage3 LATEST file
  90. source="/pub/linux/gentoo/releases/x86/autobuilds/"
  91. dest="/releases/x86/autobuilds/"
  92. filematch="latest-stage3-i686-hardened.txt"
  93. downloadFiles(source,dest,filematch)
  94.  
  95. LATEST_STAGE3 = processConf(os.path.join(dest,filematch))
  96.  
  97. # sync x86 Stage3 files
  98. source="/pub/linux/gentoo/releases/x86/autobuilds/" + LATEST_STAGE3
  99. dest="/releases/x86/autobuilds/" + LATEST_STAGE3
  100. filematch="stage3-i686-hardened-*"
  101. downloadFiles(source,dest,filematch)
  102.  
  103.  

Reply to "ftp sync"

Here you can reply to the paste above