查看谁登录了 LibreSwan VPN

查看谁登录了 LibreSwan VPN

我们最近设置了一个 LibreSwan VPN,它大部分时间都运行良好。我们遇到的一个问题是尝试查看当前登录的用户。我找到了一个解决方案,可以查看活跃解决方案的数量https://lists.openswan.org/pipermail/users/2011-January/020042.html但实际上并不能看到谁登录到了会话(删除 grep 会提供很多详细信息,包括连接的 IP,但不包括实际的用户名)。

我目前的想法是 grep /var/log/messages 来查找连接并报告没有附带断开连接消息的连接。PPPD 很好用,可以用 PID 标记所有内容,这使得它变得有点简单,但我希望有更好的方法。

答案1

以防其他人遇到这种情况,下面是我最终使用的脚本。它在 /var/log/messages 中搜索登录消息,然后检查 PID 是否仍在由 pppd 使用。它仅输出用户名和登录时间。

#!/bin/bash
# Process each log in message
grep "logged in" /var/log/messages | grep pppd | while read -r line ; do
    #echo "$line"  # Useful for debugging
    # Extract the PID
    pid=$(echo $line | cut -d "[" -f2 | cut -d "]" -f1)
    user=$(echo $line | grep -o -P '(?<=user ).*(?= logged)')
    #echo $pid  # Useful for debugging
    # See if the PID is still in use
    ps aux | grep pppd | grep $pid  > /dev/null
    disconnectCheck=$?
    # If it is in use report the user who logged in as active
    if [[ $disconnectCheck -eq 0 ]] ; then
        echo "$user logged in at ${line:0:15}"
    fi
done

答案2

我将其添加到我的 /etc/ppp/options.xl2tpd:

plugin /usr/lib64/pptpd/pptpd-logwtmp.so

(我的服务器也安装了 PPTPD - 但已禁用。)

有了这个,我可以做到:

last | grep ppp | grep "still logged in"

唯一的缺点是,与 pptp 会话不同,它不会记录连接客户端的 IP 地址。

相关内容