在 shell 脚本中使用 SCP 时如何响应密码提示?
首先,我很清楚关于这个话题有很多问题。我已经阅读过它们,但仍然可以找到适合我的情况的答案。
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用“sshpass”!
Use "sshpass"!
不允许使用 ssh 密钥吗?那将是一个更好的解决方案。
Are ssh keys not allowed? That would be a better solution.
我认为你不能轻易做到这一点。您可以做的是使用公钥身份验证。
类似的东西
(或
dsa
)。但是,嘿,这不是服务器故障,不是吗? ;-)
I don't think you can easily do that. What you can do is using public key authentication instead.
Something along these lines
(or
dsa
).But hey, it's not serverfault, is it? ;-)
考虑使用密钥或外部库。
我认为否则不可能(我希望我没有错),因为它会强制自动暴力入侵和嗅探密码。
有一些库可以做你想做的事情(使用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.
我同意你应该使用钥匙。但是,如果您想在纯文本脚本中对密码进行硬编码,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.
像这样的东西 - http://code.google.com/p/enchanter/ ?
Something like this - http://code.google.com/p/enchanter/ ?
你没有说你在家里和学校使用哪个平台。假设 Linux、Cygwin 或 OS/X 您有多种选择:
尚未在服务器上关闭
对于选项 (1),您可以
ssh-keygen,没有密码
私钥。请注意,如果其他人使用同一台计算机,则省略密码短语可能不是一个好主意,但您的目标是避免输入密码。
帐户并将其放入
~/.ssh/authorized_keys
选项,其中 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:
hasn't been turned off at the server
For option (1), you would
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.
account and place it in
~/.ssh/authorized_keys
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
如果您可以从 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.
按照说明设置
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
文件并输入然后从您的终端执行
source ~/.bash_profile
。之后,如果您在终端中输入remote_scp
,它应该无需密码即可运行scp
命令。Once you set up
ssh-keygen
as explained here, you can doscp -i ~/.ssh/id_rsa /local/path/to/file [email protected]:/path/in/remote/server/
where
id_rsa
is the local key generated in thessh-keygen
setup.If you want to lessen typing each time, you can modify your
.bash_profile
file and putThen from your terminal do
source ~/.bash_profile
. Afterwards if you typeremote_scp
in your terminal it should run thescp
command without password.我发现无法在我们的系统上自动执行此操作 - 我通过 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.
正如许多人已经说过的那样,使用 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.