Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何期望百分比和空格_Regex_Expect - Fatal编程技术网

Regex 如何期望百分比和空格

Regex 如何期望百分比和空格,regex,expect,Regex,Expect,我正在编写一个expect脚本来检查内存使用情况,只有在内存使用率低于65%时才能继续执行下一步 #!/usr/bin/expect -f spawn telnet $serverip send "show performance\r" expect { timeout { send_user "\nCPU Usage is too high.\n";exit 1} "0-65%" # i need to expect 0-65% } 然后继续执行其他命令 输出为: C

我正在编写一个expect脚本来检查内存使用情况,只有在内存使用率低于65%时才能继续执行下一步

#!/usr/bin/expect -f
spawn telnet $serverip
send "show performance\r"
expect {
    timeout { send_user "\nCPU Usage is too high.\n";exit 1}
    "0-65%" # i need to expect 0-65%
    }
然后继续执行其他命令

输出为:

CPU used MEM used  RX(Kbps)  TX(Kbps)  RX(Kbps)  TX(Kbps)
1.0%    51.2%      0.000     0.000     1.620     2.426
我需要确保使用的内存小于65%。如何在EXPECT脚本中执行此操作


谢谢你的帮助。它一直在折磨我

-re
标志的帮助下,必须在
expect
本身中使用正则表达式

有两种方法可以做到这一点

  • 匹配所有
    show performance
    命令输出,直到出现提示,然后在该输出中应用tcl的遗留regexp

  • 仅直接匹配所需值(即mem使用的%值)

  • 我假设您的设备的提示将是
    #
    。但是,有些设备的提示可能会有所不同。所以,为了处理这个问题,我们可以提出一个通用的提示模式

    set prompt "#|>|\\\$";
    
    如果您的设备的提示在此中不可用,请包括相同的提示

    #!/usr/bin/expect -f
    
    #This is a common approach for few known prompts
    #If your device's prompt is missing here, then you can add the same.
    set prompt "#|>|\\\$"; # We escaped the `$` symbol with backslash to match literal '$'
    
    spawn telnet $serverip
    
    # Add code for login here
    
    expect -re $prompt;  # Using '-re' flag here to match one one of the prompt.    
    
    # Your some other code here to something if any
    
    # This is to clean up the previous expect_out(buffer) content
    # So that, we can get the exact output what we need.
    expect *;    
    
    send "show performance\r"; # '\r' used here to type 'return' .i.e new line
    expect -re $prompt; # Matching the prompt with regexp
    
    #Now, the content of 'expect_out(buffer)' has what we need
    set output $expect_out(buffer);
    
    # Applying the tcl's regexp here
    if {[regexp {%\s+([^%]+)} $output ignore mem]} {
        puts "Memory used : $mem"
    }
    
    我使用的模式是
    {%\s+([^%]+)}
    。在您的输出中,我们有2个百分比符号。第一个对应于使用的CPU,第二个对应于使用的内存。因此,我基本上是在尝试匹配文本
    %51.2%

    让我来解码这个模式

    %
    -匹配第一个百分号

    \s+
    -匹配多个空格

    [^%]+
    -匹配除%(这是我们获得所需值的地方,即值51.2)以外的任何内容

    那么这里需要括号做什么呢?那是分组的
    Expect
    将匹配的输出保存到
    Expect\u out(0,字符串)
    。对于第n个子匹配,它将保存在
    expect\u out(n,字符串)
    。i、 e.对于第一个子匹配
    expect\u out(1,字符串)
    和第二个子匹配
    expect\u out(2,字符串)
    ,依此类推
    Expect
    将所有匹配和不匹配的输入存储到名为
    Expect\u out(缓冲区)
    的变量中。这就是短篇小说。还有一件事可能会困扰你。这人在这里干什么?你可以看一看,了解更多关于同样的事情

    #!/usr/bin/expect -f
    
    #This is a common approach for few known prompts
    #If your device's prompt is missing here, then you can add the same.
    set prompt "#|>|\\\$"; # We escaped the `$` symbol with backslash to match literal '$'
    
    spawn telnet $serverip
    
    # Add code for login here
    
    expect -re $prompt;  # Using '-re' flag here to match one one of the prompt.    
    
    # Your some other code here to something if any
    
    # This is to clean up the previous expect_out(buffer) content
    # So that, we can get the exact output what we need.
    expect *;    
    
    send "show performance\r"; # '\r' used here to type 'return' .i.e new line
    expect -re $prompt; # Matching the prompt with regexp
    
    #Now, the content of 'expect_out(buffer)' has what we need
    set output $expect_out(buffer);
    
    # Applying the tcl's regexp here
    if {[regexp {%\s+([^%]+)} $output ignore mem]} {
        puts "Memory used : $mem"
    }
    
    这就是第一条路。那么,我上面描述的第二种方法呢?这要容易一点

    send "show performance\r"; 
    expect {
            -re {%\s+([^%]+)} { set mem $expect_out(1,string); puts "Memory used : $mem" }
            timeout { puts timeout_happened }
    }
    
    这看起来更舒适,不需要另外应用单独的
    regexp
    。这是它的一个优点。根据您的要求,您可以使用任何您觉得舒服的和最需要的

    一旦得到值,如果小于65%,就可以将其与
    if
    循环进行比较

    #!/usr/bin/expect -f
    spawn telnet $serverip
    send "show performance\r"
    expect {
        timeout { send_user "\nCPU Usage is too high.\n";exit 1}
        "0-65%" # i need to expect 0-65%
        }