简体   繁体   English

解析Expect_out缓冲区以忽略提示并获取send命令的所需值

[英]parsing expect_out buffer to ignore the prompt and get the required value of send command

 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

The output value is containing the prompt rather than the send output how can i parse to get the correct value CAT? 输出值包含提示而不是发送输出,我该如何解析以获得正确的值CAT?

The prompt should be given properly. 提示应正确给出。 Since the command and pattern both are same, it leads to matching the command which is being sent by us itself. 由于命令和模式都相同,因此导致匹配我们自己发送的命令。 (ie cat /storage/system/abcd.txt\\r ). (即cat /storage/system/abcd.txt\\r )。

Instead, a generalized approach for the matching the prompt is follows, 而是遵循一种与提示匹配的通用方法,

set prompt "\r\n(.*?)(#|%|>|\\\$) $"

Here, we have escaped the literal dollar sign with backslash and the last dollar is meant for the end-of-line regular expression. 在这里,我们用反斜杠转义了美元符号,最后一个美元用于行尾正则表达式。

#!/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

I too have face similar problem and it got resolved after adding an \\r and expect in between. 我也遇到过类似的问题,并且在添加\\ r后期望得到解决。 This can help you. 这可以为您提供帮助。 please try. 请试试。

    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)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM