- 1 year ago
Windows command echo Y doesn't work for sftp command for automation confirm
The sftp
command in Windows does not support automatically confirming prompts using the echo Y
method. However, you can use a different approach to automate the confirmation.
One option is to use the expect
utility, which allows you to automate interactive command-line applications. You can install expect
for Windows using tools like Cygwin or MSYS2.
Here's an example of how you can use expect
to automate the confirmation in an sftp
session:
bash#!/usr/bin/expect
spawn sftp user@host
expect "password:"
send "your_password\r"
expect "sftp>"
send "put file.txt\r"
expect {
"Overwrite*" {
send "y\r"
exp_continue
}
"sftp>"
}
send "quit\r"
expect eof
In the example above, we're using expect
to spawn the sftp
command and automate the password entry, file transfer, and confirmation prompts. When the "Overwrite" prompt is encountered, we send "y\r" to confirm the overwrite. After the transfer is complete, we send "quit\r" to exit the sftp
session.
You can save this script to a file, for example, automate_sftp.expect
, and execute it using the expect
command: expect automate_sftp.expect
.
Please note that the expect
utility is not a native Windows command, and you'll need to install it separately.