Based python3 + Raspberry Pi boot from the start to send and wifi ip information

background:Each time you run raspberry pie, do not know the ip address and wifi information, it is troublesome. If implemented by the script automatically sent to the mailbox ip and wifi information, it is very convenient. This brings me through access to information, write this script, effective pro-test, a friend in need can learn to use.

Users only need to modify the mailbox number, email account name and authorization code can stmp .

# 导入库
import yagmail
import time
import urllib.request
import subprocess
#检查网络连通性
def check_network():
    while True:
        try:
            result=urllib.request.urlopen('http://www.baidu.com')
            print (result)
            print ("Network is Ready!")
            break
        except Exception as e:
            print (e)
            print ("Network is not ready,Sleep 5s...")
            time.sleep(5)
    return True
#运行iwconfig命令行,返回信息
def iwconfig():
    result=subprocess.getoutput('iwconfig') 
    return result
#运行ifconfig命令行,返回信息
def ifconfig():
    result=subprocess.getoutput('ifconfig') 
    return result
#发送邮件
def sendEmail():
    check_network()
    yag = yagmail.SMTP(
        user = "[email protected]",   #发件人邮箱
        password='**************',  #授权码
        host = 'smtp.qq.com')
    #邮件内容
    contents = [ifconfig(), iwconfig()]
    yag.send(to = '[email protected]',#收件人邮箱
             subject = 'IP_config',  #邮件主题
             contents = contents)

if  __name__ == '__main__' :
    sendEmail()

qq stmp authorization code is found inside the mailbox, the steps of: "Settings"> "Account", as shown in the FIG decline found. Click the "authorization code", then you need to verify, validate authorization code will copy it.

Note: Copy the code after pasting, no spaces between the letters.
For example: "tyeb yyew weew refs", after the paste should be "tyebyyewweewrefs"
In the "Settings" find accounts
Set Raspberry Pi boot from the start of its code. Creating autoIP.py file in the root directory of here, of course, you can develop your own name.
Here Insert Picture Description
Use sudo vim /etc/rc.local command, as shown in FIG modify the code, can be saved. (Note that your path to the right).
I used here is vim, you can use nano, personal habits, right, vim not introduce how to use.

Here Insert Picture Description
Can then restart the system, the effect is as follows.

Published 15 original articles · won praise 9 · views 2654

Guess you like

Origin blog.csdn.net/qq_38413498/article/details/101553572