我正在自动化以下过程:
通过ssh连接到名为“alpha”的系统,密码为“alpha”,用户名为“alpha”。连接后,我想设置root密码(“kickass”)。我连接的系统默认没有root密码。我写了这个期望脚本来完成这项工作,但它不能始终如一地工作。它工作一次然后如果我再次更改密码以进行测试,它会在发出“sudo passwd root”后等待“输入新的UNIX密码:”提示符。有什么想法吗?
#!/usr/bin/expect -f
set arg1 [lindex $argv 0]
set force_conservative 1 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
set timeout -1
spawn ssh alpha@$arg1
match_max 100000
expect -exact "password: "
send -- "alpha\r"
expect -exact "alpha@alpha:~\$ "
send -- "sudo passwd root\r"
expect -exact "password for alpha: "
send -- "alpha\r"
expect -exact "new UNIX password: "
send -- "kickass\r"
expect -exact "Retype new UNIX password: "
send -- "kickass\r"
expect -exact "alpha@alpha:~\$ "
send -- "exit\r"
expect eof
感谢。
答案 0 :(得分:2)
扩展Andrei Sfrent的评论:
Sudo通常具有某种超时值,因此如果您在超时期限内发出第二个sudo命令,则不会再次要求您输入密码。您可以将代码修改为以下内容:
send -- "sudo passwd root\r"
expect {
-exact "password for alpha: "
{
send -- "alpha\r"
exp_continue
}
-exact "new UNIX password: "
{
send -- "kickass\r"
}
}