try expect语句打印错误相关细腻
时间: 2023-08-15 20:05:50 浏览: 43
在使用 `expect` 命令时,可以通过捕获错误信息并打印出来,以便更好地调试脚本。以下是一个示例脚本:
```
#!/usr/bin/expect
set timeout 10
set username "user"
set password "pass"
spawn ssh $username@host
expect {
"password:" {
send "$password\r"
exp_continue
}
"Permission denied" {
puts "ERROR: Permission denied!"
exit 1
}
"Connection refused" {
puts "ERROR: Connection refused!"
exit 1
}
timeout {
puts "ERROR: Connection timed out!"
exit 1
}
eof {
puts "ERROR: Unexpected end of file!"
exit 1
}
"*#" {
send "ls\r"
expect "*#"
puts $expect_out(buffer)
}
}
```
在此脚本中,我们使用 `expect` 命令捕获了不同的错误信息,并在发生错误时打印出相应的错误消息。例如,在登录时如果出现 "Permission denied" 错误,我们将打印出 "ERROR: Permission denied!"。同样地,如果出现 "Connection refused"、"Connection timed out" 或 "Unexpected end of file" 错误,我们将打印出相应的错误消息。
注意,在 `expect` 命令中,我们使用了 `exp_continue` 命令来继续等待下一个匹配项,以便在出现 "password:" 时输入密码。