in Tech

Push Notifications on successful SSH connections

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:

$ mkdir /my_scripts
$ cd /my_scripts

You can use the editor of your choice to create a new file (call it something like sshpushover.py):

$ nano sshpushover.py

Copy and paste the following code (some of this code is adapted from the official Pushover library found here):

#!/usr/bin/env python

from time import sleep
import os
import pwd
import httplib, urllib

def PushOver(title,message,url):
   app_key = "<your_app_key>"
   user_key = "<your_user_key>"
   #Connect with the Pushover API server
   conn = httplib.HTTPSConnection("api.pushover.net:443")

   #Send a POST request in urlencoded json
   conn.request("POST", "/1/messages.json",
   urllib.urlencode({
   "token": app_key,
   "user": user_key,
   "title": title,
   "message": message,
   "url": url,
   }), { "Content-type": "application/x-www-form-urlencoded" })

   #Any error messages or other responses?
   conn.getresponse()

#App-specific variables
usr=pwd.getpwuid(os.getuid()) [0]
#print "successful ssh login with username: " + usr
PushOver('raspberryPI SSH login','User: '+usr+' has logged in via SSH.','')

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:

nano /etc/bash.bashrc

Add the following:

# send a pushover notification each time the user logs in via SSH
if [[ -n $SSH_CONNECTION ]] ; then
        python /my_scripts/sshpushover.py
fi

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 .bash_profile, .bashrc, etc. so there could potentially be a better way to go about this.

Write a Comment

Comment