Skip to content

Instantly share code, notes, and snippets.

@carry0987
Last active September 19, 2018 01:58
Show Gist options
  • Save carry0987/4b8a221dbc0e3a18b1a9b8b4ccb6223d to your computer and use it in GitHub Desktop.
Save carry0987/4b8a221dbc0e3a18b1a9b8b4ccb6223d to your computer and use it in GitHub Desktop.
RPi3-Auto-Email-New-Public-IP-If-Changed-Python3

RPi3 Auto Email New Public IP If Changed (Python3)

Step

  1. Use wget to download IP-Reporting script

    sudo wget https://raw.github.com/carry0987/Raspberry-Pi-Repo/master/Auto-Report-IP/report-ip.py
    

  2. Give the script executing permission

    sudo chmod +x report-ip.py
    

  3. Enter your gmail address, password and receiver email address to the script

    sudo vim report-ip.py
    

    Note : If your gmail have set Two-Step Verification, you should follow the step to get token:

    1. Log-in into Gmail with your account
    2. Navigate to https://security.google.com/settings/security/apppasswords
    3. In 'select app' choose 'custom', give it an arbitrary name and press generate
    4. It will give you 16 chars token
    5. Use the token as password with your Gmail account and two factor authentication will not be required.
  4. Move report-ip.py to usr folder

    sudo mv report-ip.py /usr/local/bin
    

  5. Create a systemd service to make the script will auto be run on boot It can auto check the network connection make sure the network is online so that it can send the email

    sudo vim /etc/systemd/system/report-ip.service
    

  6. Put the following script into the report-ip.service,then save and exit

    [Unit]
    Description=IP Reporter
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    ExecStart=/usr/bin/python3 /usr/local/bin/report-ip.py
    
    [Install]
    WantedBy=multi-user.target
    

  7. Enable the service

    sudo systemctl enable report-ip
    

  8. Start the service

    sudo systemctl start report-ip
    

  9. Use systemctl to check the status of the service

    sudo systemctl status report-ip
    

  10. Now edit crontab to make the script run every minutes

    sudo vim /etc/crontab
    

  11. Add the following script on the bottom, then save and exit

    * * * * * root /usr/bin/python3 /usr/local/bin/report-ip.py
    

  12. Restart cron service

    sudo service cron restart
    

  13. Finish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment