Create system inventory step by step use and put in this lesson we are actually going to create a script
that will ask from a user for it and put any type of form or record that we are you would have to create
in Linux.
Now if you are running let's say you're a paci server or your web server then you probably would have
to give some kind of forum to the users so they would come in and type in say even a DNS record or even
a system a mentor.
In our case you'll put it in a hostname you put an IP address you put in a description or any other
information.
That's a kind of form you're given right.
And then you click on Save that record is saved.
That's how we usually see it online.
Whenever we go online and whenever we type in information that is saved somewhere most likely in bigger
companies it is saved in a database.
But now since we are creating our own little database on little inventory we keep track of all the systems
we build.
So there has to be a way.
So you could have that script together and B could expose that script on our server so users can use
it as a form and put that information in.
So anything that you want to do that could be done later but now for now for this lesson you will learn
that the first script that will create is that script that will add a record so it will ask you Do you
want to add a record.
And so please provide provide hostname.
Second question I'll ask you please provide IP address of the system.
The third one is please provide a description of the system.
Once all that information is provided that information is saved into a file since it's a Linux machine
it will be saved in a Rugland text file.
If you are using Windows more most people create this Excel sheet to keep track of all their equipment.
Do all types of asset management.
Second type of script will Khowar is about deleting a record.
So this group will ask pretty much the same things as the first.
But in this case it will ask to delete the record and that their script that will Khowar.
It's about combining both the above scripts to add a record and to delete the record.
So we will use a case statement in which it will ask how you use or what do you want to do do you want
to add a record or do you want to delete a record.
Use a picks a of to add a record or B to delete a record.
Depending on their choices then it will take them to either the script or the second script.
So this is what we are actually going to learn so let's let me get into real quickly.
My Linux machine
OK I have it open.
I am logged in as myself and I am in my home my home directory and p.s. directory which is the professional
scripts directory that I created.
Now the first thing before I start writing a script I need to create an empty file where all the input
will be stored once once actually provided by the user.
So I'm going to create touch database and I'm going to call it database that's verified stare at the
last one is the database.
OK let's clear the screen.
Now the first thing I wanted to do is to add a record.
So let's create a new script called add record and you know what we have to do here.
We have to define the bash the comments
and the comment.
Could be the author that day
description one.
My favorite.
This script will add
system in centaury to database file
modified the same date OK.
All of formalities are done.
Standard is followed.
Now let's get into asking question from the user.
What do we want to ask the first question and when you ask the first question you have to throw that
question on the screen and to display that question on the screen you have to use which command echo.
Yes echo.
Please enter hostname because we are going to create an inventory off our system.
I've been building a new system.
You could add your system inventory into this database file please and to your host name Question Mark
and then what do we do with that.
Once we throw that question on the screen to the user the user rule and to the host name once the hostname
is entered on the screen then we want to capture that and put that into a variable right.
And to put that answer into a variable we have to use a command.
Read read.
You could use any variable name that you like but I'll pick hostname just host because hostname itself
is a command and I don't want to confuse you with the actual command so I'm just going to leave read
host echo the second echo I'm adding is because to put in an empty line on the screen so it will look
neat.
Right now what do you do.
First thing first before I add a record into my database file.
I want to check the hostname the user has entered does not previously exist in the database file.
If it does I am going to error out that
so error that outlet's to grep minus Q Q as in for silent and dollar host.
Is host.
Is our variable from where we grepping this.
You have to define the absolute path and the absolute path file which is home of Saul.
P.S. databased remember this is the file we created before we started working on this script.
OK so this is going to actually grow up from this group.
The output of the host from this file.
Okay perfect.
Now next thing is simple.
If then if the output is what is that the return code.
Yes if the output is successful which is equal zero.
So if it does find it then
echo error
hostname which is the host that you entered already exist and simply Atco another line just to make
it need for an exit.
And fee for that.
If you open up that's pretty much it for hostname.
Now we will do the same thing for second question.
So first we asked the second question which is Acco please answer.
IP address read that IP put in the Atco as an empty line and then will do the same thing.
Grepped minus this and in silent for Dal or IP which is the variable from where from home I I've P POS
and the database file if the output matches dollar question mark is equal to zero which is returned
that as has successful then error
IP which is the variable already exist then simply echo again is a line exit here too.
If the IP exists just don't do anything.
And then pretty much and if with the nice ocus we have asked two questions anti-wind to verify or validate
these two answers.
Against our database file that answer provided by the user already exists or not perfect.
Now that question I want to ask the user is me hit and keys this way it will be easier to see on the
screen.
So echo please enter description
and view wine to read the description
and put in one more echo.
And now this echo out that host which we have the whole list that we ask on the first question right
here host with the IP
with the description I'm just going to copy that description so I don't make a mistake in the spelling
and to redirect sign to our database file.
I could use one but then every time the script runs it's going to overwrite it and it would delete all
the records that were added previously.
So it's ADD.
And then I could put a D and
know before I do that will just put right here and go provided record has been added.
Perfect right.
And just put one more Acco just for it to look good save it.
OK.
Now a signed executable or missions
that's run it.
Ad-Rock perfect please.
And our hostname.
I'll put it in Redhat one please.
IP address 192 168.
1.
I'll start with two.
Please enter description.
This is Redhat version 6 the provided record has been added.
Perfect.
OK so now when we go and do Cat our database file you will see that record being added.
You see now what we could do is to make it look a little neat and clean and organized.
We could just V-I our database file and right here we could put
the name that just the host name IP and description it has not no effect on our script.
It's just that I am putting it in.
So be easier to OK.
The record is added.
Now let me try to run the same script with the same record so then we will see it will complain that
this record already exists.
So let's clear the screen add record please enter hostname Red-Haired one
error hostname bread had already exist.
Perfect.
It is doing its job.
Please enter hostname Red Hat to.
It doesn't exist right now.
Let's please enter the IP address 192 168 1 to
add record line 23.
Error.
Command not found.
OK we have an issue in the script guys.
So let's look at it while it is actually validating the IP that a success does not.
It is complaining that something is not right in our script and what is not right.
The line in line 23.
It says error.
Command not found.
Why.
Because first of all error itself is no.
There is not a command.
We should have an echo in front of it.
We probably don't have that echo so let's go look at line 23 as vi.
Add record and just put Colin 23 will take us straight to that line.
Yes.
You see what I did is I put it in if the returned status is zero which is successful then it should
have then here and then go.
This should be at school.
We don't want that then error the simple fix also make sure whenever you run and you get an error message
back.
Read it see what it says.
OK.
Let's try again now at record please enter hostname Red Hat to please enter IP address $192 168.
1.
To this time an IP address already exists.
Ok now we know our check about hostname and IP address works perfect.
Let's add a command in my ADD record and that command I want to do is every time it runs.
It should clear the screen first so this way you don't see anything that you are already working on
it should bring up a new screen.
OK so let's run it.
And you will see what I'm talking about.
So when you run add and hit enter.
You see Kim over the newer screen and it actually pushed all the stuff that you had on the screen pushed
it up so it looks better.
It looks professional.
Please enter hostname Redhat to please enter IP address 1 to 168 1.3 please.
And our description Redhat version 7 perfect the provided record has been added.
This is just the information we have at the end of this.
This is what you want it to do.
Create or add a record.
Now we need to create another script that will instead of adding it it will delete that record.
So before we go ahead and try hard to leak record let's add another record so we could actually have
quite a few record to play with.
So please enter hostname.
Red Hat 3 IP address 1 2 168 1 for
Redhat
machine at.
Now if I do cat database you will see I should have 3.
Yes I do.
It's now clear the screen and now I want you to copy add record to delete record.
Now we have copied the delete record and you could just leave everything up and delete everything right
here.
So we could start a script and change the description of course.
This will
the Leatt system inventory the system
from inventory database.
Now what do you do first.
Always ask the question we cannot add any record.
If you don't ask questions at please enter hostname or IP address and now why am I asking either hostname
or IP address.
The reason I'm asking is because either the user enters the IP address or hostname.
We should give that option right in front so they should next come.
Basically grep for that input and delete that record.
It's just that simple I don't have to ask.
Please enter the IP address please into the host name and then it will go into either and tore it from
the host name or grep it from the IP.
It's a bit cumbersome so just ask.
Just put an IP or hostname and I'll do the job.
And then what do you want to do with their output.
We wanted to read it read host or host IP whatever that variable name that you want to specify.
Give it a go to the empty line.
Now grep minus Q and quite host IP from my database file if
it turns
0 equal 0 if the exit status is zero then echo.
Now we have found the IP or the hostname provided by the by the user.
And now before we go ahead and delete it we wanted to make sure the IP or the hostname provided by the
user actually exist in that database file.
You're now just going to go into those files and start deleting it.
What we don't have anything so we first need to validate if it exists and to make that validation we
have to grep that provide a name from our file.
And if I get the result which is right here if the result is successful then you could go ahead and
delete it and to delete a record and update it you have to use a command said said is a very powerful
command one of my favorite commands that could do a lot of things.
A lot of wonders in our file.
One modification said minus.
Single code slash
host IP single code
code around the variable into the single code from home.
So p.s..
Yes.
OK.
So don't worry let me explain it to you.
It's what it's doing is it's going into this file and it's trying to find host IP which is provided
by the username right here.
And once it finds it it deletes it.
So you see that slash and slash what I'm saying is whenever you find it this this provided variable
and slash d replace it replace it with what that D but D is it's to delete a line if you want to put
in.
Let's see if I put an array.
Here is Jose PII with old news just like that.
And then I will do is put an s here then what it will do is actually replace that name provided with
the name old but we don't want to replace it.
We want to delete that record.
So instead take out the substitution which is s and take out the word old and put the actual date delete.
So this is what it's doing.
And then at girl the host IP which is the variable has been deleted.
Nice.
OK.
So now be good.
We found the provided hostname or IP and we checked we validated it from the database and we deleted
it.
What if it does not find from this file.
The user says hey go ahead and delete it had five.
I don't have a read at 5.
If that return status is non-zero then do what then we'll see else.
Record wretch's to host IP which is a variable does not exist.
Simple as that.
And we close our fee.
Sorry if that fee that's it.
Hopefully you understand.
It's very simply written and done.
And now go ahead and try to run that
delete correct.
Hit enter please enter hostname or IP address.
Now I'm going to do the same thing I did in my earlier script to clear the screen so I'm just going
to control see and vi the script again and I'll just put in clear at the top.
So again it will look professional.
Let's run it delete Rick.
Please enter hostname or IP address.
I'm going to put in Redhat 36 just to check this to play with it see if it actually finds it or not
record Red Hat 36 does not exist.
Perfect.
This is what I want my script to do.
And it is doing its job perfectly.
Now let's delete record actual record.
We remember we have a record.
Read that one.
Let's try that one right read.
One has been deleted.
Nice but is did it actually delete it.
How can we verify we could actually do CAD.
Our database file.
Right.
It actually goes into this file database and it deletes it from this database.
Right.
So let's verify it.
Yes the Red had one.
It's gone.
Perfect.
OK so I have ADD a record.
I have a delete record.
Now I am going to create a third script right here which will combine both of these scripts and ask
the option from the user to what option they want to pick Do they want to add a record or did they want
to delete a record.
So.
Go.
Here is what I will do is I'll copy one of the script add record and I'll rename is inventory management.
Nice.
OK.
So now inventory management that's come down here.
Description This group will ask to add or delete a record from database file.
The rest is on.
Clear.
Yes I want to clear right below clear.
Just go ahead and delete everything.
Start from fresh OK.
First thing first.
Echo.
Clear line.
Echo.
Please select one off the following options and those options are.
Go to the line echo comma.
So I call my coat single code a equal add Iraq single called close second.
What if you want to give the option for the second one d.
Because delete delete or record right.
You can pick B2 but it's better to pick D delete.
Eric.
OK so far what the script is going to ask is please select one of the option following options and options
are a is to delete or add a record and D is to delete a record so the user has to either enter a or
D.
So now at this point what were the choices that are made by the user.
I will read those choices and I will put it in a variable and will call that variable.
Choice is just a variable is not actual choice choice some kind of comment.
So you could even put to read I if you want that will be a variable where the answer off the user will
go inside of that variables.
So I'll put a choice now once I read the choice I will say Case case is the statement and I will do
dollar choice in all my choices whichever the choice this is pect put it in a case statement and what's
the case statement is being put in than what to do.
So for a to this home I saw P.S. at record right.
This is a script that will actually add a record.
Colon semicolon semi-colon.
This is how the case statement works.
De home I solved.
P.S. delete record semi-colons.
Perfect.
Anything else.
If you touch anything else beside a or D then just say echo in Balad choice
by
and the will and or case statement would
e a c.
Why I pick an empty line here.
Nothing.
No reason you can put it here as well but I just want to make my script look good.
OK so I hopefully you understand what I just did here.
So basically from here to here is just what it is doing on the screen and then whatever the option is
picked by the user it will lock into this variable choice then is going to read those choices in and
it's going to run based on those choice.
The choice is a right here the choices a run this command is the choices D.
Run this command.
If the choice is anything but A&E then says hey inwell a choice by and then close the statement.
Just that simple C bar script
run our script and mentor the management and please select one of the following options A to add or
record d to delete or record let's try adding a record first a hit and or O'Casey it took us to another
script and that script is to add a record.
Now we'll add a record.
Red Hat 5 IP address 1 to 168 out 1. 6 description Red Hat.
Second what that is the pro-ID record has been added.
Now let's do the same thing for delete inventory management.
Please like one of the following options.
Now this time the D.
It took us to the second script please and to hostname or IP address.
Now I'm going to pick 192 168.
Want 6 address I just entered hit enter that IP address has been deleted.
Awesome awesome awesome.
Now the last choice.
If I just accidently hit B and B is not one of the choices right guys.
Invalid choice by perfect See how easy it becomes to put that in a real structural form.
Way to add inventory now adding an inventory is just one example that I picked.
I have worked many times in the past where I would have to create some kind of file or keep track of
some inventory regardless of hostname or IP anything.
So you could actually use descript and provide that to your users so they could add it instead of the
tying the file and adding the record because most people are not comfortable with the high file.
So try that and try it with different options.
You could also add 4 scripts or 5 different scripts and put all those scripts together into one main.