bash 如何在shell脚本中将密码传递给sftp连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19908549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:24:50  来源:igfitidea点击:

how to pass password to sftp connection in shell script

bashunix

提问by Trinadh Sidda

I have an sftp connection to a server in Unix. Without password, I use the syntax to connect and execute command

我有一个到 Unix 服务器的 sftp 连接。没有密码,我使用语法来连接和执行命令

sftp -b $user@$server_name

sftp -b $user@$server_name

Can anyone suggest me how can I write a shell script to connect a remote server non interactively using a password

任何人都可以建议我如何编写一个 shell 脚本来使用密码以非交互方式连接远程服务器

回答by cmdilip

Try with this below option,

尝试使用以下选项,

lftp -u $user,$pass sftp://$host << --EOF--
cd $directory
put $srcfile
quit

--EOF--

--EOF--

回答by Tux

You could use ~/.ssh/config file.

您可以使用 ~/.ssh/config 文件。

#
# ~/.ssh/config
#

Host servername
Hostname 127.127.127.127
Port 22
User root

#EOF: config

Then simply connect with "ssh servername" and if you don't want to use password you can use SSH key. Here is good tutorial on how to do that > http://www.cyberciti.biz/tips/linux-multiple-ssh-key-based-authentication.html

然后只需使用“ssh servername”连接,如果您不想使用密码,您可以使用 SSH 密钥。这是关于如何做到这一点的好教程> http://www.cyberciti.biz/tips/linux-multiple-ssh-key-based-authentication.html

If you just want to pass user/server from terminal, you can do this.

如果您只想从终端传递用户/服务器,您可以这样做。

#!/bin/bash
sftp -b ""@""

then use it like this './sftp.sh user server'

然后像这样使用它'./sftp.sh user server'

use SCP like this;

像这样使用SCP;

scp -P 22 user@server:/dir/file.tgz ~/Desktop/

use SFTP like this;

像这样使用 SFTP;

sftp user@server:/dir/file.tgz ~/Desktop/file.tgz

You can also try this;

你也可以试试这个;

sftp user@host <<EOF
get /dir/file.tgz
rm /dir/file.tgz
EOF

回答by Brian Campbell

The best way to do this would be to create a key pair on the client, and add the key to the target user's ~/.ssh/authorized_keys.

最好的方法是在客户端创建一个密钥对,并将密钥添加到目标用户的~/.ssh/authorized_keys.

To create a key pair, run ssh-keygenand when it asks for a password, just hit return to indicate "no password". Then either run ssh-copy-id $user@$server_nameor manually create a ~/.ssh/authorized_keyson the server and copy the contents of the ~/.ssh/id_rsa.pubfrom the client into it (ssh-copy-idisn't available on all machines, so on some you'll have to do it manually).

要创建密钥对,请运行ssh-keygen,当它要求输入密码时,只需按回车键表示“无密码”。然后在服务器上运行ssh-copy-id $user@$server_name或手动创建 a并将客户端~/.ssh/authorized_keys的内容复制~/.ssh/id_rsa.pub到其中(ssh-copy-id并非在所有机器上都可用,因此在某些机器上您必须手动执行)。

Now you should be able to run sshor scpwithout a password, as it should use your key instead. If it doesn't work, make sure that the permissions on your ~/.ssh/directory and contents are correct on both machines; the directory should be 0700(drwx------) and the files should be 600(-rw-------). Also check that key authentication is enabled on both the client and the server.

现在,你应该能够运行sshscp不使用密码,因为它应该用你的密钥来代替。如果它不起作用,请确保您的~/.ssh/目录和内容在两台机器上的权限都正确;目录应为0700( drwx------),文件应为600( -rw-------)。还要检查是否在客户端和服务器上都启用了密钥身份验证。