在 Bash 中使用 'expect',响应顺序任意
Use 'expect' in Bash with responses in any order
我正在使用这个期望文件 (keygen.exp) 生成 SSH 密钥,我正在使用 ssh-keygen 作为示例来测试 'expect':
#!/usr/bin/expect
spawn ssh-keygen
expect "Enter file"
send "./id_rsa\r"
expect "Enter passphrase"
send "\r"
expect "Enter same passphrase"
send "\r"
interact
它工作得很好,但是,如果每个 运行 的输入提示顺序不一样怎么办,并且在不同的 运行 上可能会有不同的问题?
我想要这样的东西:
#!/usr/bin/expect
spawn ssh-keygen
expect if-is "Enter file"
send "./id_rsa\r"
expect if-is "Enter passphrase"
send "\r"
expect if-is "Enter same passphrase"
send "\r"
interact
可能吗?
你可以有一个循环的 expect 语句,以不同的方式处理不同的输出,比如:
expect {
"Enter file" {
send "./id_rsa\r"
exp_continue
}
"Enter passphrase" {
send "\r"
exp_continue
}
"Enter same passphrase" {
send "\r"
exp_continue
}
$
}
但请注意,您需要一些方法来跳出循环。这里的最后一个模式 - $
没有 exp_continue
(或任何其他操作)所以它将跳出循环 if 登录时得到的提示包括$
。
请参阅 https://www.tcl.tk/man/expect5.31/expect.1.html
中的完整文档
以任何顺序期待,无限超时:
#!/usr/bin/expect
spawn /path/to/some-programme
expect {
"some string" {
set timeout -1
send "some input\r"
exp_continue
}
"some other string" {
set timeout -1
send "some other input\r"
exp_continue
}
...
}
#no 'interact'
#end of file
我正在使用这个期望文件 (keygen.exp) 生成 SSH 密钥,我正在使用 ssh-keygen 作为示例来测试 'expect':
#!/usr/bin/expect
spawn ssh-keygen
expect "Enter file"
send "./id_rsa\r"
expect "Enter passphrase"
send "\r"
expect "Enter same passphrase"
send "\r"
interact
它工作得很好,但是,如果每个 运行 的输入提示顺序不一样怎么办,并且在不同的 运行 上可能会有不同的问题?
我想要这样的东西:
#!/usr/bin/expect
spawn ssh-keygen
expect if-is "Enter file"
send "./id_rsa\r"
expect if-is "Enter passphrase"
send "\r"
expect if-is "Enter same passphrase"
send "\r"
interact
可能吗?
你可以有一个循环的 expect 语句,以不同的方式处理不同的输出,比如:
expect {
"Enter file" {
send "./id_rsa\r"
exp_continue
}
"Enter passphrase" {
send "\r"
exp_continue
}
"Enter same passphrase" {
send "\r"
exp_continue
}
$
}
但请注意,您需要一些方法来跳出循环。这里的最后一个模式 - $
没有 exp_continue
(或任何其他操作)所以它将跳出循环 if 登录时得到的提示包括$
。
请参阅 https://www.tcl.tk/man/expect5.31/expect.1.html
以任何顺序期待,无限超时:
#!/usr/bin/expect
spawn /path/to/some-programme
expect {
"some string" {
set timeout -1
send "some input\r"
exp_continue
}
"some other string" {
set timeout -1
send "some other input\r"
exp_continue
}
...
}
#no 'interact'
#end of file