sftp using expect to automate file transfer not working

Omari Victor Omosa :

I want automate below command so as to pass the password and proceed with file transfer.

#This command works well but it will require password
echo "put This_file_from_server_a.csv /TO/THIS/SERVER_B/PATH" | sftp [email protected]

i have tried to use expect, so as to automate/ send password automatically, but it is not working i.e.

expect -c 'spawn "put This_file_from_server_a.csv /TO/THIS/SERVER_B/PATH" | sftp [email protected]; expect "assword:"; send "THE_PASSWORD\r"; interact'

I get error

send: spawn id exp4 not open while executing "send "THE_PASSWORD\r""

What could be the issue? without considering alternatives: such as sshpass, lftp, private keys...

gwatene :

Consider creating private keys on both servers that you are trying to do the transaction then try doing the same, it should work

in case it does not use expect as below.

expect  <<'END_EXPECT'
set timeout -1
spawn sftp [email protected]
expect "assword:"
send "THE_PASSWORD\r"
expect "sftp>"
send "put This_file_from_server_a.csv /TO/THIS/SERVER_B/PATH\r"
expect "sftp>"
send "quit\r"
expect eof
END_EXPECT

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174931&siteId=1