在期望脚本中给出正确答案

giving correct answer in expect script

我想为 10 台 PC 自动远程配置 sensors-detect。 有些 'questions' 需要在我不与那些 PC 互动的情况下回答。这些是 5 'questions' 和我的首选答案:

Some south bridges, CPUs or memory controllers contain embedded sensors. 
Do you want to scan for them? This is totally safe. (YES/no): 

答案是yes

Some Super I/O chips contain embedded sensors. We have to write to
standard I/O ports to probe them. This is usually safe.
Do you want to scan for Super I/O sensors? (YES/no):

答案是yes

Some hardware monitoring chips are accessible through the ISA I/O ports.
We have to write to arbitrary I/O ports to probe them. This is usually
safe though. Yes, you do have ISA I/O ports even if you do not have any
ISA slots! Do you want to scan the ISA I/O ports? (YES/no): 

答案是yes

Some systems (mainly servers) implement IPMI, a set of common interfaces
through which system health data may be retrieved, amongst other things.
We first try to get the information from SMBIOS. If we don't find it
there, we have to read from arbitrary I/O ports to probe for such
interfaces. This is normally safe. Do you want to scan for IPMI
interfaces? (YES/no):

答案是yes

Lastly, we can probe the I2C/SMBus adapters for connected hardware
monitoring devices. This is the most risky part, and while it works
reasonably well on most systems, it has been reported to cause trouble
on some systems.
Do you want to probe the I2C/SMBus adapters now? (YES/no):

答案是no

即使是“(yes/no)”、一些 (YES/no) 和一些 (yes/NO) 也会随机提问,这取决于计算机类型。我尝试使用 Expect 脚本自动执行此操作:

spawn ssh $n2@node2

expect "password:"
send -- "$pa\r"
expect -re $prompt
send -- "echo $pa | sudo -S apt-get install lm-sensors\r"
expect -re $prompt
send -- "sudo sensors-detect\r"

expect {
        "I2C/SMBus adapters now?"
        {   send -- "no\r"
            exp_continue
        }
    "Do you want to scan for them? This is totally safe."
        {   send -- "yes\r"
            exp_continue
        }
    "Super I/O sensors?"
        {   send -- "yes\r"
            exp_continue
        }
    "IPMI interfaces?"
        {   send -- "yes\r"
            exp_continue
        }
    "I/O ports?"
        {   send -- "yes\r"
            exp_continue
        }
}

expect "Just press ENTER to continue: "
send -- "\r"
expect "Do you want to add these lines automatically to /etc/modules? (yes/NO)"
send -- "yes\r"
expect -re $prompt
send -- "sudo service kmod start\r"
expect -re $prompt
send -- "exit\r"
expect eof

不幸的是,脚本没有正确回答并由于超时而退出。一些问题被回答了 3 次。这是错误:

Some south bridges, CPUs or memory controllers contain embedded sensors.
Do you want to scan for them? This is totally safe. (YES/no): yes

Some Super I/O chips contain embedded sensors. We have to write to
standard I/O ports to probe them. This is usually safe.
Do you want to scan for Super I/O sensors? (YES/no): yes

Some systems (mainly servers) implement IPMI, a set of common interfaces
through which system health data may be retrieved, amongst other things.
We first try to get the information from SMBIOS. If we don't find it
there, we have to read from arbitrary I/O ports to probe for such
interfaces. This is normally safe. Do you want to scan for IPMI
interfaces? (YES/no): yes

Some hardware monitoring chips are accessible through the ISA I/O ports.
We have to write to arbitrary I/O ports to probe them. This is usually
safe though. Yes, you do have ISA I/O ports even if you do not have any
ISA slots! Do you want to scan the ISA I/O ports? (yes/NO): yes
yes
yes
yes

Lastly, we can probe the I2C/SMBus adapters for connected hardware
monitoring devices. This is the most risky part, and while it works
reasonably well on most systems, it has been reported to cause trouble
on some systems.
Do you want to probe the I2C/SMBus adapters now? (YES/no): Sorry, no supported PCI bus adapters found.
no

您可以使用 -nocase 使 Expect 具有 case-insensitive 模式匹配。

我们不应该在模式中使用带有大写字符的 -nocase。模式中的大写字符永远无法匹配。

expect -nocase "HI THERE!"  ;# WRONG, CAN NEVER MATCH!
expect -nocase "hi there"   ;# RIGHT!

因此,您的代码可以更改为,

spawn ssh $n2@node2

expect "password:"
send -- "$pa\r"
expect -re $prompt
send -- "echo $pa | sudo -S apt-get install lm-sensors\r"
expect -re $prompt
send -- "sudo sensors-detect\r"
expect {
        -nocase "i2c/smbus adapters now?"  {send "no\r";exp_continue}
        -nocase "(yes/no):" {send  "yes\r";exp_continue}
        "Just press ENTER to continue:" {send "\r"}
}
expect "Do you want to add these lines automatically to /etc/modules? (yes/NO)"
send -- "yes\r"
expect -re $prompt
send -- "sudo service kmod start\r"
expect -re $prompt
send -- "exit\r"
expect eof

在你的情况下,当问题 Do you want to probe the I2C/SMBus adapters now? 出现时,应该回答 no。休息将只有 yes。由于 -nocase 应以小写字母传递,因此我使用 i2c/smbus adapters now? (yes/no):

如果观察到这种模式,那么它会发送 no 如果有其他问题,无论如何它们都会以 (yes/no) 结尾,它会发送 yes.

我在最后保留了模式 Just press ENTER to continue: 而没有 exp_continue,这是我们退出此循环的条件。