如何制作 sshpass scp bash 脚本,将密码而不是密钥作为 cron 运行?

问题描述 投票:0回答:1

我正在尝试制作一个 bash 脚本来发送文件。在终端中运行此命令时,我收到错误:

第 4 行:警告:此处文档在第 2 行由文件结尾分隔(需要 `EOF

这是脚本:

#!/bin/bash
sshpass -p 'password' scp /dir/sub/sub/sub/sub/file.csv [email protected]:/ <<-EOF
quit
EOF

它将发送文件,但是当我尝试使用 crontab 运行脚本时,它不会运行。我需要改变什么?而且我只能使用密码而不能使用密钥。

bash scp sshpass
1个回答
0
投票

我测试了以下代码是否可以工作:

cron 作业脚本:
cron-job--scp-files-locally.sh

#!/bin/bash

# create an ind file with current minute timestamp
touch $(date +%Y-%m-%d_%H%M).ind
# copy  all ind files to /tmp
/usr/bin/sshpass -p 'greatPassword' /usr/bin/scp *.ind metoo@localhost:/tmp
# remove all ind files
rm -f *.ind

当前用户的 crontab 行是:

* * * * * /home/cfrm/cron-job--scp-files-locally.sh

输出测试:
ls -1 /tmp/*ind

/tmp/2023-12-11_1619.ind
/tmp/2023-12-11_1620.ind
/tmp/2023-12-11_1621.ind

夏天

建议的 cron 作业脚本:

#!/bin/bash
/usr/bin/sshpass -p 'greatPassword' /usr/bin/scp /dir/sub/sub/sub/sub/file.csv [email protected]:/
© www.soinside.com 2019 - 2024. All rights reserved.