Using Python to execute system commands via mail

Source: Internet
Author: User

Implementation of the idea: by detecting the inbox of the mail sent by the specified user, detect the message header, execute the specified command. The program periodically detects messages in the Inbox, separates the sender and title of the first message in the Inbox, ignores the message if the sender is not the specified user, detects the title in the message and executes the specified command in the header, and sends two messages out, a message with a meaningless title to itself, Prevents repeated execution of a command, sending a message to a specified user informing the user that the corresponding command has been executed.

#-*-coding:cp936-*-ImportOS, sys, stringImportPoplibImportSmtplibImport TimedefReceive_mail ():#detects whether a mailbox receives a command message sent from a specified mailboxHost ="pop3.126.com"            #POP3 server addressUsername ="[email protected]"         #User namePassword ="Password"            #Passwordpp= Poplib. POP3 (host)#Create a POP3 object that is actually connected to the server at this time.Pp.set_debuglevel (1)#set debug mode to see interactive information with the serverPp.user (username)#send a user name to the serverPp.pass_ (password)#send a password to the serverret= Pp.stat ()#get the message on the server, return is a list, the first item is a total of more than one message, the second item is the total number of bytesDown = PP.RETR (Ret[0])#get the first message    #Output Message Content    #For line in down[1]:    #Print LineGetfrom=down[1][9].decode ('Utf-8') 0 Getsubject=down[1][11].decode ('Utf-8') Pp.quit ()#Exit      ifgetfrom!='From : "=?utf-8?" b?5qkb6zsm5r2u?= "<[email protected]>':        Pass    Else:        ifgetsubject=='Subject:reboot':            return1elifgetsubject=='SUBJECT:RESTARTMQ':            return2elifgetsubject=='Subject:ifconfig':            return3Else:            return0defsendmail (): Send_mail="[email protected]"      #e-mail inboxSend_mail_passwd="Password"            #e-mail passwordReceive_mail='[email protected]'  #Receive MailboxSend_mail_server='smtp.126.com'            #Outgoing mailbox SMTP servermail_to= Smtplib. SMTP (send_mail_server,25) Mail_to.login (send_mail,send_mail_passwd) msg="From : [email protected] <"+send_mail+""">to: <"""+receive_mail+""">subject:igorn Messigorn Mess"""Mail_to.sendmail (send_mail,receive_mail,msg) mail_to.close ()defsendreply (): Send_mail="[email protected]"      #e-mail inboxSend_mail_passwd="Password"            #e-mail passwordReceive_mail='[email protected]'  #Receive MailboxSend_mail_server='smtp.126.com'            #Outgoing mailbox SMTP servermail_to= Smtplib. SMTP (send_mail_server,25) Mail_to.login (send_mail,send_mail_passwd) msg="From : [email protected] <"+send_mail+""">to: <"""+receive_mail+""">subject:order Excuorder Excu"""Mail_to.sendmail (send_mail,receive_mail,msg) mail_to.close ()if __name__=='__main__':     while1: Time.sleep (5)        ifReceive_mail () ==1: SendMail () sendreply () Os.system ('Shutdown-r Now')        elifReceive_mail () ==2: SendMail () sendreply () Os.system ('RESTARTMQ')        elifReceive_mail () ==3: SendMail () sendreply () Os.system ('ifconfig')        Else:            Pass

Using Python to execute system commands via mail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.