One of the things you should do when first setting up your new server is changing the SSH port. This is not because it will actually stop a real attacker but because your server will be hammered with automated random login attempts, so the real reason why we will change the default ssh port is so that when the automated attacks can’t even connect to port 22 then usually it just stops and moves on to the next server. If you are using a cloud droplet or VPS that supports snapshots, now is the time to take one just incase you get locked out if something goes terribly wrong (never hurts to be safe!).
In this post I will show you how to use bash redirection to write to a file using the cat command. This is a very simple trick that you can use for all sorts of things when it comes to bash scripting including setting up certain configuration files when more powerful options aren’t available or aren’t worth using. You can also use this in your bash scripts to create files from scratch if need be without having to have separate files or download them remotely.
Today I will be showing you how to use a basic expect script inside of your bash scripts for automation. Expect works just like the name sounds, expect to see a prompt or other piece of text and then send the response. With this you can do things such as automatically entering password for SSH (not recommended for daily use), automatically answer prompts, etc.