简体   繁体   中英

How can I listen for and report server (SSH) connections via a Python script?

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

pam_python将允许您编写一个PAM模块,该模块可以通知任何使用PAM的服务的身份验证尝试。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM