利用python收發郵件功能實現遠程電腦的控制

來源:互聯網
上載者:User

標籤:

功能並非原創, 只是重複實現了 http://codecloud.net/python-control-128.html 中描述的功能. 

 

實現功能: 通過給固定郵件地址發送命令(包含在主題中)的方式控制遠程電腦實現相應功能(譬如關機等, 可以根據實際需要實現更複雜的功能)

實現原理: 遠程電腦不斷(每隔10s)利用 python 指令碼檢查郵件伺服器是否有新郵件, 如有則對郵件進行分析, 如果包含可執行命令, 則執行相應命令

你需要的: 裝有 PYTHON 的 PC; 最好兩個郵箱(一個用於接收命令, 一個用來發郵件); 可選手機郵件用戶端(可以實現在手機上發送郵件, 更高大上)

注意: 不要使用163郵箱來接受命令, 每隔 10s 的檢查間隔會導致網易拒絕讓你登入的~ 

 

我反正喪心病狂用學校的伺服器.  Python 代碼如下:

 1 # -*- coding: utf-8 -*- 2  3 import poplib,email,re  4 from email.header import decode_header 5 import smtplib 6 from email.mime.text import MIMEText 7 import time 8 import os,sys 9 import random10 11 12 def send_mail(to_mail_addr, message):13     from_mail_addr = "****@fudan.edu.cn"14     password = "****"15     smtp_server = "mail.fudan.edu.cn"  #smtp_server16 17     message = "Hello, mission received: \n \t \t" + message18     msg = MIMEText(message, ‘plain‘, ‘gb2312‘)19     msg[‘Subject‘] = ‘This is a reply msg by PythonMailControl‘20     msg[‘From‘] = from_mail_addr21     msg[‘To‘] = to_mail_addr22 23     server = smtplib.SMTP(smtp_server, 25) 24     server.set_debuglevel(1)25     server.login(from_mail_addr, password)26     server.sendmail(from_mail_addr, [to_mail_addr], msg.as_string())27     server.quit()28 29 30 def get_last_mail():31     try:32         host = ‘mail.fudan.edu.cn‘  # pop3 server33         username = ‘****@fudan.edu.cn‘34         password = ‘****‘35         p = poplib.POP3_SSL(host)36         p.user(username)37         p.pass_(password)38         ret = p.stat()39     except poplib.error_proto,e:40         print "Login failed:",e41         return (-2,0,0)  # error code, nonsense, nonsense42 43     mail_number = ret[0] # return the total number of mails44     ret = p.list()45     down = p.retr(1)     # return the last received mail46     return (1,mail_number,down) 47 48 49 if __name__ == "__main__": 50     mail_number_old = 051     while 1:52         (error,mail_number,down) = get_last_mail()53         if (error==1) and (mail_number != mail_number_old):  # no error and have new mail54             mail_number_old = mail_number    55             for line in down[1]:    # check the mail line by line, get the sender and subject56                 if line[0:4]=="From":57                     from_mail_addr = re.search( ‘[0-9a-zA-Z_]*@[0-9a-zA-Z_.]*‘, line).group(0)   58                 if line[0:7]=="Subject":59                     content = line[9:]60             if content[0] == ‘@‘:   # response according to the subject61                 cmd = content[1:]62                 try:63                     os.system(cmd)  # do sth.64                     send_mail(from_mail_addr,cmd)  # reply message to the mail sender65                 except:66                     os.system(‘echo error‘)67         time.sleep(10)

 

運行待機情況: 

 

然後用手機給我的學號郵箱發個郵件. 主題是 @shutdown -s -t 600

其中 @ 是前置字元, 用於給程式判斷這是一個命令. shutdown 等則是10分鐘後關機的命令. 

 

等待5-10s, 在PC上就出現了等待關機的提示

 

同時, 手機上也受到了回複的郵件. 

 

完. 

然而暫時並沒有什麼卵用~

 

利用python收發郵件功能實現遠程電腦的控制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.