0

I needed to write a small script Shell bash script

The task is as follows:

"Develop a program that outputs information about users in the system after a certain time interval: who went in, who went out."

I tried to do this:

#!/bin/bash
while [ true ]; do
    clear
    date
    who
    sleep 5
done

then i tried like this:

#!/bin/bash
while [ true ]; do
    clear
    date
    $USER
    sleep 5
done

But everything is still the same.Only the date is displayed.(I run with windows through git bash)

enter image description here

What am I doing wrong?

As far as I understand, the commands that I have prescribed should simply show those who are in the system. But how exactly to deduce who entered and who left?

I thank you in advance for your help, I'm quite new to this and don't understand much, sorry.

6
  • 1
    I'm not sure it will be fine to do your assignment for you, so here is a hint: try apropos -r -s 1,8 'log.*in' | grep -i -e user (it searches through the pages of your system's manual; use man apropos, man man and man grep to read more on it). Chances are that (at least) one of the listed commands has the ability to show the logins to your system, possibly filtered by date and time.
    – fra-san
    Commented Sep 6, 2021 at 8:58
  • @fra-san gives out that the command is not found :(
    – Y_sgard
    Commented Sep 6, 2021 at 9:04
  • For completeness - I'm not particularly confident it would have more chances to work on your system - you may replace apropos -r with man -k in the command I suggested in my previous comment.
    – fra-san
    Commented Sep 6, 2021 at 12:05
  • 1
    You are running this under windows. May I ask, if you are referring to a virtual machine or emulated system like WSL or if you want to apply the bash code to Windows as the operating system? In the latter case, I suggest moving the question to Superuser or Stackoverflow, as this forum is for Unix&Linux only and you may not get good answers for Windows-related problems.
    – FelixJN
    Commented Sep 6, 2021 at 12:11
  • @FelixJN of course, I'm not trying to do this directly through windows. I indicated that I opened the file through git bash
    – Y_sgard
    Commented Sep 6, 2021 at 12:22

1 Answer 1

0

You could just follow the file where logins are logged and filter the output, e.g. like this:

tail -f /var/log/auth.log | egrep "(Accepted publickey|Accepted password|Disconnected from user)"

on a debian system this will catch all lines containing ssh-logins either with password or public key, you will have to change the path/name of the logfile (/var/log/auth.log) and the match-phrases to your needs.

the output would then be something like:

Sep  6 11:43:28 myserver sshd[19052]: Accepted password for myuser from 85.237.30.84 port 43890 ssh2
Sep  6 11:43:29 myserver sshd[19522]: Disconnected from user myuser 85.237.30.84 port 43890
Sep  6 11:43:51 myserver sshd[20287]: Accepted publickey for myuser from 85.237.30.84 port 38334 ssh2: ED25519 SHA256:/****
Sep  6 11:43:57 myserver sshd[20420]: Disconnected from user myuser 85.237.30.84 port 38334

the -f switch on tail lets you follow the file until you hit <ctrl>-c you may also list all matching lines in the logfile by replacing tail -f with cat which reads the whole file.

4
  • this is only in linux, isn't it? i mean (/var/log/auth.log)
    – Y_sgard
    Commented Sep 6, 2021 at 11:20
  • yes, this is where sshd and pam logs authentication related things, very common on debian based systems and also the Windows-Subsystem for Linux - when using WSL, you might need to start the logging daemon using service rsyslog start in order to get this file.
    – brt
    Commented Sep 6, 2021 at 13:14
  • i need to write "service rsyslog start" in command line, right?And what shoud i do then? :(
    – Y_sgard
    Commented Sep 6, 2021 at 15:54
  • after starting rsyslog, you should be able to examine /var/log/auth.log, e.g. by using cat /var/log/auth.log. this should help you finding the search patterns for the lines you want to filter.
    – brt
    Commented Sep 6, 2021 at 17:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .