Bash Script for secure transfer

The idea is simple, I want to be able to execute a command scpfrom my system and move files to another computer and vice versa.

In my case, there is a small problem. To connect to the destination computer, I need ssh on host_b first, and then ssh on host_c.

In short, I need to copy all my files to host_b first and then to host_c

This is scematic:

my_computer -> host_b -> host_c

Since, I have to do this in order to run my code manually each time, this becomes something frustrating, so I thought I had to make my own script to automate this process.

Here is my code:

#!/usr/bin/expect -f

echo
echo "Move files from your computer to HostC!"
echo

printf 'Username for hostB: '
read -r userB
hostb_addr='hostb.com'

printf 'Username for hostC: '
read -r userC
hostc_addr='hostc.com'

printf 'File you want to move: '
read -r filepath

scp_params='-P 8140'

# connect via scp to hostB
spawn scp $scp_params $filepath $hostb_user@$hostb_addr:~/temp_transfer

interact

The main idea is to upload files from my computer to a temporary folder on hostB, copy them back to hostC again, and then delete the files from hostB.

, 'spawn not found'

: , bash

+4
1

ssh/scp . ssh- hostB, hostC . scp -o.

scp -o ProxyCommand="ssh $hostB nc $hostC 22" $path_to_file $hostC:$path_to_file
+1

Source: https://habr.com/ru/post/1652351/


All Articles