Login/Logout Slack Notifications from an Ubuntu Server

The Engagement 4Cast

Login/Logout Slack Notifications from an Ubuntu Server

Blog

08/27/2018

Grant Commodore

Do you want to be notified when someone logs in or out of your server? This step by step guide will tell you exactly how to make it happen!

Prerequisites:

-Ubuntu Web server with the latest version of Python installed

-An application that allows one to SSH into a server

-WINSCP (optional)

-A Slack account

-A slack incoming webhook

Step 1: Login Notification to Slack

import sys

import getpass

def send_message_to_slack(text):

  from urllib import request, parse

  import json

  post = {"text": "{0}".format(text)}

  try:

      json_data = json.dumps(post)

      req = request.Request("Your Slack URL goes here",

                            data=json_data.encode('ascii'),

                            headers={'Content-Type': 'application/json'})

      resp = request.urlopen(req)

  except Exception as em:

      print("EXCEPTION: " + str(em))

send_message_to_slack(getpass.getuser() + “ has logged in to the server”)

Step 2: Logout Notification to Slack

import sys

import getpass

def send_message_to_slack(text):

  from urllib import request, parse

  import json

  post = {"text": "{0}".format(text)}

  try:

      json_data = json.dumps(post)

      req = request.Request("Your Slack URL goes here",

                            data=json_data.encode('ascii'),

                            headers={'Content-Type': 'application/json'})

      resp = request.urlopen(req)

  except Exception as em:

      print("EXCEPTION: " + str(em))

send_message_to_slack(getpass.getuser() + " has logged out of the server")
 
About the Engagement 4Cast

4Site Interactive Studios is a talented troupe of web professionals who are passionate about creating tools to support digital marketers. We love to hear from our community! Reach out to us with your thoughts and questions. And don’t forget to subscribe below to get notified when we post new blogs – no spam, just content👍🏼

Subscribe & stay ahead of the crowd with sage marketing tips and predictions.

How can we help?