bash 使用“expect”自动发送密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1044986/
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-17 21:01:42  来源:igfitidea点击:

Using 'expect' to automatically send in password

linuxbashshellscpexpect

提问by Saobi

I am trying to copy a file from my remote server to my local. Here's my script to run it, by using 'expect' to automaticlally send in password

我正在尝试将文件从远程服务器复制到本地。这是我运行它的脚本,通过使用“expect”自动发送密码

scp user@host:/folder/myFile ./
expect "Password: "
send "myPassword"

When I run this, it still prompts for "Password", what is wrong?

当我运行这个时,它仍然提示“密码”,这是怎么回事?

采纳答案by Sam

From what you are trying to do it sounds like you might be better off using ssh with a public and private key. You could google for ssh-keygen tutorial to get started.

从您尝试做的事情来看,听起来您最好将 ssh 与公钥和私钥一起使用。您可以在 google 上搜索 ssh-keygen 教程以开始使用。

回答by glenn Hymanman

While I agree with Sam and nik, the answer to you questions is that you didn't "hit enter":

虽然我同意 Sam 和 nik,但您问题的答案是您没有“按 Enter”:

send "mypassword\r"

回答by Eden

This expect script does the job (thanks to 'zedwood')

这个期望脚本完成了这项工作(感谢'zedwood'

#!/usr/bin/expect -f
set filename [lindex $argv 0]
set timeout -1
spawn scp $filename [email protected]:/home/myusername/
set pass "mypassword"
expect {
        password: {send "$pass\r" ; exp_continue}
        eof exit
}

回答by nik

please do not even leave such scripts around that will be picked up by someone else and used elsewhere.... do try public key authentication, it is are very easy to setup.

请甚至不要留下这样的脚本,这些脚本会被其他人捡到并在其他地方使用....尝试公钥身份验证,它很容易设置。