I have a shell script which will kick off a series of jobs on a remote server. I can ssh into this server (but it does require a password!) and run the scripts manually, but there are certain steps that are also done locally so I would like to avoid manually sshing and executing things. Is there a way to automatically ssh/login and then kick off the command within a larger local shell script?
I would suggest if you have many commands to run on the remote server that you create those as a shell script that you upload once and then execute that once per the ssh command.
Make sure that you have execute permissions on the script and that it doesn't rely on any information that is gained from your normal shell session, like variables or path set by an interactive login script, for example: .bashrc as these are not run when running a command via a remote ssh command.
Also remember to check to make sure that the "return" of the script is checked to make sure the command completes as you expected.
To execute a script/command on the remote host run
ssh [<USER>@]<REMOTE_HOST> [-f] <REMOTE_COMMAND>
-f option if you want ssh to immediatelly go to the background instead of waiting for the command to finish.
Conceiring the login, automated password identification is in general a bad idea. If it really is impossible for your setup, to generate a private/public key pair (with the private key stored on the local machine and the pub key stored on the remote machine) and use that for authentification - as suggested in the comment above -, you will have to revert to a tool like
sshpass where you can initiate your remote session/ command via:
sshpass -p <YOURPASSWORD> ssh [<USER>@]<REMOTE_HOST> [-f] [<REMOTE_COMMAND]
You can use public/private key authentication to establish your
ssh without the need of a password.
To do that you just need to generate the key pair (
ssh-keygen) and add your public key to the server's user ~/.ssh/authorithed_keys file.
#!/bin/bash ssh user@server RemoteCommand #man ssh for more info local command1 local command2 .....