Python 利用邮件系统完成远程控制电脑的实现(关机、重启等)
0.我们如何通过邮件系统完成远程控制电脑(关机、重启等)?
实现思路:
需要有两个邮箱:接收指令邮箱(A)发送指令邮箱(B)
被控制的电脑(查看A邮箱):
1. 每隔指定时间监听A 邮箱 查看最近的邮件
2.打开最近邮件,看是否是 B邮箱地址发来的执行命令
3.向A和B邮箱都发送主题为:反馈内容为:“目标计算机已收到指令!开始执行:xxx指令!”邮件
4. 执行指定邮箱发送的执行命令
远程人员操作:
登录B邮箱,向A邮箱发送主题为:“目标计算机执行”内容为:xxx指令(例如:关机)
1.代码编写
importpoplib importsmtplib importos importtime fromemail.parserimportParser fromemail.headerimportdecode_header fromemail.utilsimportparseaddr fromemail.mime.textimportMIMEText fromemail.headerimportHeader email_params=dict() email_params['email_message']='反馈' email_params['email_subject']='目标计算机反馈' email_params['email_from']='A邮箱' email_params['email_user']='A邮箱' email_params['email_exec']='B邮箱' email_params['email_to']=['A邮箱','B邮箱'] email_params['email_smtp']='smtp.163.com' email_params['email_pop']='pop.163.com' email_params['email_pass']='A邮箱的密码(smtp、pop3的密码)' interval_time=5 defdecode_str(s): value,charset=decode_header(s)[0] ifcharset: value=value.decode(charset) returnvalue defget_headers(message): headers=dict() forheaderin['From','To','Subject']: value=message.get(header,'') ifvalue: ifheader=='Subject': headers['Subject']=decode_str(value) elifheader=='From': hdr,addr=parseaddr(value) headers['From']=decode_str(addr) elifheader=='To': hdr,addr=parseaddr(value) headers['To']=decode_str(addr) returnheaders defguess_charset(message): charset=message.get_charset() ifcharsetisNone: content_type=message.get('Content-Type','').lower() pos=content_type.find('charset=') ifpos>=0: charset=content_type[pos+8:].strip() returncharset defget_content(message): forpartinmessage.walk(): content_type=part.get_content_type() charset=guess_charset(part) ifcontent_type=='text/plain'andcharset: try: returnpart.get_payload(decode=True).decode(charset) exceptAttributeError: print('typeerror') exceptLookupError: print("unknownencoding:utf-8") defreceive_email(): email_server=poplib.POP3_SSL(email_params['email_pop']) email_server.user(email_params['email_user']) email_server.pass_(email_params['email_pass']) resp,mails,octets=email_server.list() index=len(mails) ifindex==0: returnNone,None resp,lines,octets=email_server.retr(index) message=b'\r\n'.join(lines).decode('utf-8','ignore') message=Parser().parsestr(message) email_server.quit() returnget_headers(message),get_content(message) defsend_email(email_message): message=MIMEText(email_message,'plain','utf-8') message['Subject']=Header(email_params['email_subject'],'utf-8') message['From']=Header(email_params['email_from']) message['To']=Header(','.join(email_params['email_to']),'utf-8') email_server=smtplib.SMTP_SSL(email_params['email_smtp']) email_server.login(email_params['email_from'],email_params['email_pass']) email_server.sendmail(email_params['email_from'],email_params['email_to'],message.as_string()) email_server.close() defexec_operator(exec_content): ifexec_content=='关机': os.system("shutdown-s-t1") elifexec_content=='重启': os.system("shutdown-r") else: os.system(exec_content) if__name__=='__main__': whileTrue: headers,content=receive_email() ifheadersandcontentandheaders['From']==email_params['email_exec']andheaders['Subject']=='目标计算机执行': email_message="目标计算机已接收到指令!开始执行:"+content+"指令!" send_email(email_message) exec_operator(content) time.sleep(interval_time)
2.解释说明
A邮箱:是163邮箱,需要开启smtp、pop3服务
上面所说的smtp、pop3密码,指的是开启上图所示的服务要设定的密码!!!
B邮箱:是QQ邮箱,只是用来发送执行命令到A邮箱的,所以不必开启smtp、pop2服务
3.运行截图
B邮箱向A邮箱发送命令