RPi 2B automatically sends the acquired IP to the fixed mailbox

Source: Internet
Author: User
Tags get ip

/************************************************************************* * RPi 2B automatically send get IP to a fixed mailbox * declaration: * This article mainly records the RPI 2B how to automatically send IP to the mailbox in the form of mail. * 2016-2-21 Shenzhen Nanshan Ping Shan village Zengjianfeng ************************************************************ ************/First, reference documents:1. RPi Email IP on Boot Debian http://Elinux.org/rpi_email_ip_on_boot_debian    2. How to convert list tostring[Duplicate] http://stackoverflow.com/questions/5618878/how-to-convert-list-to-string    3. Python forLoop statements http://www.tutorialspoint.com/python/python_for_loop.htmSecond, cat bootsendemail.py #!/usr/bin/python import subprocess import smtplib fromemail.mime.text Import mimetext import datetime # change to your own account information # account Infor Mation to='[email protected]'# Email to send to. Mail_user='[email protected]'# Email to send from. Mail_password='fill in the authorization password'# Email password. SmtpServer= Smtplib. SMTP ('smtp.163.com') # Server to use. Smtpserver.ehlo () # Says'Hello'To the server Smtpserver.starttls () # Start TLS Encryption Smtpserver.ehlo () Smtpserv Er.login (Mail_user, Mail_password) # LoginchTo server today= Datetime.date.today () # Get Current time/Date arg='IP Route list'# Linux command to retrieve IP addresses. # Runs'Arg' inchA'Hidden Terminal'. P=subprocess. Popen (arg,shell=true,stdout=subprocess. PIPE) Data= P.communicate () # Get data from 'P Terminal'. # GetIP Data ip_lines= data[0].splitlines () IPs=""     forIpinchip_lines:ips+ = IP +"\ n"# creates the text, subject,' from', and' to'of the message. Msg=Mimetext (IPs) msg['Subject'] ='IPs for Raspberrypi on%s'% Today.strftime ('%b%d%Y') msg[' from'] ="[email protected]"msg[' to'] ="[email protected]"# sends the message smtpserver.sendmail (Mail_user, [to], msg.as_string ()) # Closes the SMTP server. Smtpserver.quit () Three, put bootsendemail.py into/usr/bin/Iv. Modify/etc/rc.local #!/bin/sh-E # # rc.local # # this script isExecuted at the end of each multiuser runlevel. # Make sure that the script would"Exit 0"On success or any other # value on error. # # In order to enable or disable Thisscript Just change the execution # bits. # # bydefault  Thisscript does nothing. # Print The IP address _ip=$ (hostname-i) | |true    if["$_ip" ]; Then printf"My IP address is%s\n" "$_ip"bootsendemail.py # Add ThisLine Fi exit0Five, restart the system, and check the mailbox:defaultVia192.168.1.1Dev Wlan0defaultVia192.168.1.1Dev Wlan0 Metric303     192.168.0.0/ -Dev eth0 proto kernel scope link src192.168.0.5     192.168.1.0/ -Dev wlan0 proto kernel scope link src192.168.1.102     192.168.1.0/ -Dev wlan0 proto kernel scope link src192.168.1.102Metric303 

RPi 2B automatically sends the acquired IP to the fixed mailbox

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.