如何使用 Bash 脚本中的密码运行 sftp 命令? [英] How to run the sftp command with a password from Bash script?
问题描述
我需要使用 sftp 从 Linux 主机将日志文件传输到远程主机.我已经从我的操作组获得了相同的凭据.但是,由于我无法控制其他主机,因此我无法生成并与其他主机共享 RSA 密钥.
I need to transfer a log file to a remote host using sftp from a Linux host. I have been provided credentials for the same from my operations group. However, since I don't have control over other host, I cannot generate and share RSA keys with the other host.
那么有没有办法从 Bash 脚本内部通过 cron 作业?
So is there a way to run the sftp
command (with the username/password provided) from inside the Bash script through a cron job?
我发现了一个类似的堆栈溢出问题,在 Bash 脚本中指定 sftp 的密码,但是我的问题没有令人满意的答案.
I found a similar Stack Overflow question, Specify password to sftp in a Bash script, but there was no satisfactory answer to my problem.
推荐答案
除了使用公钥认证之外,您还有几个选择:
You have a few options other than using public key authentication:
- Use keychain
- Use sshpass (less secured but probably that meets your requirement)
- Use expect (least secured and more coding needed)
如果您决定给 sshpass 一个机会,这里有一个可行的脚本片段:
If you decide to give sshpass a chance here is a working script snippet to do so:
export SSHPASS=your-password-here
sshpass -e sftp -oBatchMode=no -b - sftp-user@remote-host << !
cd incoming
put your-log-file.log
bye
!
这篇关于如何使用 Bash 脚本中的密码运行 sftp 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!