gpt4 book ai didi

linux - Sftp 文件从远程服务器到本地服务器

转载 作者:太空狗 更新时间:2023-10-29 11:45:59 25 4
gpt4 key购买 nike

抱歉,如果这个问题太简单了。但我是 Java 开发人员,不知道 shell 脚本。我用谷歌搜索,但找不到我要找的东西。

我的需求

  1. 使用 Sftp 连接到远程服务器 [基于 pub/pri 的身份验证键]。指向私钥文件的变量
  2. 传输文件特定扩展名 [.log] 到本地服务器文件夹。要设置的变量远程服务器路径和本地文件夹
  3. 将传输的文件重命名为远程服务器
  4. 将所有传输的文件记录在一个 .txt 文件中

任何人都可以给我一个 shell 脚本吗?

到目前为止,这是我根据建议构建的。我还有一些问题 ;)

export PRIVKEY=${private_key_path}
export RMTHOST=user@remotehost
export RMTDIR=/logs/*.log
export LOCDIR=/downloaded/logs/
export LOG=sucess.txt

scp -i $PRIVKEY $RMTHOST:$RMTDIR $LOCDIR
for i in 'ls -1 $LOCDIR/*.log'
do
echo $i >> $LOG
done

ssh $RMTHOST -c "for i in `ls -1 $RMTDIR; do mv /logs/$i /logs/$i.transferred; done"

最佳答案

这种方法怎么样?

  1. Connect to remote server using Sftp [authentication based on pub/pri keys]. A variable to point to private key file
  2. Transfer files with specific extension [.log] to local server folder. Variable to set remote server path and local folder
scp your_user@server:/dir/of/file/*.log /your/local/dir
  1. Log all the transferred files in a .txt file
for file in /your/local/dir/*.log
do
echo "$file" >> $your_txt
done
  1. Rename the transferred file in remote server
ssh your_user@server -c "for file in /dir/of/file/*.log; do mv /dir/of/file/"$file" /dir/of/file/new_name_based_on"$file"; done"

关于linux - Sftp 文件从远程服务器到本地服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15291034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com