今天就把包裡放置已久的樹莓派(Raspberry Pi)給加工一下!
目標:
1.無螢幕的主機,使用ssh控制(開機時傳送Pi的 ip)。
2.偽裝成家裡蘋果群的的TimeMachine
3.裝上如果攜帶時,要變成無線AP
食材:
Raspberry Pi (B) 基本安裝好 RASPBIAN
USB HDD 已經格式化成 HFS+
作業一:
已經安裝好ssh的pi,雖然可以運作,可是不曉得網卡的ip,所以寫個script,在每次開機時,把pi的位址,用gmail傳送出來。紅色的部分請依照自己的需求修改。
不妨放在家目錄的Code下(例如/home/pi/Code/startup_mailer.py)
sudo nano /home/pi/Code/startup_mailer.py
import subprocess import smtplib import socket from email.mime.text import MIMEText import datetime # 這裏改成自己的email to = 'example.com' gmail_user = 'test@gmail.com' gmail_password = 'gmail的密碼' smtpserver = smtplib.SMTP('smtp.gmail.com', 587) smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo smtpserver.login(gmail_user, gmail_password) today = datetime.date.today() # Very Linux Specific arg='ip route list' p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE) data = p.communicate() split_data = data[0].split() ipaddr = split_data[split_data.index('src')+1] my_ip = 'Your ip is %s' % ipaddr msg = MIMEText(my_ip) msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y') msg['From'] = gmail_user msg['To'] = to smtpserver.sendmail(gmail_user, [to], msg.as_string()) smtpserver.quit()
可以執行看看喔
python /home/pi/Code/startup_mailer.py就會寄一封信,到你指定的信箱中,告訴你的Pi流浪到哪裡去了!
然後把上面的指令加到 /etc/rc.local,就搞定了,原本開機時,螢幕就會提示目前的ip,然後再寄出信件告訴自己Pi的位址:
sudo nano /etc/rc.local
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address if it doesn't work ad sleep 30 before all your code
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
python /home/pi/Code/startup_mailer.py &
fi exit 0
這樣,隨時就可以附近的電腦已SSH控制Pi
參考:http://elinux.org/RPi_Email_IP_On_Boot_Debian
您好想請教...若要取得外部ip該如何改
回覆刪除