树莓派开机自启的方法(解决rc.local开机步启动问题)
来源:互联网 发布:windows 10镜像文件img 编辑:程序博客网 时间:2024/04/29 09:40
问题起源:
知道隔壁老王家的wifi 密码 提前在树梅派设置好了wifi帐号和密码,但是竟然进不了路由器管理页面。于是就不知道树梅派在局域网的ip这就非常尴尬了。然后呢!就弄个开机自动发送ip到我邮箱的脚本。于是就发生了下面的事情
发送邮件的脚本:
1.设置脚本
# -*- coding: utf-8 -*-if __name__ == '__main__': print "这是配置文件,不能单独运行"#设置收件邮箱toaddrs = 'xxxxxxxxxx@163.com'#设置发送邮箱fromaddr = 'xxxxxxxxxxx@163.com' #设置发送邮箱的账号密码username = 'xxxxxxxxx@163.com' password = 'xxxxxxxxxxxxxxx'#设置SMTP服务器、端口,根据你的邮箱设置,smtp_sever = 'smtp.163.com:25'
2.发送邮件获取ip脚本
#!/usr/bin/env python# -*- coding: utf-8 -*-#version <= python 2.7#__author__ = 'Page Wong'import arrayimport structimport socketimport fcntlfrom datetime import datetimeimport smtplibfrom email.mime.text import MIMETextdef get_ip_address(): ''' #获取IP地址功能# ''' #先获取所有网络接口 SIOCGIFCONF = 0x8912 SIOCGIFADDR = 0x8915 BYTES = 4096 sck = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) names = array.array('B',b'\0' * BYTES) bytelen = struct.unpack('iL', fcntl.ioctl(sck.fileno(), SIOCGIFCONF, struct.pack('iL', BYTES, names.buffer_info()[0])))[0] namestr = names.tostring() ifaces = [namestr[i:i+32].split('\0', 1)[0] for i in range(0, bytelen, 32)] #再获取每个接口的IP地址 iplist = [] for ifname in ifaces: ip = socket.inet_ntoa(fcntl.ioctl(sck.fileno(),SIOCGIFADDR,struct.pack('256s',ifname[:15]))[20:24]) iplist.append(ifname+':'+ip) return iplistdef ip_save_file(iptxt): ''' #存入IP地址到文件# 直接发邮件不需读取次文件 后续通过其它途径传送IP地址时用到 ''' #获取当前时间,因为每次启动IP都在变,记录上时间容易区分 now = datetime.now().strftime('%Y-%m-%d %H:%M:%S') #写入文件 with open('ipaddress.txt','w') as f: f.write(now+'\r\n') for ip in get_ip_address(): f.write(ip+'\r\n')def ip_send_mail(iptxt): ''' #发送IP地址到制定邮箱# ''' #导入邮件账户配置文件 import config as mail fromaddr = mail.fromaddr toaddrs = mail.toaddrs username = mail.username password = mail.password server = smtplib.SMTP(mail.smtp_sever) #设置邮件正文,get_ip_address()返回的是list,要转换成str ip = '\r\n'.join(iptxt) #设置邮件标题和正文 msg = MIMEText(ip,'plain', 'utf-8') msg['Subject'] = 'IP For RaspberryPi' msg['From'] = fromaddr msg['To'] = toaddrs #启动SMTP发送邮件 server.ehlo() server.starttls() server.login(username,password) server.sendmail(fromaddr, toaddrs, msg.as_string()) server.quit()def main(): ''' #程序入口# ''' #获取IP iptxt = get_ip_address() #将IP存入文件,如果直接发送邮件,这步可以省略。 ip_save_file(iptxt) #将IP地址发送到指定邮箱 ip_send_mail(iptxt)if __name__ == '__main__': ''' #运行 ''' main()
这里什么都不用改了。
下面就是开机启动了。一开始用了很通用的办法在rc.local里面加入python /home/pi/get-ip-address.py
结果呢开机没有启动 至于为什么我现在也不清楚
于是我就用了另外一种办法
先给py脚本加上权限sudo chmod +x /home/pi/get-ip-address.py
然后保存一个开机启动脚本保存脚本为/etc/init.d/get-ip-address文件
#!/bin/sh#/etc/init.d/get-ip-address### BEGIN INIT INFO# Provides: get-ip-address# Required-Start: $remote_fs $syslog# Required-Stop: $remote_fs $syslog# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: get-ip-address# Description: This service is used to start my get-ip-address### END INIT INFOcase "$1" in start) echo "Starting get-ip-address" nohup python /home/pi/get-ip-address.py & ;; stop) echo "Stop" #kill $( ps aux | grep -m 1 'python /home/pi/get-ip-address.py' | awk '{ print $2 }') ;; *) echo "Usage: service get-ip-address start|stop" exit 1 ;;esacexit 0
保存之后添加权限sudo chmod +777 /etc/init.d/get-ip-address
加到开机启动sudo update-rc.d get-ip-address defaults
原创:http://www.jianshu.com/p/1a160067d8fd
1 0
- 树莓派开机自启的方法(解决rc.local开机步启动问题)
- 树莓派的rc.local文件(设置开机启动)
- 没有/etc/rc.d/rc.local最方便的设置开机自启的方法
- 解决centos7 开机/etc/rc.local 不执行的问题
- 树莓派Debian开机启动项(rc.local)
- Ubuntu 创建开机启动脚本的一种方法(rc.local)
- Update-rc.d && rc.local管理Ubuntu的开机启动
- 树莓派通过rc.local实现开机自启失败的原因
- 解决Linux开机启动文件rc.local不能自动执行
- linux的开机启动过程(rc.local、init.d、rc.x、init的作用)
- centos7/etc/rc.local不开机自启
- centos开机自动启动SVN服务的方法1、编辑rc.local文件
- 开机启动服务chkconfig开机启动脚本/etc/rc.local
- ubuntu开机启动rc.local无效
- linux rc.local 开机自启 rc.local中的命令不能自动运行
- centos7 开机/etc/rc.local 不执行的问题
- centos7 开机/etc/rc.local 不执行的问题
- centos7 开机/etc/rc.local 不执行的问题
- 用JSONObject解析和处理json数据
- Android的monkey用法
- Eclipse中.setting目录下文件介绍
- 修改MP4 Parser 生成Jar包
- Java中元组的使用
- 树莓派开机自启的方法(解决rc.local开机步启动问题)
- whoami and id -ru
- 【微信小编】螺旋图
- css--5.浮动
- 如何使用batch-import工具向neo4j中导入海量数据
- codeforces 474D DP
- Openstack新建云主机的流程
- 五大经典算法(二)动态规划算法
- 查询设备密码文件