daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest Nov 5th, 2018 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Define imports.
  2. from time import time, strftime
  3. from datetime import datetime
  4. from glob import iglob
  5. from os import path, system
  6. from sys import exc_info
  7. from subprocess import Popen, PIPE
  8. from curses import initscr, noecho, cbreak, echo, nocbreak, endwin
  9. from psycopg2 import connect
  10.  
  11. # Function to format time.
  12. def formatTime(time):
  13.     # Define the time variables.
  14.     day = time // (24 * 3600)
  15.     time = time % (24 * 3600)
  16.    
  17.     hour = time // 3600
  18.     time %= 3600
  19.    
  20.     minutes = time // 60
  21.     time %= 60
  22.    
  23.     seconds = time
  24.    
  25.     # Format and return the time.
  26.     formatTime = "%d:%d:%d:%d" % (day, hour, minutes, seconds)
  27.     return formatTime
  28.  
  29. # Define local variables.
  30. run = True
  31. complete = False
  32. timeStart = time()
  33. root = path.abspath(path.dirname(__file__))
  34.  
  35. # Define import variables.
  36. files = list()
  37. fileCheck = 0
  38. lineCheck = 0
  39. totalFiles = 0
  40. totalLines = 1
  41. totalErrors = 0
  42.            
  43. # Define database variables.
  44. con = connect(host="localhost", user="**********", password="**********", dbname="**********")
  45. con.set_client_encoding("UNICODE")
  46. cur = con.cursor()
  47.  
  48. # Initialize the script.
  49. system("clear")
  50. print("IMPORTER")
  51. print("A program to import credentials into a PostgreSQL database.\n")
  52.  
  53. # Sanitize user input.
  54. try:
  55.     location = input("Folder Path: ")
  56. except KeyboardInterrupt:  
  57.     # Quit the program.
  58.     system("clear")
  59.     run = False
  60.     quit()
  61.    
  62. if(location[len(location) - 1] == "/"):
  63.     location = location[:len(location) - 1]
  64.  
  65. # Change to cursor screen.
  66. screen = initscr()
  67. noecho()
  68. cbreak()
  69.  
  70. # Check for folder.
  71. if(path.isdir(location)):
  72.     # Loop through files recursively.
  73.     for dir in iglob(location + "/**/*", recursive=True):
  74.         # Check for exit.
  75.         try:
  76.             # Check for file.
  77.             if(path.isfile(dir)):
  78.                 # Update the information.
  79.                 files.append(dir)
  80.                 totalFiles += 1
  81.                 totalLines += int(Popen(["wc", "-l", dir], stdout=PIPE).communicate()[0].decode().split(" ")[0])
  82.                
  83.                 # Output live information.
  84.                 screen.clear()
  85.                 screen.addstr(0, 0, "INITIALIZING IMPORT...")
  86.                 screen.addstr(1, 0, "Getting things read to start importing.")
  87.                 screen.addstr(3, 0, "Files: " + str(totalFiles))
  88.                 screen.addstr(4, 0, "Lines: " + str(totalLines))
  89.                 screen.addstr(5, 0, "")
  90.                 screen.refresh()
  91.         except KeyboardInterrupt:
  92.             # Reset the screen
  93.             screen.keypad(0)
  94.             echo()
  95.             nocbreak()
  96.             endwin()
  97.  
  98.             # Break the loop
  99.             run = False
  100.             break
  101. # Check for file.
  102. elif(path.isfile(location)):
  103.     # Add to array.
  104.     files.append(location)
  105.     totalFiles += 1
  106.     totalLines += int(Popen(["wc", "-l", location], stdout=PIPE).communicate()[0].decode().split(" ")[0])
  107. else:
  108.     # Reset the screen
  109.     screen.keypad(0)
  110.     echo()
  111.     nocbreak()
  112.     endwin()
  113.            
  114.     # Reset the screen.
  115.     print("Cannot find specified location.")
  116.     run = False
  117.     quit();
  118.  
  119. # Ensure the program is running.
  120. while run:
  121.     # Handle errors.
  122.     try:
  123.         # Loop through all files in array.
  124.         for i in range(0, len(files)):
  125.             lineCount = 0
  126.             lineFile = int(Popen(["wc", "-l", files[i]], stdout=PIPE).communicate()[0].decode().split(" ")[0])
  127.                    
  128.             # Loop through lines in files
  129.             file = open(files[i], encoding="latin-1", errors="surrogateescape")
  130.             for line in file:
  131.                 try:
  132.                     # Increment the counters.
  133.                     lineCount += 1
  134.                     lineCheck += 1
  135.  
  136.                     # Parse out the credentials.
  137.                     line = line.rstrip("\n")
  138.                     pos = line.find(":")
  139.                     if(pos == -1):
  140.                         pos = line.find(";")
  141.                        
  142.                     # Get the credential variables.
  143.                     username = line[:pos]
  144.                     password = line[pos+1:]
  145.                    
  146.                     # Output live information.
  147.                     screen.clear()
  148.                     screen.addstr(0, 0, "IMPORTING CREDENTIALS...")
  149.                     screen.addstr(1, 0, "Importing the data into the database.")
  150.                     screen.addstr(3, 0, "FILES")
  151.                     screen.addstr(4, 0, "Path: " + files[i])
  152.                     screen.addstr(5, 0, "Username: " + username[:40])
  153.                     screen.addstr(6, 0, "Password: " + password[:40])
  154.                     screen.addstr(7, 0, "Lines: " + str(lineCount) + "/" + str(lineFile))
  155.                     screen.addstr(8, 0, "Rate: " + str(round(lineCount / (time() - timeStart), 3)) + " str/s")
  156.                     screen.addstr(10, 0, "TOTAL")
  157.                     screen.addstr(11, 0, "Files: " + str(fileCheck) + "/" + str(totalFiles))
  158.                     screen.addstr(12, 0, "Lines: " + str(lineCheck) + "/" + str(totalLines))
  159.                     screen.addstr(13, 0, "Errors: " + str(totalErrors))
  160.                     screen.addstr(14, 0, "Rate: " + str(round(lineCheck / (time() - timeStart), 3)) + " str/s")
  161.                     screen.addstr(15, 0, "Time: " + formatTime(round(time() - timeStart)))
  162.                     screen.addstr(16, 0, "Complete: " + str(round((lineCheck / totalLines) * 100, 3)) + "%")
  163.                     screen.addstr(17, 0, "")
  164.                     screen.refresh()
  165.                    
  166.                     # Import to the database.
  167.                     cur.execute("INSERT INTO clear(username, password) VALUES(%s, %s);", [username, password])
  168.                     con.commit()
  169.                 except KeyboardInterrupt:
  170.                     raise
  171.                 except:
  172.                     # Check if the files exists.
  173.                     if(path.exists(root + "/errors.log") == False):
  174.                         new = True
  175.                     else:
  176.                         new = False
  177.                    
  178.                     # Log the error's log.
  179.                     log = open(root + "/errors.log", "a")
  180.                     if(new):
  181.                         log.write("{0:<20}".format("TIME"))
  182.                         log.write("{0:<30}".format("FILE PATH"))
  183.                         log.write("{0:<10}".format("LINE"))
  184.                         log.write("{0:<25}".format("USERNAME"))
  185.                         log.write("{0:<25}".format("PASSWORD"))
  186.                         log.write("{0:<30}".format("TYPE"))
  187.                         log.write("{0:<100}\n".format("INFO"))
  188.                    
  189.                     # Log the error.
  190.                     log.write("{0:<20}".format(strftime("%x") + " " + strftime("%X")))
  191.                     log.write("{0:<30}".format(files[i]))
  192.                     log.write("{0:<10}".format(str(lineCount)))
  193.                     log.write("{0:<25}".format(username[:20]))
  194.                     log.write("{0:<25}".format(password[:20]))
  195.                     log.write("{0:<30}".format(str(exc_info()[0])))
  196.                     log.write("{0:<100}\n".format(str(exc_info()[1])))
  197.                     log.close()
  198.                    
  199.                     # Increment the counters.
  200.                     totalErrors += 1
  201.                                
  202.                     # Resume on error.
  203.                     pass
  204.             file.close()
  205.             fileCheck += 1
  206.            
  207.             # Check for end of files array.
  208.             if i == len(files) - 1:
  209.                 # Reset the screen
  210.                 screen.keypad(0)
  211.                 echo()
  212.                 nocbreak()
  213.                 endwin()
  214.  
  215.                 # Close the database connection.
  216.                 cur.close()
  217.                 con.close()
  218.  
  219.                 # Break the loop
  220.                 complete = True
  221.                 run = False
  222.     except KeyboardInterrupt:
  223.         # Reset the screen
  224.         screen.keypad(0)
  225.         echo()
  226.         nocbreak()
  227.         endwin()
  228.  
  229.         # Close the database connection.
  230.         cur.close()
  231.         con.close()
  232.  
  233.         # Break the loop
  234.         run = False
  235.        
  236. # Output final information.
  237. system("clear")
  238. if(complete):
  239.     print("IMPORT COMPLETE\n")
  240. else:
  241.     print("IMPORT CANCELED\n")
  242. print("Files: " + str(fileCheck) + "/" + str(totalFiles))
  243. print("Lines: " + str(lineCheck) + "/" + str(totalLines))
  244. print("Errors: " + str(totalErrors))
  245. print("Time: " + formatTime(round(time() - timeStart)))
  246. print("Rate: " + str(round(lineCheck / (time() - timeStart), 3)) + " str/s")
  247. print("Complete: " + str(round((lineCheck / totalLines) * 100, 3)) + "%")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top