[期望脚本解析命令输出]

问题描述 投票:-1回答:1

Summary

期待脚本+ ssh =>检查点防火墙

使用expect脚本时需要解析命令的输出。

目标是为每个网络对象提取IP地址(subnet4 :)和名称(名称:)。然后将提取的IP地址与输入的地址进行比较,如果相同,则在防火墙上创建访问规则时使用$ name变量(在将其与相应的IP相关联之后)作为服务。

Command_Output_To_Be_Parsed

objects:
 - uid: "67c51c9a-81e3-43ee-ba96-b733c7672d83"
 name: "CP_default_Office_Mode_addresses_pool"
 type: "network"
 domain:
   uid: "41e821a0-3720-11e3-aa6e-0800200c9fde"
   name: "SMC User"
   domain-type: "domain"
 subnet4: "172.16.10.0"
 mask-length4: 24
 subnet-mask: "255.255.255.0"
 - uid: "caee1116-8087-4310-9208-b422d3628a7e"
 name: "IPv6_Link_Local_Hosts"
 type: "network"
 domain:
   uid: "a0bbbc99-adef-4ef8-bb6d-defdefdefdef"
   name: "Check Point Data"
   domain-type: "data domain"
 subnet6: "fe80::"
 mask-length6: 64
 from: 1
 to: 2
 total: 2

What i tried so far

1)无法将唯一的名称输入$ name变量

2)没有弄清楚如何将IP地址与名称联系起来

send      "mgmt add network name DuplicateTst subnet $srcIPAddr mask-length   32\r"
expect {
-re "same" {
       expect ">"
       send   "set clienv rows 0\r"
       expect ">"
       send   "mgmt show networks\r"
       expect {
         -re "objects(.*)>" {
            set Outcome  $expect_out(1,string)
            puts "The whole array $Outcome"
            #exit 100
              }
           }
       #timeout { puts "timeout"}
 }
}

set lines [split $Outcome "\n"]
foreach line $lines {
    #puts "line: $line"
    set notCleanIP [regexp -inline -lineanchor -all -- {(?:)(\s+)\"(?!255) \d+\.\d+\.\d+\.\d+\"} $line];
    set notCleanName [regexp -inline -lineanchor -all -- {(?:)(\s+)name:  \"[a-z A-Z 0-9]+\"} $line ];
    set cleanName [regexp -inline -lineanchor -all -- {(?:)[a-z A-Z 0-9]+} $notCleanName ]
    set cleanIP [regexp -inline -lineanchor -all -- {(?:)\d+\.\d+\.\d+\.\d+} $notCleanIP ];
    puts "CleanIP   >>> $cleanIP"
    puts "CleanName >>> $cleanName"
    if { $cleanIP == $srcIPAddr } {
           #puts "equivalent *************************"
           #set cleanName [regexp -inline -lineanchor -all -- {(?:)[a-z A-Z 0-9]+} $notCleanName ];
           #puts "################# clean name >> $cleanName"

   }
    #set newVariable2 [regexp -inline -lineanchor -all -- {(?:)(\s+)\"[a-z A-Z 0-9]+\"} $line];
    puts "NotCleanIP   >>> $notCleanIP"
    puts "NotCleanName >>> $notCleanName"
}

exit 100

Result

{  name} { } AddressSrc8b3c855163437d0be28cc8995864 { } {  }

这不是我的预期。仅预期:AddressSrc8b3c855163437d0be28cc8995864

CleanIP >>> 100.5.5.5正确提取IP地址。

Question

如何解决这个问题?任何想法将不胜感激。

谢谢。

parsing output expect
1个回答
0
投票

@Emily E.请记住nextTime。谢谢。

得到它的工作。解决方案是输入expertMode(root)并使用jq。

 # for((i=0;i<=2;i++));do VV=$(jq  -cr ".objects[$i].subnet4" jsonOutput)    VVV="100.5.5.5";for nbr in $VV; do if [[ $nbr =~ $VVV ]];then echo "$nbr $(jq -cr ".objects[$i].name" jsonOutput)";fi;done;done
result => 100.5.5.5 SrcAddressObj8b3c855163437d0be28cc8995864

以上内容适用于shell配置为“/ bin / bash”而不是gaiaCLI的用户。

对于gaiaCLI用户:

send   "for((i=0;i<=3;i++));do VV=\$(jq -c -r \".objects\[\$i\].subnet4\"    /tmp/jsonOutput) VVV=\$V;for nbr in \$VV; do if \[\[ \$nbr =~ \$\{VV
V\} \]\];then echo \"\$nbr \$(jq -c -r \".objects\[\$i\].name\"   /tmp/jsonOutput)\";fi;done;done\r"
            #expect "#"
            sleep 15
            expect {
              -re "done(.*)\n" {
                set shutsu $expect_out(1,string)
                puts "$shutsu"
                }
               }
            sleep 15
            expect "#"
            send   "exit\r"
            expect ">"
            set lines [split $shutsu "\n"]
            foreach line $lines {
                    set objectIP   [regexp -inline -lineanchor -all -- {\d+\.\d+\.\d+\.\d+} $line];
                    set objectName [regexp -inline -lineanchor -all -- {(?:)(\s)[a-z A-Z 0-9]+} $line];
                    set objName    [regexp -inline -lineanchor -all -- {(?!\s)[a-zA-Z 0-9]+} $objectName];
                    if { $objectIP == $srcIPAddr } {
                            puts "objectIP: $objectIP and objectName: $objName"
                            set service $objName
                            puts "finally: here is the service to use =>  $service"
© www.soinside.com 2019 - 2024. All rights reserved.