在perl中读取命令的输出

t9aqgxwy  于 2023-08-06  发布在  Perl
关注(0)|答案(3)|浏览(149)

我在运行一个Perl脚本后有一个输出,它有几行,但我唯一需要的是gatewayName:statusCode:“失败”,ex。
编辑:例如3个网关,2个失败2个成功,我只会收到一封电子邮件。这是一批完成后的输出。我想得到任何一个单一的电子邮件后,所有的批次都做了,或者我可以生活在每批不同的电子邮件告诉哪一个失败。

"Policy installation - Site-Firewalls"  failed  (100%)
tasks:
- uid: "e822c095-cb8b-4f8b-be51-c50a3bdf6aad"
  type: "task"
  domain:
    uid: "41e821a0-3720-11e3-aa6e-0800200c9fde"
    name: "SMC User"
    domain-type: "domain"
  task-id: "97be087a-fad4-49ca-adc0-6fa0e32b4518"
  task-name: "Policy installation - Site-Firewalls"
  status: "failed"
  progress-percentage: 100
  start-time:
    posix: 1689859245449
    iso-8601: "2023-07-20T09:20-0400"
  last-update-time:
    posix: 1689859512075
    iso-8601: "2023-07-20T09:25-0400"
  suppressed: false
  task-details:
  - uid: "aa342b56-2f4d-4354-a491-b89812b9bf65"
    name: null
    domain:
      uid: "41e821a0-3720-11e3-aa6e-0800200c9fde"
      name: "SMC User"
      domain-type: "domain"
    color: "black"
    statusCode: "succeeded"
    statusDescription: "Succeeded"
    taskNotification: "e822c095-cb8b-4f8b-be51-c50a3bdf6aad"
    gatewayId: "b74b22ba-cd75-4698-99b9-cc897f501fc1"
    policyId: "e539bfb8-6c3c-4835-a124-53a1b616f9aa"
    fastInstallStatus:
      worksessionId: "3baf8bc3-52ad-42df-be15-37b5ac2aa336"
      gatewayId: "b74b22ba-cd75-4698-99b9-cc897f501fc1"
      policyId: "e539bfb8-6c3c-4835-a124-53a1b616f9aa"
      status: "blade_not_supported"
      detailedReason: []
    runningStages: []
    waitingStages: []
    stagesInfo:
    - type: "info"
      stage: "legacy policy load"
      messages:
      - type: "info"
        message: "Threat Prevention Policy installation was canceled for:"
    cluster: false
    meta-info:
      validation-state: "ok"
      last-modify-time:
        posix: 1689859474791
        iso-8601: "2023-07-20T09:24-0400"
      last-modifier: "System"
      creation-time:
        posix: 1689859246735
        iso-8601: "2023-07-20T09:20-0400"
      creator: "System"
    tags: []
    icon: "General/globalsNa"
    comments: ""
    display-name: ""
    customFields: null
    gatewayName: "SF-Test1"
    revision: "3baf8bc3-52ad-42df-be15-37b5ac2aa336""
- uid: "a61261c0-6f40-4a89-a2b4-ff96c097c703"
    name: null
    domain:
      uid: "41e821a0-3720-11e3-aa6e-0800200c9fde"
      name: "SMC User"
      domain-type: "domain"
    color: "black"
    statusCode: "succeeded"
    statusDescription: "Succeeded"
    taskNotification: "e822c095-cb8b-4f8b-be51-c50a3bdf6aad"
    gatewayId: "786eae4e-4f10-46f5-b0ef-effe426d828e"
    clusterGroupId: "1e34b75a-4a79-4306-97a7-763189253649"
    policyId: "e539bfb8-6c3c-4835-a124-53a1b616f9aa"
    fastInstallStatus:
      worksessionId: "3baf8bc3-52ad-42df-be15-37b5ac2aa336"
      gatewayId: "1e34b75a-4a79-4306-97a7-763189253649"
      policyId: "e539bfb8-6c3c-4835-a124-53a1b616f9aa"
      status: "blade_not_supported"
      detailedReason: []
    runningStages: []
    waitingStages: []
    stagesInfo: []
    cluster: false
    meta-info:
      validation-state: "ok"
      last-modify-time:
        posix: 1689859470158
        iso-8601: "2023-07-20T09:24-0400"
      last-modifier: "System"
      creation-time:
        posix: 1689859246716
        iso-8601: "2023-07-20T09:20-0400"
      creator: "System"
    tags: []
    icon: "General/globalsNa"
    comments: ""
    display-name: ""
    customFields: null
    gatewayName: "SF-Test1"
    revision: "3baf8bc3-52ad-42df-be15-37b5ac2aa336"
  - uid: "b2d97e39-8d7a-447f-a10b-4cc52d562197"
    name: null
    domain:
      uid: "41e821a0-3720-11e3-aa6e-0800200c9fde"
      name: "SMC User"
      domain-type: "domain"
    color: "black"
    statusCode: "failed"
    statusDescription: "Failed"
    taskNotification: "e822c095-cb8b-4f8b-be51-c50a3bdf6aad"
    gatewayId: "cca50637-eed9-4dbd-b64f-1f368e2882d8"
    policyId: "e539bfb8-6c3c-4835-a124-53a1b616f9aa"
    fastInstallStatus:
      worksessionId: "3baf8bc3-52ad-42df-be15-37b5ac2aa336"
      gatewayId: "cca50637-eed9-4dbd-b64f-1f368e2882d8"
      policyId: "e539bfb8-6c3c-4835-a124-53a1b616f9aa"
      status: "blade_not_supported"
      detailedReason: []
    runningStages: []
    waitingStages: []
    stagesInfo:
    - type: "err"
      stage: "legacy policy load"
      messages:
      - type: "err"
        message: "Policy installation had failed due to an internal error. If the problem persists please contact Check Point support"
    cluster: false
    meta-info:
      validation-state: "ok"
      last-modify-time:
        posix: 1689859511828
        iso-8601: "2023-07-20T09:25-0400"
      last-modifier: "System"
      creation-time:
        posix: 1689859246704
        iso-8601: "2023-07-20T09:20-0400"
      creator: "System"
    tags: []
    icon: "General/globalsNa"
    comments: ""
    display-name: ""
    customFields: null
    gatewayName: "SF-Test3"
    revision: "3baf8bc3-52ad-42df-be15-37b5ac2aa336"

字符串
这是我的代码

my $results = qx/$args/;  ## -----This is when the command is executed.
print $results;

          ###DO I NEED TO DO A FOR LOOP ?? How do I say if it fails look for the gateway?
        for ($results) 
        {

        if  ($results =~ /failed/) {
                #my @array  = split("\n", $results);
                #my $len = @array;
                #my $gw1 = $array[$len-1];
                #my @gw = split(/:/,$gw1);

        push  (@fails, $results);
     

        }
    }
## Send the report via e-mail
if (@fails){
  #  my ($fails) = @_;
#my $gateway=$gw[1];
    my $sendmail = '/sbin/sendmail -t';
#print "this is gateway $gateway";
open (SENDMAIL, "|$sendmail") or die "Cannot open sendmail: $!";
print SENDMAIL "From:" . 'admin@test.com';
print SENDMAIL "\n";
print SENDMAIL "To:" . 'email@gmail.com';
print SENDMAIL "\n";
print SENDMAIL "Subject: " . " Failed IPS Clusters\n";
print SENDMAIL "\n";
print SENDMAIL @fails;
close (SENDMAIL);
}
}

sirbozc5

sirbozc51#

当在列表上下文中使用时,qx返回输出行的列表,而不是所有内容都在一个字符串中。然后可以像往常一样循环:

my @results = qx/$args/;
my ($gateway, $status);
for my $line (@results) {
  if ($line =~ /gatewayName: "(.*)"/) {
    $gateway = $1;
  } elsif ($line =~ /statusCode: "(.*)"/) {
    $status = $1;
  }
}

字符串
好的做法是使用词法文件句柄和3-argument form of open

open my $SENDMAIL, "|-", "/sbin/sendmail -t"
  or die "Unable to open pipe to sendmail: $!\n";

7ivaypg9

7ivaypg92#

下面是如何从多行字符串中提取内容的示例:

my $output = <<'END';
name: "SMC User"
      domain-type: "domain"
    color: "black"
    statusCode: "failed"
    statusDescription: "Failed"
customFields: null
    gatewayName: "test-gateway"
    revision: "a26546f2-2328-427d-b2f2-8b1411d09acc"
END

my $status_code;
my $gateway_name;
for my $line (split /\n/, $output) {
    if ($line =~ /^\s*statusCode:\s+"([^"]+?)"/) {
        $status_code = $1;
    }
    if ($line =~ /^\s*gatewayName:\s+"([^"]+?)"/) {
        $gateway_name = $1;
    }
}

if ($status_code eq "failed") {
    # Send the report via e-mail
    open (my $SENDMAIL, '|-', '/sbin/sendmail', '-t') or die "Cannot open sendmail: $!";
    #... print headers and subjects as before..
    print $SENDMAIL "$gateway_name failed\n";
    close $SENDMAIL;
}

字符串

osh3o9ms

osh3o9ms3#

如果我正确理解了你的问题,那么它是,你在Perl脚本中运行命令$args,并将输出存储在$results中。现在你想处理这个,并得到statusCodegatewayName的值,以进一步处理。
以下是您可以执行的操作:

use strict;
use warnings;

# Execute the command and capture the output
my $results = qx/$args/;

# Initialize variables to store the extracted values
my $statusCode;
my $gatewayName;

# Split the output into lines and process each line
foreach my $line (split /\n/, $results) {
    if ($line =~ /statusCode: "([^"]+)"/) {
        $statusCode = $1;
    }
    elsif ($line =~ /gatewayName: "([^"]+)"/) {
        $gatewayName = $1;
    }
}

# Print the extracted values
print "Gateway Name: $gatewayName\n";
print "Status Code: $statusCode\n";

# Check if $statusCode is "failed" to send email
if ($statusCode eq 'failed') {
    # your code here to send email
}

字符串

相关问题