期望没有正确设置Vagrant passwd

时间:2014-11-21 20:33:21

标签: bash expect

我正在运行一个来自bash脚本的子期望脚本,这个脚本在这里提供了一个vms,但是没有得到我预期的结果(ala,password = foo)。这是我正在运行的,帮助appr

## Create generic(s) for Vagrant
groupadd admin
useradd -G admin vagrant
/usr/bin/expect -dc 'expect { 
  eval spawn passwd vagrant 
  set prompt ":|#|\\\$"                       ## use correct prompt
  interact -o -nobuffer -re $prompt return    ## must be done twice due to week passwd
  send "vagrant\r"
  interact -o -nobuffer -re $prompt return
  send "vagrant\r"
  interact
}'

这是调试的屏幕上限

Screen Cap

2 个答案:

答案 0 :(得分:0)

结束

PASSWD=$(expect -c '
  log_user 0
  proc abort {} {
    puts "Error with setting password?"
    exit 1
  }
  spawn passwd vagrant
  expect {
    password:        { send "vagrant\r" }
    default          abort
  }
  expect {
    password:        { send "vagrant\r" }
    default          abort
    eof
  }
  puts "User Vagrant has had password set..."
')

答案 1 :(得分:0)

您的expect代码可以使用exp_continue进行优化,如下所示。

PASSWD=$(expect -c '
  log_user 0
  proc abort {} {
    puts "Error with setting password?"
    exit 1
  }
  spawn passwd vagrant
  expect {
    password:        { send "vagrant\r";exp_continue }
    default          abort
    eof
  }
  puts "User Vagrant has had password set..."
')