在 Bash 脚本中指定 sftp 的密码
我正在尝试编写一个脚本来通过 SFTP 备份文件。问题是,它需要密码,而且我认为没有办法手动指定 SFTP 的密码。我听说过使用公钥不需要密码,但这需要能够 ssh 进入远程服务器并修改一些配置文件,而我无法做到这一点。
目前我的解决方案是使用 cURL,但这是不安全的(使用普通的 FTP)。我还查看了 .netrc 文件,但这似乎是针对 FTP 而不是 SFTP。如何手动指定 sftp 密码?
I am trying to write a script to back up a file over SFTP. The problem is, it requires a password, and I see no way to manually specify a password to SFTP. I've heard about requiring no password by using public keys, but that requires being able to ssh into the remote server and modify some configuration files, which I cannot do.
Currently my solution is to use cURL, but that is insecure (uses normal FTP). I also looked at the .netrc
file, but that seems to be for FTP instead of SFTP. How do I manually specify a password for sftp?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
cURL 可以支持 sftp,如手册所述:
cURL can support sftp, as documented by the manual:
Lftp 允许为 ftp 和 sftp 指定密码,并且根本不需要公钥。您的 sh 同步脚本可能如下所示:
Lftp allows specifying passwords for both ftp and sftp and does not require public keys at all. Your sh sync script may look like this:
您可能还想考虑使用 python(paramiko 模块),因为可以从 shell 快速调用它。
安装模块
示例 FTP 上传脚本
You might also want to consider using python (the paramiko module), as it can quickly be called from the shell.
Install the Module
Example FTP Upload Script
Bash 程序等待 sftp 请求输入密码,然后将其发送:
将其放入名为
sftp_autologin.sh
的文件中。\r
发送一个到 sftp 来执行命令。我不在密码中包含“p”,因为在某些系统上它是大写的,而其他系统上是小写的。 Expect 会生成 sftp 命令。等待看到字符串“assword”并发送命令。然后结束。要使其正常工作:
然后运行它:
它应该会让您进入 sftp 命令行,而不提示您输入密码。
它不安全吗?
这是您可以运行的最不安全的命令。它将密码暴露给命令行历史记录以及任何可以读取“ps”输出的其他人,并且基本上完全违背了密码的全部目的。
但是,嘿,欺诈火灾的另一个记录是什么,每年受害者损失只有大约 250b 美元。咱们去500b吧。
这会自动使用 sftp shell 运行一些命令,并在完成后自动退出:
Bash program to wait for sftp to ask for a password then send it along:
Put that in a file called
sftp_autologin.sh
. The\r
sends an to sftp to execute the command. I don't include the 'p' in password because on some systems it's uppercase, others lowercase. expect spawns the sftp command. Waits for the string 'assword' to be seen and sends a command. Then ends.To get this to work:
Then run it:
It should drop you into the sftp commandline without prompting you for a password.
Is it insecure?
It's about the most unsecure command you can run. It exposes the password to the commandline history, to anyone else who can read 'ps' output, and basically defeats the entire purpose of passwords all together.
But hey what's another log on the fraud fire, it's only about 250b dollars in victim losses per year. Lets go for 500b.
This automatically runs some commands with the sftp shell and exits automatically when done:
为了使用公钥,您不需要修改任何“配置文件”。您只需将公钥的副本保留在 ssh 知道查找的位置(通常是
~/.ssh/authorized_keys
)。您可以使用 sftp 来完成此操作。如果您尚未在服务器上建立任何authorized_keys 文件,则只需将id_rsa.pub 文件放在其位置即可。In order to use public keys you do not need to modify any "configuration files". You merely need to leave a copy of your public key in a place where ssh knows to look (normally
~/.ssh/authorized_keys
). You can do this with sftp. If you haven't established any authorized_keys file on the server, you can simply put your id_rsa.pub file in its place.您无法从命令行指定 ssh / scp 或 sftp 的密码。不提示输入密码进行连接的唯一方法是使用公钥身份验证。
您说您无法 ssh 到服务器来修改配置文件,但如果您可以 sftp 到服务器,您可能可以上传您的公钥。
您的公钥只需位于主目录中的 .ssh 目录下即可。
You can't specify a password to ssh / scp or sftp from the command line. The only way to connect without prompting for a password is to use public key authentication.
You say that you can't ssh to the server to modify configuration files but if you can sftp to the server you can probably upload your public key.
Your public key just has to go under the .ssh directory in your home directory.