How can I login to a remote server and execute a set of commands then when done logout and continue my script?
Thanks.
ssh can be used to execute a command, rather than start a remote interactive login shell. For example:
ssh user@host ls
Will log into host and execute the ls command.
You can use this inside a bash script as normal:
#!/bin/bash
# do local commands
ssh user@host "ls; grep something file.txt; copy a b"
# do more local commands
From ssh's man page, the exit status will be the exit status of the remove command or 255 if an error occurred.
scp to copy the commands to the remote server, and run it or (2) put the commands into a variable inside the bash script and just run it as ssh user@host "$COMMAND". You could read from a file into COMMAND rather than have it all inline: COMMAND=$(cat remote_cmds.sh)$EEEEE: ssh [email protected] 'EEEEE='eeeesssse';echo "$EEEEE";echo "$EEEEE"'As others have stated, you can use ssh. However, if you're running a script, you may want to setup ssh to login without a password. You can do that by setting up a public/private key via the ssh-keygen command. Also change the permission of keyfiles (id_rsa, id_rsa.pub) as 600 for public key and 400 for private key for security of modification. chmod 600 id_rsa.pub ;chmod 400 id_rsa
On your system, you run ssh-keygen to generate the public and private keys. On Unix/Linux/Mac, these sit in the $HOME/.ssh directory. (Keep the passphrase blank!). Then, you want to create a file called authorized_keys on the remote machine under the $HOME/.ssh directory and copy your public key there.There is no need of generating encryption keys on remote m/c.
Now, when you do ssh or scp to the remote machine, you don't have to give the password.