Skip to content
Advertisement

Shell script for remote SSH

I’m new to shell scripts, and I have centos running. I want to write a shell script that ssh a remote machine and execute a bunch of commands. The problem I’m facing is how to provide the username, the password, the remote machine address, and the private access key to a command that shall connect the remote machine.

I’ve Google’d and found some scripts, but all of them need a utility called expect, and I don’t want to install any utility, only to run my script. Is there a way to do this?

Advertisement

Answer

You can pass all you need in a ssh call, doing the following:

ssh -i private_key_path user_name@remote_machine "command"

If you’re going to use this connection many times, and want to maintain it configured, add the following lines to you .ssh/config file:

Host host_alias
    User user_name
    HostName remote_machine
    IdentityFile private_key_path

and then access the remote machine, and execute the command you want, by doing:

ssh host_alias "command"

Notice that command, AFAIK, must be embraced in quotes, as it must be considered as only one argument by ssh.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement