How can I login to a remote server and execute a set of commands then when done logout and continue my script?
Thanks.
The SSH client program can be used for logging into a remote machine or server and for executing commands on a remote machine. When command is specified, it is executed on the remote host/server instead of a login shell.
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.
Small variation with easier code formatting using ssh
and bash -s
:
echo '
globchar="*"
ls -1d $globchar
ls -ld $globchar
' |
ssh user@host "bash -s --"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With