在 shell 脚本中使用 SCP 时如何响应密码提示?

发布于 2024-08-05 16:32:10 字数 276 浏览 10 评论 0原文

首先,我很清楚关于这个话题有很多问题。我已经阅读过它们,但仍然可以找到适合我的情况的答案。

我想使用 shell 脚本将整个 ~/cs###/assign1 目录从本地目录复制到学校主目录。我的问题是,我的脚本中有没有办法等待密码提示,然后模拟键盘事件来“输入”我的密码?


这里是有关如何设置密钥的非常详细的指南

First of all, I am well aware of that there are many of questions regarding this topic. I have read them, but still could figure out an appropriate answer for my situation.

I would like to scp the entire ~/cs###/assign1 dir from local to school home dir with a shell script. My question is, is there a way in my script to wait for the password prompt, and then simulate key board event to 'type' in my password?


here is a really detailed guide of how to set up the key

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

扎心 2024-08-12 16:32:10

使用“sshpass”!

#!/bin/bash
sshpass -p "password" scp -r /some/local/path [email protected]:/some/remote/path

Use "sshpass"!

#!/bin/bash
sshpass -p "password" scp -r /some/local/path [email protected]:/some/remote/path
风为裳 2024-08-12 16:32:10

不允许使用 ssh 密钥吗?那将是一个更好的解决方案。

Are ssh keys not allowed? That would be a better solution.

情话难免假 2024-08-12 16:32:10

我认为你不能轻易做到这一点。您可以做的是使用公钥身份验证。

类似的东西

ssh-keygen -t rsa
ssh school mkdir .ssh/
cat ~/.ssh/id_rsa.pub | ssh school "cat >>.ssh/authorized_keys"

(或dsa)。

但是,嘿,这不是服务器故障,不是吗? ;-)

I don't think you can easily do that. What you can do is using public key authentication instead.

Something along these lines

ssh-keygen -t rsa
ssh school mkdir .ssh/
cat ~/.ssh/id_rsa.pub | ssh school "cat >>.ssh/authorized_keys"

(or dsa).

But hey, it's not serverfault, is it? ;-)

绻影浮沉 2024-08-12 16:32:10

考虑使用密钥或外部库。

我认为否则不可能(我希望我没有错),因为它会强制自动暴力入侵和嗅探密码。

有一些库可以做你想做的事情(使用SFTP协议,而不是调用scp),例如libssh

我再次强烈推荐钥匙。

Consider using keys, or an external library.

I don't think it's possible otherwise (I hope I'm not wrong), as it imposes automatic brute force intrusion and sniffing of passwords.

There are libraries that can do what you want (use the SFTP protocol, not calling scp), such as libssh.

Again, I highly recommend keys.

软糖 2024-08-12 16:32:10

我同意你应该使用钥匙。但是,如果您想在纯文本脚本中对密码进行硬编码,expect 可以自动化该过程的交互部分文件。

I agree that you should use keys. But expect can automate the interactive aspect of the process IF you want to hardcode your password in a plain-text script file.

盛夏尉蓝 2024-08-12 16:32:10

你没有说你在家里和学校使用哪个平台。假设 Linux、Cygwin 或 OS/X 您有多种选择:

  1. 公钥身份验证(如果需要)
    尚未在服务器上关闭
  2. ssh-agent 和 ssh-add,以便在每个会话中输入一次密码

对于选项 (1),您可以

  1. 在家里使用以下命令生成密钥对
    ssh-keygen,没有密码
    私钥。请注意,如果其他人使用同一台计算机,则省略密码短语可能不是一个好主意,但您的目标是避免输入密码。
  2. 将公钥上传到您的学校
    帐户并将其放入
    ~/.ssh/authorized_keys
  3. 将 scp 与“-i 身份文件”一起使用
    选项,其中 Identityfile 是
    您的私钥的完整路径。或者,向 .ssh/config 添加一个条目(请参阅手册页)

对于第二个选项,ssh-agent 允许您在每个会话的本地进程中缓存一次密码。您设置了过期时间

You don't say which platform you are using at home and at school. Assuming Linux, Cygwin or OS/X you have several options:

  1. Public key authentication if it
    hasn't been turned off at the server
  2. ssh-agent and ssh-add to enter your password once per session

For option (1), you would

  1. generate a keypair at home using
    ssh-keygen, with no passphrase on
    the private key. Note that omitting a passphrase is probably not a good idea if other people use the same computer, but your objective was to get around having to type in the password.
  2. upload the PUBLIC key to your school
    account and place it in
    ~/.ssh/authorized_keys
  3. Use scp with the "-i identityfile"
    option, where identityfile is the
    full path to your private key. Or, add an entry to .ssh/config (see the man pages)

For the second option, ssh-agent allows you to cache your password in a local process one time per session. You set an expiration time

世界等同你 2024-08-12 16:32:10

如果您可以从 Windows 计算机挂载该目录(例如,通过我大学的 Windows 实验室都使用的 AFS、NFS 或 SMB),您可以使用 pscp 与 -pw 开关。

If you can mount the directory from a Windows machine (e.g. via AFS, NFS or SMB which my university's Windows labs all did), you can use pscp with the -pw switch.

落花随流水 2024-08-12 16:32:10

按照说明设置 ssh-keygen此处,您可以执行

scp -i ~/.ssh/id_rsa /local/path/to/file < a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b392e26243f2e0b223b65282426">[电子邮件受保护]:/path/in/remote/server/

其中 id_rsa 是在 ssh-keygen 设置中生成的本地密钥。

如果您想减少每次输入的次数,可以修改您的 .bash_profile 文件并输入

alias remote_scp='scp -i ~/.ssh/id_rsa /local/path/to/file [email protected]:/path/in/remote/server/

然后从您的终端执行 source ~/.bash_profile。之后,如果您在终端中输入 remote_scp ,它应该无需密码即可运行 scp 命令。

Once you set up ssh-keygen as explained here, you can do

scp -i ~/.ssh/id_rsa /local/path/to/file [email protected]:/path/in/remote/server/

where id_rsa is the local key generated in the ssh-keygen setup.

If you want to lessen typing each time, you can modify your .bash_profile file and put

alias remote_scp='scp -i ~/.ssh/id_rsa /local/path/to/file [email protected]:/path/in/remote/server/

Then from your terminal do source ~/.bash_profile. Afterwards if you type remote_scp in your terminal it should run the scp command without password.

仅此而已 2024-08-12 16:32:10

我发现无法在我们的系统上自动执行此操作 - 我通过 ssh 进入一个设备,然后通过 ssh 到下一个设备,并且在这两个设备上,我无法安装任何其他软件。所以我只能使用现有的shell脚本命令。我可以复制某种类型的所有文件(例如 *.png),也可以在脚本文件中逐个复制,但必须为每个单独的文件输入密码。我做的一件事很有帮助,就是将密码输入到文件中,剪切它,然后将其粘贴到每个文件的每个提示中。

I found no way to do this on our systems automatically - I ssh into one device, and then ssh to the next device, and on both, I cannot install any other software. So I can only use existing shell script commands. I can either copy all the files of a type with, such as *.png, or one by one in my script file, having to enter the password for each individual file. The one thing I did that was helpful was to type the password into a file, cut it, and be able to paste it to each prompt for each file.

故事未完 2024-08-12 16:32:10

正如许多人已经说过的那样,使用 ssh 密钥将是最安全、最好的方法。
如果其他人仍然想知道并寻求帮助,在 Ubuntu 帮助中有一个快速而直接的 使用 ssh 密钥的方式

As many have already said that using ssh keys would be the safest and best way.
If anyone else is still wondering around and searching for help, in Ubuntu help there is a fast and straight forward way to use ssh keys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文