CentOS SFTP批量文件传输实用指南
一 前置准备
二 方法一 批处理脚本结合 SFTP 内置命令
sftp user@hostname <<'EOF'
cd /remote/path
lcd /local/path
put *.txt
bye
EOF
sftp -b - user@hostname <<'EOF'
cd /remote/path
lcd /local/path
$(while IFS= read -r f; do echo "put \"$f\""; done < file_list.txt)
bye
EOF
三 方法二 使用 lftp 实现高效批量与并发
lftp -e 'mirror --reverse --parallel=10 -R /local/path /remote/path; quit' -u username,password sftp://hostname
lftp -e 'mirror --parallel=10 /remote/path /local/path; quit' -u username,password sftp://hostname
四 方法三 使用 expect 自动化交互式 SFTP(含密码场景)
#!/usr/bin/expect -f
set timeout 30
set host "remote_host"
set user "your_username"
set pass "your_password"
set local_dir "/path/to/local"
set remote_dir "/path/to/remote"
spawn sftp $user@$host
expect "password:"
send "$pass\r"
expect "sftp>"
send "cd $remote_dir\r"
expect "sftp>"
send "lcd $local_dir\r"
expect "sftp>"
send "mput *\r"
expect "sftp>"
send "bye\r"
expect eof
五 方法四 提升效率的替代与增强方案
lftp -e 'pget -n 8 sftp://hostname/remote/bigfile.iso -o bigfile.iso; quit' -u username,password
rsync -avz --progress --bwlimit=10000 -e ssh /local/path/ user@hostname:/remote/path/
find /local/path -type f | parallel -j 4 scp -i /path/to/key {} user@hostname:/remote/path/