树莓派开机启动,获取本地IP地址和外网IP地址;可以实现配置反向代理或者隧道实现远程访问;
1、配置邮件,见我之前分享的链接;
2、编写获取IP地址的脚本sendip.sh
#!/bin/bash
#check network availability
#当前时间
CURRENT_TIME=$(date "+%Y-%m-%d %H:%M:%S")
#读取设备hostname
HOSTNAME=$(hostname)
#日志记录目录
LOG="/home/pi/Program/log/sendip.log"
while true
do
RET_CODE=$(curl -I -s --connect-timeout 5 www.baidu.com -w %{http_code} | tail -n1)
if [ $RET_CODE = "200" ]; then
echo $CURRENT_TIME "Network OK, will send mail..." >$LOG 2>&1
break
else
echo $CURRENT_TIME "Network not ready, wait..." >$LOG 2>&1
sleep 1s
fi
done
# 获取外网IP
OUT_SIDEIP=$(curl -s -retry 10 -retry-delay 2 myip.ipip.net)
echo $CURRENT_TIME "外网地址:"$OUT_SIDEIP >$LOG 2>&1
# 获取内网IP
INSIDE_IP=$(LC_ALL=C ifconfig | grep "inet addr:" | grep -v "127.0.0.1" | cut -d: -f2 | awk '{print $1}')
INSIDE_IP2=$(LC_ALL=C ifconfig | grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" | grep -v "127.0.0.1" | cut -d: -f2 | awk '{print $2}' | head -n 1)
if [ -z $INSIDE_IP ]; then
INSIDE_IP=$INSIDE_IP2
echo $CURRENT_TIME "内网地址:"$INSIDE_IP >$LOG 2>&1
fi
# send the Email
echo -e "设备ID:"$HOSTNAME"\n当前时间:"$CURRENT_TIME"\n外网IP:"$OUT_SIDEIP"\n内网IP:"$INSIDE_IP | mutt -s "电子秤 $HOSTNAME 开机 IP地址上报" test@qq.com
3、给脚本添加可执行权限
chmod 777 sendip.sh