Our lesson next lesson is about listing users who have logged in let's say for example today or any
Pacific day.
So what happened is let's say your manager comes in comes to you and asks you hey Imraan Can you give
me a list of all the users who have logged in today.
And if they are logged into form a specific machine from a specific IP or even you could say hey can
you give me a list of Muzzo who logged in on July 25th.
Then what would happen.
You have to actually go and get per the right command.
Look for the right options and the right piping commands.
And then you run the command.
So what if you already have a script that does this type of job for you and you know that your manager
is going to come back to you and asks you the same thing.
Probably like two or three times a week or a month and then you need to create a script.
So we'll create this script for our managers so be happy.
So let's go directly into our machine and I and by the way what this group is going to do is it's going
to look for users who are logged in today.
That's a first group we will.
Right.
The second.
What it will do is within the same script will do as we will add the user input meaning we will ask
the user.
OK you want me to get a list of all the users but do you want to get it for a specific day or specific
month.
So we will get that option give that leverage to the users so they could simply input the month or date
and it will get them that output.
Sounds interesting right.
So Ill try that.
Lets go back to our machine and again logged in as our self which we are.
And let's go to our professional script scripture tree and I am going to name this script list uses
OK a regular drill.
Define our variable our author
date lead to data out and you can put it in the script.
This script will list users logged in by date modified.
You could pick that too.
I'm going to leave that out.
I know you are expert now.
OK.
So the first thing that it will do is now before I write anything let's write in quite out of this save
it and if you want to get a list of uses by date what do you do you do run last command right.
You hit enter and you get all those users in the first column and the machine to their terminal the
time and which date they logged in the time and if they're still logged in or not if you do that say
last you'll see these are all the machines that user logged in with from all the machine the time they're
logged in.
So we want to know the list of users by the date.
So the command we ran already right.
But we need you don't need all that stuff.
We need a simple user so you have to modify our command right to be last.
And then we will do.
That's the group for like let's say Monday.
So it will give us all the Monday and everything.
But we also wanted to make sure that it actually goes by the data.
If I if I'm mad my manager says give me all the list of users that are logged in today so I know I could
run that command and I know today's date right.
And I could just simply grep for that.
But what if I want this script to run automatically then the script needs to know what today's date
is.
And for it to find out you'll have to run the date command and then you could do is simply grep for
this date.
Say Friday August 30 first and then sorry First you typed the last command and then grope for that Pacific
date and it will give you only the uses for that Pacific date.
Right.
That's how you do it.
But again script doesn't know what date is today so a script has to run the command date to find today's
date and parse it and then put that into the script.
So to do that we'll get back to our script which is list users.
And here first we'll do is we'll define the variable that what is today's date set date equal and put
the tick tick always comes before the date of the command.
So date AWC
dollar one the one month or two dollar three this is what we want and tick again to close.
But what exactly this command is doing so let's copy this command.
See our script and run it see it's giving you the first three fields.
If you just type date it give you the three field the time the EDT which is the zone the year and bunch
of information.
So you don't want to get all that information you just want the first three columns so you could go
up against these columns.
So we already defined that in our command and it will pick only the first three columns.
Let's get back to our list of uses we already defined that we put in a variable and variable is called
to day.
Now we will do is we'll do
last just to actually run the command and we want to clear up for what you want to grab for our variable
and variable is what today.
So basically what saying is run the last command group for the variable and variable is this.
So this is what it's going to do.
And now let's run it get permissions.
OK you see what is happening here.
It's saying August no such file directory first.
No such valedictory why's happening.
Let me tell you why.
OK.
So let's do the CAT of the script you see it's when you run this command it gives you the information
which is Friday August 1st.
And we are passing this output to the group.
But when you do the passing of information of grep and then you do this entire thing.
Then the group doesn't know which to go up for.
It has to grow for Friday or August or 30 years.
So we have to put that in quotes so the group would know that this whole thing is one string then I'll
drop it.
So we have to modify our script the same way list and we will put the code around our variable
perfect.
Now let's clear the screen.
Let's run it again.
And this time it is giving me all the users that have been logged in today.
So system will find out today's date and it will go up for today's date.
So this is our first script that I mentioned that we want do you have the list submitted to your to
your manager.
And if the manager only wants the user you can modify your script by doing list and instead of last
and up today you could just do sorry or get to what column number one because you don't want all the
stuff that comes with it right.
So now let's run it and you will only get the users.
And that's what he or she wants to get the list for right now.
Next thing is the why to create a script as a user input to pull the user.
So what if you had the script which which the script knows that I'm going to poll users that logged
in today only by default.
But now we want to give the power to the user so the user will define its own its own variable or its
own choices.
Let's put it this way.
So for that I'll go back into our Linux user script and what we'll do is we leave everything as is and
no sorry we will actually have to read more because this one actually is for today and we don't want
to grow up for today and we will get rid of this one as well.
So you know what the best thing is is for deleting this or moving this whoops squid out of it and just
copy it.
This user
by input
that's a second.
Sorry shoes by.
OK.
So now this is now of equal motherhood.
This way we could leave the previous script as is and modify the new one
OK so let's get rid of it.
So description this group will list users logged in by date or now by the
by user input.
OK.
So first thing is it has to ask the user what you're looking for.
So whenever you need to ask user what do you have to do you have to display that message on the screen
to display the message on the screen.
Is echo
an output code here.
Because I'm going to put parentheses.
So please enter day and I'll put parentheses for example Monday because I have to let the user know
the example and I'm putting parentheses that's where you when you put parentheses in Echo then I think
you would have to put in code but you could try it out code.
Hopefully it works.
But anyway just to be sure.
OK so now you have to read the input.
So you do d for day Ecko is to put an empty line.
Next one you need to ask is please enter month.
For example August that's how these last echo again just put an empty line.
So not read because we have to read the input read and is a variable from month ago.
Another echo.
Now we have to ask another question please.
Enter date for example.
28
Okay now read the date.
You already have a variable for it or put da.
So we have ask all these questions.
Let's put a quick here or we have all your questions and we have added those questions into our variable.
Now what do we want to do next.
Next we want to do is run the command last and grep for
coat dollar d dollar and dollar you see how it is going to grow up but it is going to grow up the turd
month.
The end to and to day and to month and to date.
Put that variables here and I'll drop from the last let's run it and we'll see how it works.
List uses buy in please and turn day perfect.
Put in Monday as it say please enter month at say August please.
And to date now put 20 and it say's there is no user that actually has logged in that date to verify
our scrip runs.
Let's run just last command.
This to see if there's actual uses or work.
So we'll pick.
Sunday August 26.
Perfect.
Let me clear the screen so you will see the
Sunday August 26 so we know for sure that this is the day month that someone in my machine or I have
logged in.
So when I hit enter now it's giving me all the users that been logged in to my Linux machine on this
date.
Again if you were to just view the list of users not the whole information would you have to do is you
just put it at the end you put an awk dollar one and this way it will only give you
the users clear the screen.
That's right again this user by input please.
And to date.
Sunday
August 26.
Perfect.
This is what we wanted to know and you could just simply copy and paste it to your email and send it
your boss and you are happy he's happy and your script is done.
Next time it comes back to you and say hey can you give me the same list again.
What do you have to do you have to run the command.
Put in all those variables again.
No you have a script.
You made your life easy so you just run the script and there it goes.
Now you could go one step further.
How is you could actually automate this task by running to the cron job.
If your manager tells you that I need a list of everyone who logs in on Monday of every month you could
specify that as well.
All right.
Hopefully it works out for you.
Try it out try a different options.