The Engagement 4Cast
-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
In your Ubuntu server create a file “login. py”
This is the code that you will place in the file:
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”)
SFTP into your server and go to the /etc/profile.d/ folder
Create a .sh file and in the file type “python3 login.py” (If there is a permissions issue just ssh into the server and add your .sh file there)
Now whenever someone logs into your server a message to slack will be sent
In your server create a file “logout.py”
This is the code that you will place in the file:
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")
SFTP into your server and go to the /home/ubuntu/ folder (Note: This step will need to be repeated for each user’s log out that you want to monitor)
Make sure that you can view hidden files
Open the .bash_logout file and add “python3 logout.py” directly above the existing code (If there is a permissions issue just ssh into the server and edit the .bash_logout file there)
Now when someone logs out of the server a message will be sent to slack
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.