I decided I wanted some sort of notification that would let me know when a user has successfully logged into my RaspberryPi (running Raspbian). The following explains how to get push notifications sent to your phone (via Pushover) on successful SSH connections.
The first step is to get Pushover set up. Visit https://pushover.net and if you don’t already have an account, you will need to create one. Log into your account and make a note of your user key (you will need this later). Once you have your account set up, click on Register an Application towards the bottom of the page. On this page, you are required to give your new application a name, a type (in this case, chose Script), and optionally you can fill out a description, URL, and give it an icon. Accept the terms of service and click Create Application. On the following page, take a note of your API key for this application. Now that we have a unique application set up, we can go ahead and create a python script on the RaspberryPi.
On my RaspberryPi I decided to create a new directory and keep all my scripts in one place:
You can use the editor of your choice to create a new file (call it something like
Copy and paste the following code (some of this code is adapted from the official Pushover library found here):
The final step we need to complete is running this script every time any user logs in via SSH. In order to do this we must use
/etc/bash.bashrc as it is included by
~/.bashrc, and read every time a shell starts up. If we used
~/.bashrc then only the current user would run this script, whereas we want any user to run this script. Edit it by:
Add the following:
You can test this script by closing your current SSH session and initiating a new one. You must of course have Pushover installed on your iPhone or Android device, but you should now get a push notification letting you know that you have logged into your RaspberryPi.
And that’s it. Please let me know if there’s any more efficient or generally better ways of going about this. I wasn’t too familiar with all of the different
.bashrc, etc. so there could potentially be a better way to go about this.