通过电子邮件在启动时发送raspberry pi IP

时间:2015-10-23 23:08:42

标签: python raspberry-pi crontab raspbian

我正在尝试让我的RP可以通过WIFI访问,但我无法控制我的路由器(我住在学生宿舍)而且我无法配置静态IP,所以我编写了一个python脚本,获取当前的IP并将其发送到我的Gmail帐户。 然后我将脚本添加到crontab

这是脚本:

#!/usr/bin/python

import socket
import struct
import fcntl

import smtplib
from email.mime.text import MIMEText

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', 'wlan0'[0:15])[20:24]))

msg = MIMEText(ip)

msg['Subject'] = 'RP IP'
msg['From'] = '****@gmail.com'
msg['To'] = '****@gmail.com'

try:
       mail = smtplib.SMTP('smtp.gmail.com', 587)

        #Login to SMTP server
        mail.starttls()
        mail.login('****@gmail.com', my_pass)

        mail.sendmail(msg['From'], msg['To'], 'Subject: RP IP\n\nThe Current IP 
        mail.quit

        print "Success!"
except SMTPException:
        print "Error"

问题是脚本返回错误:

Traceback (most recent call last):
  File "/home/pi/Programming/python/send_ip_by_mail.py", line 11, in <module>
    ip = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', 'wlan0'[0:15])[20:24]))
IOError: [Errno 99] Cannot assign requested address

当我自己运行脚本时,它工作正常,但在启动时却没有。 有什么问题?

谢谢!

0 个答案:

没有答案