I am a bit new to Python, but an experienced programmer. I am writing a Python 2.7 script which should be started by a Linux server at boot. The purpose is to send, via various mediums, notification(s) when a user connects to the server.
My problem has to do with the actual listening. How can I make the module see when a user connects (via SSH, or whatever) to the server? Some quick pseudocode:
# When a connection is made
# Send an email
# Send a text message
# Send notification to server output
# Etc...
I'd like to include some details in the notification, like username, IP, connection time, last connection, a cute custom connection message, whatever. Any protips on how to do those things best is appreciated as well, but I'm sure I can figure this out elsewhere.
Thanks in advance for any guidance!
If your sshd is using syslog, you could configure syslog to send the auth facility to a named pipe, then write a Python script to read the FIFO. Here's an example that uses bash.
Or, like sblom said, you could tail /var/log/auth.log in a Python script. Either way, you should get lines like this:
Mar 29 19:58:13 mybox sshd[13914]: Accepted password for jtg from 192.168.0.20 port 51538 ssh2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With