[Autogenerated] Now,
one of the really great things about using sd is that
we can edit files non interactively.
Now that does allow us to work remotely.
So we're going to take a look at working with our server dot text file.
Now, here we only need to put one entry in.
But if you have multiple servers that you can use as test machines,
we'll add multiple IP addresses in in the demonstration,
we'll be using two Ubuntu systems just so we cache
show that this does really work.
But TIFF, you don't have access, don't worry, just put the one IP address in.
We can then create our wire loop,
we can read into our server variable and then we go into our loop.
Of course, in the loop is where the code is going to be.
And we can specify that we're working on our current server
variable with then into our SSH command.
This is where we connect to our servers.
The minus N suppresses a connection two standard in which we need.
If we got multiple servers, if you've only got the one server,
you will find it won't make any difference.
But TIFF, you've got more than one server.
If it stops after the first server,
then make sure you've suppressed the standard in with minus lowercase N.
The upper case C is then running a command against our server.
And we can see then the command,
the command is in our quotes and we're using sudo to elevate our privileges.
Make sure you've got sd URI file is,
and you're able to elevate without the use of a password.
We are making an edit to the SSH D configuration.
So if you're not happy making the edit to your actual configuration,
then put the path to another file where you'll have to make
sure that that file exists but be aware the code as is is
editing your SSH D configuration.
But all we're looking at doing is removing commented lines and empty lines.
Once we have finished editing the file,
we can co ee out that we've finished working on that server.
We can then go through and iterate through to any other servers
with the done keyword showing the end of the loop.
But we read our servers in from our server dot text file.
Now, as always, the very best way to learn is for you to do it.
So follow through with me,
this is your chance to get some great hands on experience.
We're going to create a list of servers,
we can connect to those servers via SSH and run a command.
And by doing so,
we're going to be really able to streamline our administrative tasks.
Now we're going to be working with two systems,
but you'll be able to see that we can very easily edit a
configuration file on both of those systems.
If you only got one server that you can work with, well, that doesn't matter.
We're leaving it down to you.
It's getting the concept of what we're doing is the real important thing.
So, come on, let's find our command line.
Now, look at this,
I'm working on my Ubuntu one system and I've got two
Ubuntu systems up and running.
I'm just going to create my server dot text file.
So if I go through it doesn't go through VMs server dot text.
If you only got one system, well, that doesn't matter.
Just put your local host address in.
But I'm going to go through and put it in then 19 2D 190.168 dot 56.1 oh one,
which is this system and then I can copy that and paste it
down and then change this to one oh two,
which is my other system.
The idea of me showing it with two systems is we can
see that this does actually work.
If I go through then and save those changes, we are ready to go.
If I go through and look at my wire loop wi read into my server variable,
we can go through and execute our do block from here.
I can go through and say echo and then something like,
you know,
starting on dollar server and close that we're then into
our main task and we're going to use SSH,
we're going to use the option minus N.
So that then just allows me to connect to multiple
servers without swallowing my standard in.
It really is required.
We can specify an option so I can go minus O and I'm going to go
through and put strict host key checking equals no.
Now, in my case, I haven't connected to the machines before.
This is a brand new machine that I've set up and I
don't have any of the keys accepted.
So I could auto accept the keys,
we can go through and run our command against our server
variable and then that's going to be executing.
I'm starting off with double quotes, sudo and said,
remember we are editing that file.
So make sure you're happy to do this minus upper case E for an
enhanced regular expression and I for our in place edit,
we can create a backup if we want.
So we can create a copy of the original.
And we can also put a date based extension.
So if we put ID dot dollar date plus per percent F so
that's going to give us a date extension.
We're then ready to go through to our expression.
And this is where I'm using single quotes and we know this by now.
So we're going to be looking for lines that begin with
either our comment or the end of line marker.
And we're going to delete them from R E T C slash SSH slash SSH,
D underscore co fig.
And we do want to make sure that we've got this correct.
Let me just close off our quotes because of course
we are implementing this edit.
So we want to make sure everything is going to be correct.
But the nice thing about this is it's going to run across all of
the servers within my server dot tech file.
Now, I know I have two, but that could equally be 200 servers with that done.
We can go through an echo out that we've finished on that server,
close off our quotes and now we are done,
but don't forget to read in the server dot text file and
we're writing this on the command line.
It's not that difficult,
but it could also be written as a bash shell script once we're ready to run.
And you can see then that I've connected to and have to
put a password in for my vagrant one,
this system so I can go through and put it in my password.
So we started and finished.
We can see now I've got to go through and authenticate to their
second system and now we're finished on that.
If I go through now and take a look,
we've got our sudo and co forward slash E T C ssh,
ssh,
D underscore fi and we can see there that we've got our clean and
tidy file with the original one as a date extension.
So you can see then two oh 23 and then going through the original file.
And of course, that's what we've got on server one.
But if we go through and check on server two,
so ssh 219 2D 2190.168 dot 56.1 oh two, put in the password.
Ideally, we'd use key based authentication.
So we don't have to worry about passwords.
But then the same thing if I go through and do a sudo an co
forward slash E T C forward slash SSH.
And then the SSH D underscore fi we can see there,
we've got our clean and tidy file and then go
through and put our date extension.
We can see then that, that there as well as the backup.
Now, how easy was that, that was against just two servers.
But I say it would be very easy.
That could have been 10 servers or 20 servers or 200 servers.
So long as we know, we've got the correct said expression,
we can then carry out our edits and this is where
your power is really coming from.
Now, come on, let's summarize what we've taken a look at.