Requirements: Access the intranet via VPN from the extranet and log in to Computer A. Computer A through the shared folder (need \\IP access) to the intranet computer B, because the computer B's WI-fi IP address will change, so when computer B's I access path needs to know Computer B's latest IP
Import socket
Import Smtplib
Import JSON
From Email.header Import Header
From Email.mime.text import Mimetext
From email.utils import parseaddr, formataddr
Import threading
NOW_IP = ""
Mail_param_json = ""
# Intranet B is Win7 operating system, the intranet IP starts with 10, takes Wifi-ipdef _get_ip_win ( ):= SOCKET.GETHOSTBYNAME_EX (Socket.gethostname ()) for in ips[2]: if s.startswith ('ten'): return s
#send a message to the specified mailbox#The configuration file is a JSON string: ' {' from_addr ': ', ' Password ': ', ' to_addr ': ', ' smtp_server ': ' "}'def_format_addr (s): Name, addr=parseaddr (s)returnFormataddr (Header (name,'Utf-8'). Encode (), addr))defSend_mail (json_param_str=None):ifJson_param_str! = None andisinstance (Json_param_str, str): E_param=json.loads (json_param_str) from_addr= E_param.get ('from_addr') Password= E_param.get ('Password') to_addr= E_param.get ('to_addr') Smtp_server= E_param.get ('Smtp_server') msg= Mimetext ('IP address changed to:'+ _get_ip_win () +'\ r \ n','Plain','Utf-8') msg[' from'] = _FORMAT_ADDR ('Reboot <%s>'%from_addr) msg[' to'] = _FORMAT_ADDR ('Reboot <%s>'%to_addr) msg['Subject'] = Header ('NOTICE!!! --PC IP CHANGED','Utf-8'). Encode () server= Smtplib. SMTP (Smtp_server, 25) Server.set_debuglevel (1) server.login (from_addr, password) server.sendmail (from_addr, [to_addr], msg.as_string ()) server . Quit ()Else: Pass
#Check IP periodically for changesdefCheckip (start_ip):Globalnow_ipifStart_ip! =now_ip:GlobalMail_param_json send_mail (Mail_param_json)Print("send mail, old IP:", Now_ip,"---New IP:", start_ip) now_ip=start_ipElse: Print("The IP address has not changed, the old IP:", Start_ip,"---New IP:", now_ip) new_ip=_get_ip_win () T= Threading. Timer (3600.0, Checkip, (New_ip,)) T.start ()
# initializing the Read configuration file if __name__ ' __main__ ' : With open ('./config'r') as F : = f.read () = _get_ip_win (), _get_ip_win () checkip (START_IP)
Ps:
1, only the basic functions of implementation, code is not standardized
2, to be modified to increase the log record, the configuration file is written to Sqllite
3, the method needs to be reconstructed, not flexible
Python3 Exercise-Send IP address to mailbox