Hello, I want to point out that while I have heard about expect, I've never used it. I have some suggestions so as not to open different sessions for each download and a more bash-friendly reading of the file.
If I've got it right, expect reads output and when it comes with a matching string it just proceeds to the following code.
If I'm right, then the script could go as:
Code:
#!/bin/bash
/usr/bin/expect - << EndMark
set timeout -1
spawn ssh user@ip
expect "user@ip's password:"
send "password\r"
sleep 2
while read line; do
send "scp /path/${line} root@destination-ip:/path/\r"
expect "root@ip's password:"
send "password\r"
expect eof ## <- HERE expect the eof and then do a new request for another file(proceed to the while loop again).
done < film.txt
send "exit\r"
EndMark
exit 0
Because I have never worked with ssh either, will it expect for root@ip's password every time you try to download a file from the same session? If yes, then the above code is right, it asks for passwd each time a file is being downloaded.
If not, then the code could go as:
Code:
#!/bin/bash
/usr/bin/expect - << EndMark
set timeout -1
spawn ssh user@ip
expect "user@ip's password:"
send "password\r"
sleep 2
first_time=1 # <- HERE making a variable so as to know which is the 1st time.
while read line; do
send "scp /path/${line} root@destination-ip:/path/\r"
if [ first_time -eq 1 ]; then # <- HERE, the 1st time first_time will be 1, so it'll ask for password!
expect "root@ip's password:"
send "password\r"
first_time=0 # <- HERE, setting first_time as 0, so as not to pass the above 'if' statement again.
fi
expect eof
done < film.txt
send "exit\r"
EndMark
exit 0
Hope I helped a bit or a byte
