Page 1 of 1

sFTP with expect

Posted: Tue Mar 31, 2026 8:59 pm
by gio.cot
Hi all
i'm try to use exectp to access to sFTP server
i wrote this script to list content folder, but it seems that the "cd" command don't work
this my script
#!/usr/local/bin/expect -f
set REMOTE_DIR "/cert_xxxx_name/in"
spawn sftp cert_xxxx_name@111.22.333.xx
expect "Password:"
send "mypsw\n"
# position
send "cd $REMOTE_DIR\r"
expect "sftp>"
send "pwd \r"
expect "sftp>"
# List the directory
send "ls -la\r"
expect "sftp>"
# Exit SFTP
send "bye\r"
expect eof
this is the log that i see :
Connected to 111.22.333.xx.
sftp> pwd
Remote working directory: /
sftp> ls -la
drwxr-xr-x 3 root root 1 Jan 16 2024 .
drwxr-xr-x 3 root root 1 Jan 16 2024 ..
drwxrwxrwx 5 1013 1014 3 Jan 16 2024 cert_xxxx_name
sftp> bye
$
Why i din’t see anything about the line relative to "cd $REMOTE_DIR\r" ?? possible that directory don’t exist ??
Any suggestion ?
Thanks