Raspberry PI Development Tour-Send message recording time and IP

Source: Internet
Author: User
Tags starttls

Since most of the scenes I use for the Raspberry Pi are in the absence of a monitor and only connect it with terminal, its IP address is sometimes changed after a reboot (DHCP), which makes it impossible for me to connect to it via terminal. Then I have to be very troublesome to log into the management interface of the router, to see what the new IP it is assigned to, and then use Terminal to re-connect, too troublesome, right? As a Raspberry Pi player, this hassle is simply unacceptable!

In order to solve this problem, I let pi boot, automatically send me an email to my designated email, tell me its IP address when the boot.
Step: Execute a script on boot, detect network availability → Get your own IP address after the network is unblocked → send mail to the specified mailbox.
Here are one by one of the way.

1. Boot Start Item
Boot execution How does a script do that?
Just add a word to the/etc/rc.local file to start executing a script:

# Send a mail to notify the IP address of pi/root/data/source/send-ip-mail.sh >>/root/data/source/send-ip-mail.log 2>&1

2, the reporting IP address of the script implementation
The contents of the send-ip-mail.sh script are as follows: (Vim does not automatically create the specified directory)

#!/bin/bash # Check network availabilitywhile truedo  timeout=5  site_to_check= "www.126.com" ret_code=  ' Curl-i-S--connect-timeout $TIMEOUT $SITE _to_check-w%{http_code} | Tail-n1 '  if ["X$ret_code" = "x200"]; then  echo "Network OK, would send mail ..." Break  Else  echo "Net Work isn't ready, wait ... "  sleep 1s  fidone # get the IP address of eth0, e.g." 192.168.16.5 "Eth0_ip_addr= ' ifconfig E Th0 | Sed-n "2,2p" | awk ' {print substr ($2,1)} ' # Send the Emailecho ' Current time: ' Date ' +%f%T '. Enjoy It "| Mutt-s "IP Address of Raspberry Pi: $ETH 0_ip_addr" [email protected]

The script is simple and divided into 3 parts: The first part detects the network availability; The second part takes the IP address of the Raspberry Pi eth0 network card; The third part sends the message to the specified email.
Among them, the first part is must have, because after I experiment, in this script execution, the Raspberry Pi network is not initialized well, at this time you send directly mail is not out. I'm here to determine network availability by accessing www.126.com.
The third part requires you to pre-configure Mutt and MSMTP.

3. Installation configuration Mutt and MSMTP
Once you've configured Mutt and MSMTP, you can send the message through a code like above.
The first thing to do is to install Mutt and msmtp on the PI:

Pacman-s Msmtppacman-s Mutt

After installation, configure MSMTP first. Create the file under your user's root directory. MSMTPRC, the following:

Account Defaulthost smtp.126.comfrom [email protected]auth plainuser [email protected]password your_passwordlogfile/ Var/log/msmtp.log

Where smtp.126.com is the SMTP server address of the mailbox I use, [email protected] is the mailbox I used to send mail, Your_password is the mailbox password, you have to modify according to your situation.

Then configure Mutt. Create the file under your user's root directory. MUTTRC, the following:

Set sendmail= "/usr/bin/msmtp" set Use_from=yesset realname= "Alarm" set editor= "Vim"

Where Realname is the name of the sender and is displayed in the received message.

4. MSMTP Test

Test configuration file: Msmtp-p Test SMTP server: msmtp-s
[Email protected]:~$ msmtp--host=smtp.163.com--serverinfosmtp Server at smtp.163.com (smtp.163.gslb.netease.com [ 220.181.12.18]), Port:    163.com Anti-Spam GT for Coremail System (163com[20121016]) capabilities:    pipelining: Support for        command grouping for faster transmission    STARTTLS: Support for        TLS encryption via the STARTTLS command    AUTH:        Supported authentication methods:        PLAIN loginthis server might advertise more or other capabilities when TLS is active.

From the return information we can see that this SMTP is supported by TLS, authentication mode support PLAIN and LOGIN

5. Test Mail

Command Line Input:

echo "Test" |mutt-s "my_first_test" [email protected]

6, at this point all done, after each pi boot time, will be "tell", we no longer worry about finding PI!

7. Frequently Asked Questions:

Error 1:

Msmtp:account Default not found:no configuration file available
Msmtp There is a bug, you must specify the corresponding configuration file manually
Change/etc/muttrc set sendmail= "/usr/bin/msmtp" to set sendmail= "/usr/bin/msmtp-c. MSMTPRC"
Error 2:

MSMTP:GNU sasl:base Coding error in SASL Library
BASE64 encoding Error encountered
Change ~/.MSMTPRC in auth login
For Auth Plain
Error 3:

Statement: echo "testtest" |mutt-f/home/spider/.muttrc-s "tttttttt" [email protected]
Prompt when sending message: The sending message appears in error, the subroutine has ended 127 (Exec error.).
Unable to send a letter

There is usually a problem with the setup file,

Send the test using MSMTP first

[Email protected] ~]#/USR/LOCAL/MSMTP/BIN/MSMTP-SSMTP Server at smtp.sohu.com ([220.181.90.34]), Port:    zw_71_ PNS ESMTP readycapabilities:    STARTTLS: Support for        TLS encryption via the STARTTLS command    AUTH:        Supported authentication methods:        PLAIN loginthis Server might advertise more or other capabilities when TLS is active .

Found no problem

Re-use MSMTP to view the current file path

[[email protected] ~]#/usr/local/msmtp/bin/msmtp-ploaded System Configuration file/usr/local/msmtp/etc/ Msmtprcignoring User Configuration File/root/.msmtprc:no such file or directoryfalling back to default accountusing Acco               unt default from/usr/local/msmtp/etc/msmtprchost = Smtp.sohu.comport = 25timeout                  = Offprotocol = Smtpdomain = Localhostauth = Loginuser                   = Zabbix2018password = *passwordeval = (not set) Ntlmdomain = (not set) TLS       = Offtls_starttls = Ontls_trust_file = (not set) Tls_crl_file = (not set) Tls_fingerprint       = (not set) Tls_key_file = (not set) Tls_cert_file = (not set) Tls_certcheck = Ontls_force_sslv3            = Offtls_min_dh_prime_bits = (not set) Tls_priorities = (not set) Auto_from = Offmaildomain                = (not set) from  = [Email protected]dsn_notify = (not set) Dsn_return = (not set) KEEPBCC = Offlog File =/var/log/zabbix/msmtp.logsyslog = (not set) aliases = (not set) reading re Cipients from the command line

There is nothing wrong with displaying the configuration file from above, but when viewing. MUTTRC, you also notice a double quote character error. Modify the keyboard layout.

Error 4:

[Email protected]:~/desktop/python$ sudo echo Hello World | Mutt-s "Test Mail" [email protected]
Msmtp:authentication failed (method PLAIN)
Msmtp:server message:550 User is locked
Msmtp:could not send mail (account default FROM/HOME/DING/.MSMTPRC)
Error sending message, child exited (insufficient permission.).
Could not send the message.

The SMTP service is not turned on, the newly registered user appears to be turned off by default, some mailboxes are turned off by default and need to be turned on manually.

After you turn on the SMTP service, use the authorization password issued by the 163 mailbox server as the password= authorization code in the/HOME/DING/.MSMTPRC file

Reference: http://jingyan.baidu.com/article/3f16e003e327772591c1039f.html?st=2&os=0&bd_page_type=1&net_ type=2

Error 5:

[Email protected]:/u1/str0101>mutt-s dfdf [email protected] <.tmp
Error sending message, child exited (Service unavailable.).
Segmentation fault (core dumped)

Mail server restrictions, view sent log files. (I changed from QQ to NetEase mailbox)

Extended:

Using the standard pc104 keyboard

Domestic use of the standard 104 keyboard, the following start the Raspberry Pi settings.

1, sudo raspi-config

2. Enter International configuration options

3. Modify the keyboard layout

4. Select PC104 Standard keyboard

5. Select US Standard

6. Select Keyboard Default Layout

7. Compose key setting

8. Ctrl+alt+backspace combination, similar to the ctrl+alt+delete of Windows.

9. Complete the Setup

Raspberry PI Development Tour-Send message recording time and IP

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.