# 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
    ```

    ![](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LTlDC2gRIuHSiDNZUjc%2F-LTlDDODSEYntpFMmIUA%2Fimg_1.jpg?generation=1544866561768348\&alt=media)
2.  Give the script executing permission

    ```
    sudo chmod +x report-ip.py
    ```

    ![](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LjEd9YiU7c_ajC4muSY%2F-LjEdC7FM4f8hQ2oG4rV%2Fimg_2.jpg?generation=1562556822997910\&alt=media)
3.  Enter your gmail address, password and receiver email address to the script

    ```
    sudo vim report-ip.py
    ```

    ![](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LjEd9YiU7c_ajC4muSY%2F-LjEdC7HCHfWBi09hfkK%2Fimg_3.jpg?generation=1562556817289807\&alt=media) **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
    ```

    ![](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LjEd9YiU7c_ajC4muSY%2F-LjEdC7JQCwllcqOleMO%2Fimg_4.jpg?generation=1562556818224316\&alt=media)
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
    ```

    ![](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LjEd9YiU7c_ajC4muSY%2F-LjEdC7N-O6otOiyj5pw%2Fimg_5.jpg?generation=1562556817236959\&alt=media)
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
    ```

    ![](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LTlDC2gRIuHSiDNZUjc%2F-LTlDDONO8dYoGa_Mgnq%2Fimg_6.jpg?generation=1544866561668494\&alt=media)
7.  Enable the service

    ```
    sudo systemctl enable report-ip
    ```

    ![](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LjEd9YiU7c_ajC4muSY%2F-LjEdC7RrV37pOTb0wcY%2Fimg_7.jpg?generation=1562556817229269\&alt=media)
8.  Start the service

    ```
    sudo systemctl start report-ip
    ```

    ![](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LjEd9YiU7c_ajC4muSY%2F-LjEdC7TS08ncz5g4RAJ%2Fimg_8.jpg?generation=1562556823420612\&alt=media)
9.  Use `systemctl` to check the status of the service

    ```
    sudo systemctl status report-ip
    ```

    ![](https://github.com/carry0987/Raspberry-Pi-Note/blob/master/book_source/article/static/image/09/img_9.jpg)
10. Now edit **crontab** to make the script run every minutes

    ```
    sudo vim /etc/crontab
    ```

    ![](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LjEd9YiU7c_ajC4muSY%2F-LjEdC7VAEc7mtFGfIo4%2Fimg_10.jpg?generation=1562556817754693\&alt=media)
11. Add the following script on the bottom, then save and exit

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

    ![](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LjEd9YiU7c_ajC4muSY%2F-LjEdC7XIwNBKkpJeBOu%2Fimg_11.jpg?generation=1562556818692097\&alt=media)
12. Restart **cron** service

    ```
    sudo service cron restart
    ```

    ![](https://1629731818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTkx9vj8rDQgjbGkVku%2F-LjEd9YiU7c_ajC4muSY%2F-LjEdC7ZBVV5t2-i8lk1%2Fimg_12.jpg?generation=1562556817408898\&alt=media)
13. Finish
