Ideas for Getting and Renaming files via SFTP
Posted: Thu Mar 13, 2025 4:09 pm
Hello,
I'm working on migrating processes that currently use FTP to start using SFTP. In this scenario we are the client and will be connecting to a trading partners SSHD.
I am trying to determine what will be the best way to get all files in a directory and then rename or delete them on the SSHD? I plan on using mget to get the files. I'm struggling with what is the be way to then delete or rename the files once I have them on my system.
I will be using Expect scripts and CL programs to automate the processes. My initial thought would be to get the files and then build an Expect script that will rename or delete the files I just got. For Example I would dynamically build a script what would look something like this:
#!/usr/local/bin/expect -f
set timeout 20
spawn sftp $env(SSH_USER)@$env(SSH_HOST)
expect {
default {exit 2}
"continue connecting (yes/no)?" {send "yes\n"; exp_continue}
"password:" {send "$env(SSH_PASS)\n"; exp_continue}
"sftp>"
}
send "rename test 1.txt /processed/test 1.txt\n"
send "rename test 2.txt /processed/test 2.txt\n"
send "rename test 3.txt /processed/test 3.txt\n"
expect {
default {exit 2}
"not found" {exit 3}
"sftp>"
}
send "quit \n"
exit 0
The number of files being renamed would be dependent on which files I just previously got.
I'm curious if there is a better way?
Thanks,
Grizzly
I'm working on migrating processes that currently use FTP to start using SFTP. In this scenario we are the client and will be connecting to a trading partners SSHD.
I am trying to determine what will be the best way to get all files in a directory and then rename or delete them on the SSHD? I plan on using mget to get the files. I'm struggling with what is the be way to then delete or rename the files once I have them on my system.
I will be using Expect scripts and CL programs to automate the processes. My initial thought would be to get the files and then build an Expect script that will rename or delete the files I just got. For Example I would dynamically build a script what would look something like this:
#!/usr/local/bin/expect -f
set timeout 20
spawn sftp $env(SSH_USER)@$env(SSH_HOST)
expect {
default {exit 2}
"continue connecting (yes/no)?" {send "yes\n"; exp_continue}
"password:" {send "$env(SSH_PASS)\n"; exp_continue}
"sftp>"
}
send "rename test 1.txt /processed/test 1.txt\n"
send "rename test 2.txt /processed/test 2.txt\n"
send "rename test 3.txt /processed/test 3.txt\n"
expect {
default {exit 2}
"not found" {exit 3}
"sftp>"
}
send "quit \n"
exit 0
The number of files being renamed would be dependent on which files I just previously got.
I'm curious if there is a better way?
Thanks,
Grizzly