如何选择在unix

时间:2017-10-07 22:18:16

标签: bash shell unix

编写一个shell脚本(在Bourne shell上运行),它运行一个无限循环来监视拒绝列表上的哪些用户多次登录UNIX系统。

  1. 拒绝列表必须存储在名为user.deny的文本文件中,该文件列出了不允许多个登录的用户,每行一个用户名。
  2. 在脚本的开头,您需要检查当前目录下是否存在user.deny文件,如果没有,您的脚本会显示一条消息,然后退出。
  3. 每隔3秒,脚本必须显示一条警告消息,以报告拒绝列表中多次登录的用户。在警告消息中显示用户的全名,而不是他/她的用户名。
  4. 您的脚本无需处理用户登录然后立即注销的情况(即在3秒的时间间隔内)。
  5. 此任务的脚本必须命名为mlog.sh。以下是脚本的示例输出(如果脚本在最终中断时留下临时文件,则可以)。 $是shell提示符。

    $ ./mlog.sh
    No user on the user.deny list has multiple logins
    No user on the user.deny list has multiple logins
    The user John Smith (on the denial list) has logged in more than once!
    The user Adam Jones (on the denial list) has logged in more than once!
    No user on the user.deny list has multiple logins
    No user on the user.deny list has multiple logins
    The user David Monks (on the denial list) has logged in more than once!
    The user Nick Andrews (on the denial list) has logged in more than once!
    

    请记住,不要使用root帐户权限,作为学生,我们没有root权限,我们可以使用who,users,w,finger来监控登录系统的用户。

    w:

    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    juanz    pts/1    myvpn-35-37.stud 07:34    1:09m  0.20s  0.20s -bash
    haydenj2 pts/0    myvpn-p32-107.st 07:23    0.00s  0.90s  0.90s -bash
    fhe2     pts/2    xxxxxxxxxxxxxx-e 08:04    6:39   0.14s  0.01s nano menu.sh
    chanpw   pts/3    myvpn-33-186.stu 07:51    0.00s  0.14s  0.06s joe cp2.sh
    fhe2     pts/4    10-200-250-132-e 08:19   35:56   0.08s  0.08s -bash
    zijianc  pts/5    myvpn-33-162.stu 02:36    6:33m  0.05s  0.05s -bash
    zijianc  pts/6    myvpn-33-162.stu 02:35    6:32m  0.10s  0.02s nano 
    

    我不知道如何使用该表来显示......

    期待您的回复!

1 个答案:

答案 0 :(得分:0)

哈哈,这是我的代码:

if [-f user.deny];然后     手指| awk' {w [$ 2" " $ 3] + = 1} END {for(a in w)if(w [a]> 1)print a}' > user.deny

虽然如此; 做     手指| awk&#39; {w [$ 2&#34; &#34; $ 3] + = 1} END {for(a in w)if(w [a]&gt; 1)print a}&#39; &gt; user.deny |读线时     做     #finger | awk&#39; {w [$ 2&#34; &#34; $ 3] + = 1} END {for(a in w)if(w [a]&gt; 1)print a}&#39; &GT; user.deny     FN = wc -l<user.deny;     如果[$ FN -eq 0];那么     echo&#34; user.deny列表中没有用户有多个登录&#34 ;;     否则
    user.deny列表中的echo&#34; $ line用户有多个登录&#34 ;;     #sleep 3     科幻     完成&lt; user.deny DONE 其他     echo&#34; no user.deny,退出&#34 ;;     出口; 网络