|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2016-12-5 09:42:41
|
显示全部楼层
----------------------------------------------------------------------------
8. 【让树莓派开机自动将自己IP地址发到指定邮箱的方法】
(这里是利用inkdot11@163.com这个邮箱将树莓派IP发给inkdot11@163.com和shuangfeiyanworld@163.com这个邮箱里)
只需下面两步:
1. 建立Python脚本(脚本内容请看附录):sudo nano /home/pi/send_ip.py
2. 设置开机自启动:在/etc/rc.local中加入下面一行
/usr/bin/python /home/pi/send_ip.py
附录(Python脚本内容)(只需根据需要修改最后一行''之间的部分即可,注意:单引号符号不能删掉):
#/bin/env python
# -*-coding:utf8-*-
import socket
import fcntl
import time
import struct
import smtplib
import urllib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
#发送邮件的基本函数,参数依次如下
# 'smtp服务器地址','发件人邮箱用户名','发件人邮箱密码','发件人邮箱地址',['收件人1邮箱地址','收件人2邮箱地址'],'邮件主题',邮件html内容
def sendEmail(smtpserver,username,password,sender,receiver,subject,msghtml):
msgRoot = MIMEMultipart('related')
msgRoot["To"] = ','.join(receiver)
msgRoot["From"] = sender
msgRoot['Subject'] = subject
msgText = MIMEText(msghtml,'html','utf-8')
msgRoot.attach(msgText)
#sendEmail
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
# 检查网络连同性
def check_network():
while True:
try:
result=urllib.urlopen('http://baidu.com').read()
print result
print "Network is Ready!"
break
except Exception , e:
print e
print "Network is not ready,Sleep 5s...."
time.sleep(5)
return True
# 获得本级制定接口的ip地址
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("1.1.1.1",80))
ipaddr=s.getsockname()[0]
s.close()
return ipaddr
if __name__ == '__main__':
check_network()
ipaddr=get_ip_address()
sendEmail('smtp.163.com','inkdot11','24202420','inkdot11@163.com',['shuangfeiyanworld@163.com','inkdot11@163.com','562949252@qq.com'],'主人您好,小派给您请安了,请点击查看我当前的IP地址',ipaddr)
----------------------------------------------------------------------------
9. 【VNC远程登录树莓派的图形界面】
a> 安装tightvncserver:sudo apt-get install tightvncserver
b> 设置vnc远程登录密码:vncpasswd (先输入操作密码两次,然后会询问是否设置一个查看(view-only)密码,按自己喜欢,一般没必要。)
c> 设置开机启动:sudo nano /etc/init.d/tightvncserver
内容如下:
#!/bin/sh
### BEGIN INIT INFO
# Provides: tightvncserver
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop tightvncserver
### END INIT INFO
# More details see:
# http://www.penguintutor.com/linux/tightvnc
### Customize this entry
# Set the USER variable to the name of the user to start tightvncserver under
export USER='pi'
### End customization required
eval cd ~$USER
case "$1" in
start)
# 启动命令行。此处自定义分辨率、控制台号码或其它参数。
su $USER -c '/usr/bin/tightvncserver -depth 16 -geometry 800x600 :1'
echo "Starting TightVNC server for $USER "
;;
stop)
# 终止命令行。此处控制台号码与启动一致。
su $USER -c '/usr/bin/tightvncserver -kill :1'
echo "Tightvncserver stopped"
;;
*)
echo "Usage: /etc/init.d/tightvncserver {start|stop}"
exit 1
;;
esac
exit 0
d> 加执行权限,并更新开机启动列表: sudo chmod 755 /etc/init.d/tightvncserver
sudo update-rc.d tightvncserver defaults
e> 开启vnc服务:vncserver
f> 已经可以vnc远程登录. |
|