function put_SwVersion_MENAME()
{
output=$(expect -c '
log_user 0
match_max 2000
spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null username@IP
expect "*?Password:*"
send "dummmy\r"
expect "*#"
send "cat /storage/system/abcd.txt\r"
expect "cat /torage/system/abcd.txt\n"
puts '"\$expect_out(buffer)"'
')
echo $output
return $TRUE
}
put_SwVersion_MENAME
输出值包含提示而不是发送输出我如何解析以获得正确的CAT值?
答案 0 :(得分:0)
应该正确提示。由于命令和模式都相同,因此它会导致匹配我们自己发送的命令。 (即cat /storage/system/abcd.txt\r
)。
相反,接下来是匹配提示的通用方法,
set prompt "\r\n(.*?)(#|%|>|\\\$) $"
在这里,我们用反斜杠转义了字面的美元符号,最后一个美元用于行尾的正则表达式。
#!/bin/bash
function put_SwVersion_MENAME() {
output=$(expect -c '
log_user 0
set prompt "\r\n(.*?)(#|%|>|\\\$) $"
spawn ssh dinesh@xxx.xx.xx.xxx
expect "password"
send "welcome123\r"
expect -re $prompt
send "cat /storage/system/abcd.txt\r"
expect -re $prompt
puts "$expect_out(1,string)"
')
echo "===>$output<=="
}
put_SwVersion_MENAME
答案 1 :(得分:0)
我也面临类似的问题,并且在添加\ r之后得到了解决并且期望介于两者之间。这可以帮到你。请试试。
expect "password"
send "welcome123\r"
expect -re $prompt
send "cat /storage/system/abcd.txt\r"
expect -re $prompt
send "\r"
expect -re $prompt
puts "$expect_out(buffer)"