In this hands-on lab, we’re going to write a shell script for connecting to Linux Academy Linux servers from another Linux (or Mac) host, without having to first accept the RSA fingerprint.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Determine What Options Should Be Used with the `ssh` Command
- View the man page for the
ssh
command, and determine the option to use to disable host key checking.man ssh
- The syntax to use is:
ssh -o StrictHostKeyChecking=no user@host
- View the man page for the
- Build a Script from the Required Commands
- Create a
bin
folder incloud_user
‘s home directory.mkdir bin
- Use vim or nano to create the file
lab.sh
in the newbin
folder.vim bin/lab.sh
- Start with the shebang at the top of the file.
#!/bin/bash
- Add a line to
ssh
using the variable from above and taking the first parameter as the host address.ssh -o StrictHostKeyChecking=no $login_user@$1
- Add a line to set a variable for the login user.
login_user=cloud_user
- Add two lines to make sure that that the first variable is not null and close the script.
if [ -n $1 ]
fi
The completed script should look like this
#!/bin/bash login_user=cloud_user if [ -n $1 ] then ssh -o StrictHostKeyChecking=no $login_user@$1 fi
- Then save the file by running:
:wq!
- Create a
- Execute and Verify the Script
- Save the file, and make it executable.
chmod u+x bin/lab.sh
- Run the script, passing
10.0.1.10
as the first parameter to be assigned to$1
../bin/lab.sh 10.0.1.10
- Once prompted input the
cloud_user
‘s password.
- Save the file, and make it executable.
- Add the New `bin` Directory to the `PATH` Variable
- Append to the
.bashrc
file incloud_user
‘s home directory to add the newbin
folder to thePATH
environment variable.echo 'PATH="$HOME/bin:$PATH"' >> .bashrc
- Source
.bashrc
to pick up the change.source .bashrc
- Verify that you can run
lab.sh
without specifying the path to the script.lab.sh 10.0.1.10
- Append to the