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

时间:2015-12-29 06:38:47

标签: remote-access expect

我想为10台PC自动远程配置sensors-detect。 这是一些问题'需要在没有我与这些PC的交互的情况下回答。这些是5个问题'我的首选答案是:

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

问题以随机方式询问,即使是(是/否)',某些(是/否)和某些(是/否)取决于计算机类型。我尝试使用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

1 个答案:

答案 0 :(得分:2)

您可以使用-nocase使Expect具有不区分大小写的模式匹配。

我们不应该在模式中使用带有大写字符的-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,这是我们从此循环中退出的条件。