如何检查用户是否断开连接,是否已删除新的断开连接以及进行操作的服务器?

发布于 2025-01-20 07:51:56 字数 120 浏览 0 评论 0原文

我必须创建一个 shell 程序,以便每次某些用户(作为参数给出)从系统连接/断开连接时,如果是新的连接/断开连接以及执行操作的服务器,它将保存在文本文件中。有人可以给我一些想法或使用哪些命令。我很难使用“谁”和“手指”命令。

I have to create a shell program, such that each time certain users (given as parameters) connect/disconnect from the system, it will save in a text file if its a new connection/disconnection and the server on which the action was made. Can someone give me some ideas or which commands to use. I'm having a hard time with the commands 'who' and 'finger'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ι不睡觉的鱼゛ 2025-01-27 07:51:56

我自己解决了

#!/bin/bash

function SaveInfo
{   
    echo "( Last information saved about "$user" )"                  >> LAB6_Log
    echo "The server        Time and Date"                   >> LAB6_Log
    echo " "                                     >> LAB6_Log
    last | grep $user | awk '{print $3"     "$4" "$5" "$6" "$7" "$8" "$9}'   >> LAB6_Log
    echo "____________________"                          >> LAB6_Log
    echo " "                                         >> LAB6_Log
}

function CheckUser
{
    for user in $( who | awk '{print $1}' | uniq )
    do  
        if [ $user == "$1" ]
        then
            return 0
        fi
    done
    return 1
}

clear
echo "+____________________________________________________________+"
echo " "
if [ $# -eq 0 ]
    then
        echo "You did not enter a username. Please enter one... or more, I guess."
    else
        echo "The information about the following users are saved in the file: LAB6_Log "
        for i in $@
        do
            n=0
            m=0
            echo "Supervising this user: "$i" "
            
            while [ $n -le 4 ]
            do
                user=$i
                CheckUser $user
                TheAnswer=$?

                if [ $TheAnswer == 0 ]
                then 
                    n=$((n+1))
                    echo "checking again for "$i"..."
                    sleep 2
                else
                    m=$((m+1))
                    echo "checking again for "$i"..."
                    sleep 2
                fi
                
                if [ $m == 4 ] && [ $n == 0 ]
                then
                    echo "User not found. Probably not existing"
                    break
                fi
                if [ $n == 4 ]
                then
                    echo "Saving information..."
                    echo "____________________NEW CONNECTION" >> LAB6_Log
                    SaveInfo $user
                    echo "Information saved!"
                    break
                fi
                if [ $m == 4 ] && [ $n != 0 ]
                then
                    echo "Something happened. User maybe disconnected..."
                    echo "____________________NEW DISCONNECTION" >> LAB6_Log
                    SaveInfo $user
                    echo "Saved last information possible."
                    break
                fi
            done        
        done
        echo "Exiting..."
                
fi
echo " "
echo "+____________________________________________________________+"   

Solved it myself

#!/bin/bash

function SaveInfo
{   
    echo "( Last information saved about "$user" )"                  >> LAB6_Log
    echo "The server        Time and Date"                   >> LAB6_Log
    echo " "                                     >> LAB6_Log
    last | grep $user | awk '{print $3"     "$4" "$5" "$6" "$7" "$8" "$9}'   >> LAB6_Log
    echo "____________________"                          >> LAB6_Log
    echo " "                                         >> LAB6_Log
}

function CheckUser
{
    for user in $( who | awk '{print $1}' | uniq )
    do  
        if [ $user == "$1" ]
        then
            return 0
        fi
    done
    return 1
}

clear
echo "+____________________________________________________________+"
echo " "
if [ $# -eq 0 ]
    then
        echo "You did not enter a username. Please enter one... or more, I guess."
    else
        echo "The information about the following users are saved in the file: LAB6_Log "
        for i in $@
        do
            n=0
            m=0
            echo "Supervising this user: "$i" "
            
            while [ $n -le 4 ]
            do
                user=$i
                CheckUser $user
                TheAnswer=$?

                if [ $TheAnswer == 0 ]
                then 
                    n=$((n+1))
                    echo "checking again for "$i"..."
                    sleep 2
                else
                    m=$((m+1))
                    echo "checking again for "$i"..."
                    sleep 2
                fi
                
                if [ $m == 4 ] && [ $n == 0 ]
                then
                    echo "User not found. Probably not existing"
                    break
                fi
                if [ $n == 4 ]
                then
                    echo "Saving information..."
                    echo "____________________NEW CONNECTION" >> LAB6_Log
                    SaveInfo $user
                    echo "Information saved!"
                    break
                fi
                if [ $m == 4 ] && [ $n != 0 ]
                then
                    echo "Something happened. User maybe disconnected..."
                    echo "____________________NEW DISCONNECTION" >> LAB6_Log
                    SaveInfo $user
                    echo "Saved last information possible."
                    break
                fi
            done        
        done
        echo "Exiting..."
                
fi
echo " "
echo "+____________________________________________________________+"   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文