SFTP using Expect in bash script

To Nha Notes | July 28, 2022, 12:17 a.m.

Installation:

yum install -y expect

yum install -y sshpass

Example 1:

#!/usr/bin/expect
# for cygwin64
#
puts "start"
#spawn ping 192.168.1.101
spawn sftp usr@192.168.1.101
expect "password: "
send "usr-passwd\n"
expect "sftp>"
send "ls -l\n"
expect "sftp>"
send "bye\n"
Example 2:

#!/use/bin/expect
spawn sftp user@xx.xx.xx.xxx
expect "password:"
send "password\n"
expect "sftp>"
send "lcd /var/scripts \r"
expect "sftp>"
send "mget test.txt \r"
expect "sftp>"
send "quit \r" 

Example 3:

 

#!/bin/bash

export SSHPASS=password

sshpass -e sftp -oBatchMode=no -b - tester@192.168.1.169 << !

   ls -la

   bye

!

References

https://gist.github.com/satokjp/9826697

https://www.generacodice.com/en/articolo/1347879/How+to+run+the+sftp+command+with+a+password+from+Bash+script%3F