How To Use SSH in Unix or Linux shell script

How To Use SSH in Unix or Linux shell script

To use SSH (secure shell) in a Unix or Linux shell script, you can use the ssh command in a shell script to execute commands on a remote server or to transfer files between servers.

Here's an example of using the ssh command in a shell script to execute a command on a remote server:

#!/bin/bash

# Connect to the remote server
ssh user@example.com <<EOF

# Execute the command on the remote server
ls -l

EOF
Source:ww‮tual.w‬turi.com

This script will connect to the remote server example.com as the user user, and execute the ls -l command on the remote server. The output of the command will be displayed on the local terminal.

You can also use the ssh command to transfer files between servers. To transfer a file from the local server to the remote server, you can use the scp command, which stands for "secure copy".

Here's an example of using the scp command in a shell script to transfer a file from the local server to the remote server:

#!/bin/bash

# Transfer the file from the local server to the remote server
scp file.txt user@example.com:/path/to/destination

This script will transfer the file file.txt from the local server to the remote server example.com, placing it in the /path/to/destination directory on the remote server.

You can also use the scp command to transfer files from the remote server to the local server. To do this, you can specify the remote file as the source and the local file as the destination.

Created Time:2017-10-29 22:08:34  Author:lautturi