【问题标题】:bash, expect password is no passing in [duplicate]bash,期望密码没有传入[重复]
【发布时间】:2016-01-22 14:07:57
【问题描述】:

我正在尝试使用 scp,我需要与之通信的盒子没有钥匙。 我从在线搜索中尝试了此代码及其许多变体,但由于某种原因它没有被输入。 如果你能帮助我,那就太好了。提前致谢。 顺便说一句,我正在执行在 mac 上测试的 bash 脚本。

#!/bin/bash

#$1- source file
#$2- destination
#$3- password

/usr/bin/expect << EOF
spawn scp -rp $1  $2
set pass $3
expect -re "password:"
send "password\r" 
expect "\r"
send "\r\n"
EOF

结果是;

spawn scp -rp file1 user@x.com:/path/file2
me@whoknows.com's password: 
return value was : 0

【问题讨论】:

  • 谢谢,我估计是在expect里面用的吧?

标签: bash passwords expect scp spawn


【解决方案1】:

你要等待scp命令的end of file (eof)

#!/bin/bash

#$1- source file
#$2- destination
#$3- password

/usr/bin/expect << EOF
spawn scp -rp $1  $2
set pass $3; # Instead, you can also directly use the '$3' for password
puts "pwd = \$pass"
expect -re "password:"
send "\$pass\r" 
expect eof
EOF

输出:

dinesh@dinesh-VirtualBox:~/stackoverflow$ ./vmaxer srcfile dinesh@xxx.xxx.xx.xxx:/home/dinesh welcome!2E
spawn scp -rp srcfile dsivaji@xxx.xxx.xx.xxx:/home/dinesh
pwd = welcome!2E
dinesh@xxx.xxx.xx.xxx's password: 
srcfile                                                        100%  281     0.3KB/s   00:00    
dinesh@dinesh-VirtualBox:~/stackoverflow$ 

注意:如果您的文件太大并且花费的时间超过Expect 的默认超时时间(10 秒),您应该增加timeout 的值。

可以这样实现,

set timeout 60; # This will make the timeout value to '1 min'

【讨论】:

  • 是的,这个文件非常大,确切地说是 23M。我还应该使用交互语句吗?
  • interact 不需要。如前所述,增加timeout 值
猜你喜欢
  • 2020-03-08
  • 2018-07-05
  • 2010-11-05
  • 2017-06-20
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
相关资源
最近更新 更多