In your case
spawn is most probably a command of expect scripting language which allows automation of interactive program operations. In such a case
spawn runs an external command from the expect script. Your script example is missing a shebang sequence (first line starting by
#!) indicating the
expect interpreter and as such will not be interpreted by expect when executed directly.
Password authentication with sftp is limited to the interactive mode. To control sftp in interactive mode you can use for example the following expect script:
set timeout 20 # max. 20 seconds waiting for the server response
set user username
set pass your-pass
set host the-host-address
set dir server-dir
spawn sftp $user@$host
send "cd $dir\r"
send "mget *\r"
Another possibility is to use the public key authentication which is also more secure (see the procedure). In such a case you can simply use sftp directly in batch mode:
sftp -b - "$user@$host" <<+++EOF+++