<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DHCP &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/dhcp/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Tue, 28 Apr 2020 02:26:53 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] 让树莓派自动上报IP地址到邮箱/Let Raspberry Pi to report its IP address via Email</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%ae%a9%e6%a0%91%e8%8e%93%e6%b4%be%e8%87%aa%e5%8a%a8%e4%b8%8a%e6%8a%a5ip%e5%9c%b0%e5%9d%80%e5%88%b0%e9%82%ae%e7%ae%b1let-raspberry-pi-to-report-its-ip-address-via-email/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%ae%a9%e6%a0%91%e8%8e%93%e6%b4%be%e8%87%aa%e5%8a%a8%e4%b8%8a%e6%8a%a5ip%e5%9c%b0%e5%9d%80%e5%88%b0%e9%82%ae%e7%ae%b1let-raspberry-pi-to-report-its-ip-address-via-email/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Fri, 14 Mar 2014 15:02:46 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Raspberry Pi/树莓派]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[DHCP]]></category>
		<category><![CDATA[IP地址]]></category>
		<category><![CDATA[msmtp]]></category>
		<category><![CDATA[mutt]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[report IP address]]></category>
		<category><![CDATA[上报IP]]></category>
		<category><![CDATA[树莓派]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=7858</guid>

					<description><![CDATA[<p>
<a href="http://www.raspberrypi.org/" target="_blank" rel="noopener noreferrer">Raspberry Pi</a>是什么？<br />
引用维基百科的一句话：</p>
<blockquote>
<p>
		The Raspberry Pi is a credit card sized single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools.</p>
</blockquote>
<p>简单地说，它就是一个基于ARM CPU的、信用卡那么大的迷你计算机。</p>
<p>由于我使用树莓派的场景大多数是在没有显示器、只用terminal连接它的情况下，所以，它的IP地址有时会在重启之后变掉（DHCP的），导致我无法通过terminal连接上它。然后我又要很麻烦地登录路由器的管理界面里，去看它被分配到的新IP是什么，然后用terminal重连，太麻烦了，不是么？作为一个树莓派玩家，这种麻烦简直是无法接受的！<br />
<span id="more-7858"></span><br />
为了解决这个问题，我让Pi开机的时候，自动向我指定的Email发送一封邮件，告诉我它此次开机时的IP地址。<br />
步骤：<span style="color:#0000ff;">开机时执行一个脚本，检测网络可用性&#8594;网络通畅后获取自己的IP地址&#8594;发送邮件到指定的邮箱。</span><br />
下面一一道来。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%ae%a9%e6%a0%91%e8%8e%93%e6%b4%be%e8%87%aa%e5%8a%a8%e4%b8%8a%e6%8a%a5ip%e5%9c%b0%e5%9d%80%e5%88%b0%e9%82%ae%e7%ae%b1let-raspberry-pi-to-report-its-ip-address-via-email/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
<a href="http://www.raspberrypi.org/" target="_blank" rel="noopener noreferrer">Raspberry Pi</a>是什么？<br />
引用维基百科的一句话：</p>
<blockquote>
<p>
		The Raspberry Pi is a credit card sized single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools.</p>
</blockquote>
<p>简单地说，它就是一个基于ARM CPU的、信用卡那么大的迷你计算机。</p>
<p>由于我使用树莓派的场景大多数是在没有显示器、只用terminal连接它的情况下，所以，它的IP地址有时会在重启之后变掉（DHCP的），导致我无法通过terminal连接上它。然后我又要很麻烦地登录路由器的管理界面里，去看它被分配到的新IP是什么，然后用terminal重连，太麻烦了，不是么？作为一个树莓派玩家，这种麻烦简直是无法接受的！<br />
<span id="more-7858"></span><br />
为了解决这个问题，我让Pi开机的时候，自动向我指定的Email发送一封邮件，告诉我它此次开机时的IP地址。<br />
步骤：<span style="color:#0000ff;">开机时执行一个脚本，检测网络可用性&rarr;网络通畅后获取自己的IP地址&rarr;发送邮件到指定的邮箱。</span><br />
下面一一道来。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『1』</span>开机启动项<br />
开机执行一个脚本是怎么做到的？<br />
在<a href="http://www.codelast.com/?p=4945" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">这篇文章</span></a>的第2条里，详细陈述了如何让&nbsp;/etc/rc.local 成为开机执行的文件，搞定之后，我只需要向此文件中添加一句话，即可开机执行一个脚本了：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
# send a mail to notify the IP address of Pi 
/root/data/source/send-ip-mail.sh &gt;&gt; /root/data/source/send-ip-mail.log 2&gt;&amp;1
</pre>
<p><span style="background-color: rgb(0, 255, 0);">『2』</span>上报IP地址的脚本实现<br />
send-ip-mail.sh脚本的内容如下：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#!/bin/bash

# check network availability 
while true
do
    TIMEOUT=5
    SITE_TO_CHECK=&quot;www.126.com&quot;
    RET_CODE=`curl -I -s --connect-timeout $TIMEOUT $SITE_TO_CHECK -w %{http_code} | tail -n1`
    if [ &quot;x$RET_CODE&quot; = &quot;x200&quot; ]; then
	echo &quot;Network OK, will send mail...&quot;
	break
    else
	echo &quot;Network not ready, wait...&quot;
	sleep 1s
    fi
done

# get the IP address of eth0, e.g. &quot;192.168.16.5&quot; 
ETH0_IP_ADDR=`ifconfig eth0 | sed -n &quot;2,2p&quot; | awk &#39;{print substr($2,1)}&#39;`

# send the Email 
echo &quot;Current time: `date &#39;+%F %T&#39;`. Enjoy it&quot; | mutt -s &quot;IP Address of Raspberry Pi: $ETH0_IP_ADDR&quot; xxx@gmail.com
</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
脚本很简单，分为3部分：第一部分检测网络可用性；第二部分取树莓派的eth0网卡的IP地址；第三部分发送邮件到指定的Email。<br />
其中，第一部分是必须要有的，因为经过我试验，在本脚本执行时，树莓派的网络还没有初始化好，此时你直接发邮件是发不出去的。在这里我通过访问www.126.com来确定网络可用性。<br />
第三部分需要你预先配置好mutt和msmtp。</p>
<p><span style="background-color: rgb(0, 255, 0);">『3』</span>安装配置mutt和msmtp<br />
配置好mutt和msmtp后，就可以像上面一样，通过一句代码将邮件发送出去。<br />
首先要在Pi上安装mutt和msmtp：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
pacman -S msmtp
pacman -S mutt
</pre>
<p>安装后，先配置msmtp。在你用户的根目录下创建文件&nbsp;.msmtprc，内容如下：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
account default
host smtp.126.com
from xxx@126.com
auth plain
user xxx@126.com
password your_password
logfile /var/log/msmtp.log
</pre>
<p>其中，smtp.126.com是我使用的邮箱的SMTP服务器地址，xxx@126.com是我用于发送邮件的邮箱，your_password是邮箱密码，你要根据你的情况修改。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
然后配置mutt。在你用户的根目录下创建文件&nbsp;.muttrc，内容如下：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set sendmail=&quot;/usr/bin/msmtp&quot;
set use_from=yes
set realname=&quot;Alarm&quot;
set editor=&quot;vim&quot;
</pre>
<p>其中，realname是发件人的名字，接收到的邮件中会显示出来。</p>
<p><span style="background-color: rgb(0, 255, 0);">『4』</span>至此全部搞定，以后每次Pi开机的时候，就会&ldquo;自报家门&rdquo;，我们再也不愁找不到Pi啦！</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushShell.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushPlain.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e8%ae%a9%e6%a0%91%e8%8e%93%e6%b4%be%e8%87%aa%e5%8a%a8%e4%b8%8a%e6%8a%a5ip%e5%9c%b0%e5%9d%80%e5%88%b0%e9%82%ae%e7%ae%b1let-raspberry-pi-to-report-its-ip-address-via-email/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
