python远程监控电脑并关机

这个小程序是使用python监控电脑<二>的下个版本,主要实现了下面的功能:

  1. 发送提醒短信到手机。
  2. 获得摄像头照片以及屏幕截图保存在本地,并将其发送到指定邮箱中。
  3. 日志保存功能,以便日后查看。
  4. 监控邮件,实现远程关机功能。

第一个功能在python使用PyFetion来发送短信中有说明,可以点击传送门进去看看。
然后下面是程序

#author:liangliang
#email:liangliangyy@gmail.com
#blog:http://www.lylinux.org/
import time
from VideoCapture import Device
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import smtplib
from PIL import ImageGrab
from PIL import ImageEnhance
import os
from fetion import *
import poplib,email

#获得当前时间并格式化
def get_correcttime() :
    open_time = time.strftime('%m%d%H%M',time.localtime(time.time()))
    return open_time

#获得屏幕截图
def get_desktopimg(correcttime):
    filename = r"F:/learn/watchcomputer/desktop" + correcttime + ".jpg"
    pic = ImageGrab.grab()
    pic.save(filename)
    print "desktop img saved ok!!!!"
    return filename

#获得摄像头照片
def get_webcamimg(correcttime):
    filename = r"F:/learn/watchcomputer/webcam" + correcttime + ".jpg"
    cam = Device()

    res = (640,480)
    cam = Device()
    cam.setResolution(res[0],res[1])

    brightness = 1.0
    contrast = 1.0

    camshot = ImageEnhance.Brightness(cam.getImage()).enhance(brightness)
    camshot = ImageEnhance.Contrast(camshot).enhance(contrast)
    time.sleep(10)
    cam.saveSnapshot(filename,timestamp=3, boldfont=1, quality=80)
    print "webcam img saved ok!!!!!!"
    return filename

#发送屏幕截图和摄像头照片到邮箱
def send_img(desktop,webcam):
    try:
        from_mail = "fffffffffffffff@gmail.com"
        to_mail = "tttttttttttttt@gmail.com"
        msg = MIMEMultipart()
        msg['From'] = from_mail
        msg['To'] = to_mail
        msg['Subject'] = 'my computer'
        body = 'test img send'
        html_code = '<b><i>the guy who use my computer and the screen shot.</b></i>
 <img alt="desktop" src="cid:image1" width="683" height="384"/><img alt="webcam" src="cid:image2"/>'
        print html_code
        con = MIMEText(html_code,'html','utf-8')
        msg.attach(con)
        img1 = MIMEImage(file(desktop,'rb').read())
        img2 = MIMEImage(file(webcam,'rb').read())
        img1.add_header('Content-ID','<image1>')
        img2.add_header('Content-ID','<image2>')
        msg.attach(img1)
        msg.attach(img2)
        server = smtplib.SMTP('smtp.gmail.com')
        server.docmd('ehol',from_mail)
        server.starttls()
        server.login(from_mail,'passwd')#这儿修改成你自己的密码
        server.sendmail(from_mail,to_mail,msg.as_string())
        server.quit()
        print "mail send ok!!!!!"
        return True
    except:
        return False

#写日志
def write_log(issuccess,smsissend):
    log_file_dir = r"F:/learn/watchcomputer/computer.log"
    log_flie = file(log_file_dir,'a')
    log_flie.write('at '+now_time + '  img saved success! ')
    if (issuccess):
        log_flie.write('mail send success!!!')
    elif(not issuccess):
        log_flie.write('mail send failed!!!')
    if (smsIsSend):
        log_flie.write('sms send success')
    elif(not smsIsSend):
        log_flie.write('sms send failed')
    log_flie.write('\n\n')
    log_flie.close()

#发送提醒短信
def send_sms(correct_time):
    myphone = 'phonenumber'
    mypwd = 'fetionpassword'
    tophone = 'tophone'

    print 'send to '+tophone
    sms = 'computer is open at '+correct_time
    try:
        fetion = PyFetion(myphone,mypwd,'TCP')
        fetion.login(FetionHidden)
        fetion.send_sms(sms,tophone,True)
        fetion.logout()
        return True
    except:
        return False

#接收邮件
def accpmail():
    try:
        p=poplib.POP3_SSL('pop.gmail.com')
        #这儿recent在下面有说明
        p.user("recent:username@gmail.com")
        p.pass_('password')

        ret = p.stat()
    except poplib.error_proto,e:
        print "Login failed:",e
        return "fail"
    print "Login succeeded"
    mailnum=ret[0]
    down=p.retr(mailnum)
    for i in down[1]:
        if i.find("Subject:")==0:
            cmdstr=i
            print cmdstr
            break
    p.quit()
    return cmdstr

time.sleep(240)
now_time = get_correcttime()
desktopimg = get_desktopimg(now_time)
webcamimg  = get_webcamimg(now_time)

smsIsSend = send_sms(now_time)

if(os.path.isfile(desktopimg) and os.path.isfile(webcamimg)):
    MailIsSend = send_img(desktopimg,webcamimg)
    write_log(MailIsSend,smsIsSend)

while True:
    cmdstr = accpmail()
    if cmdstr != "fail":
        if cmdstr.find('shutdown')!=-1:
            os.system("123.mp3")
        elif cmdstr.find("quit") != -1:
            break
        elif cmdstr.find("continue") != -1:
            time.sleep(120)
            continue
    else:
        time.sleep(120)
        continue

说明下,在接收邮件函数,也就是accpmail函数中,用户名前面有个recent关键字,由于我使用的是Google的gmail,所以要加这个,如果你是别的邮箱,比如网易的163、162或者是腾讯的qq邮箱的话,就不必加那个。
这个是Google为gmail提供的说明,如果你也是gmail用户的话可以看下,不是的话就可以直接忽略掉了。
http://www.gmail-cn.com/xwnews_view.asp?id=120
还有,你想要远程关闭电脑,只要发送主题带”shutdown”的邮件到指定邮箱就ok了,我自己测试的时候是当检测到该邮件的话播放音乐,如果你想要关机的话只要把
os.system(“123.mp3”)
改成

os.system("shutdown -s -t 0")

就可以了。
好了,就是这样,写完之后看看,还是挺弱智的,现在太晚实在是不想再折腾了,等以后有时间在慢慢完善吧。现在贴出来就当作是抛砖引玉了,还望大家指正,谢谢。

发表评论

电子邮件地址不会被公开。 必填项已用*标注