How can I listen and send server messages (SSH) through a Python script?

I am a little new to Python, but an experienced programmer. I am writing a Python 2.7 script that should be run by the Linux server at boot time. The goal is to send notifications (s) through various media when a user connects to the server.

My problem is with the actual listening. How can I make the module see when the user connects (via SSH or something else) to the server? Some quick pseudo codes:

# When a connection is made # Send an email # Send a text message # Send notification to server output # Etc... 

I would like to include some details in the notification, such as username, IP, connection time, last connection, nice connection message, whatever. Any gaps on how to do these things best are also appreciated, but I'm sure I can figure it out elsewhere.

Thank you in advance for any guidance!

+7
source share
2 answers

If your sshd uses syslog, you can configure syslog to send the auth object to the named pipe, and then write a Python script to read the FIFO. Here is an example that uses bash.

Or, as sblom said, you could tail / var / log / auth.log in a Python script. In any case, you should get these lines:

 Mar 29 19:58:13 mybox sshd[13914]: Accepted password for jtg from 192.168.0.20 port 51538 ssh2 
+3
source

pam_python allows you to write a PAM module that can notify you of authentication attempts for any service that uses PAM.

+2
source

All Articles