John Mark John Mark - 3 months ago 11
Bash Question

Shell Script to open SSH connection execute a command

I`m having dificult in create an script, that get folder with subfolders inside, and open an ssh connection with other server, execute an axel command to download each file from the folder, and erase the folder and the files when the job is done.

As soon this server and folder is open on internet, I pretend to use the Axel to speed up the file download.

Anyone can help me?

Thanx

Answer

Below is the script to connect to remote server using ssh

#!/usr/bin/expect -f

spawn ssh $username@$host bash script_to_run_on_server "$directory_path"
sleep 2
expect "password:" {
    send "${$password}\r"
}
interact

In the above script, host,username,password,directory_path are variables which you can replace according to your values.

your logic to 'execute an axel command to download each file from the folder, and erase the folder and the files when the job is done' show be written in script_to_run_on_server.

Note: Make sure, the directory have read,write excess from the server environment.