Home > Software engineering >  Perl, input lines from Csv into a system command issues with reading whole csv
Perl, input lines from Csv into a system command issues with reading whole csv

Time:04-06

I'm able to run the command now but its only reading the first line of the csv for now I only have to columns like this.

name,ipaddr
Athens_GA_VRoom,151.110.32.30
Belmond_IA_VRoom,151.110.33.250

it only does the first line with IP the second it doesn't

#!/usr/bin/perl
#!/bin/bash
#use strict;
use warnings;
use diagnostics;



    my $filename = "ssg_test1.csv";
    open(INPUT, '<', $filename) or die "Cannot open $filename";
   my $line = <INPUT>;
        while ($line = <INPUT>)
        {

        chomp $line;
        push (@filename, $_);

        my @name = split(',', $line);
        my @ip = split(',', $line);
        my $node = $name[0];
        my $node1 = $ip[1];

        system ("mgmt_cli add generic-object create \"com.checkpoint.objects.classes.dummy.CpmiGatewayPlain\" name \"$node\" ipaddr \"$node1\" thirdPartyEncryption \"True\" osInfo.osName \"Gaia\"
vpn.create \"com.checkpoint.objects.classes.dummy.CpmiVpn\" vpn.owned-object.vpnClientsSettingsForGateway.create \"com.checkpoint.objects.classes.dummy.CpmiVpnClientsSettingsForGateway\" vpn.
owned-object.vpnClientsSettingsForGateway.owned-object.endpointVpnClientSettings.create \"com.checkpoint.objects.classes.dummy.CpmiEndpointVpnClientSettingsForGateway\" vpn.owned-object.vpnCli
entsSettingsForGateway.owned-object.endpointVpnClientSettings.owned-object.endpointVpnEnable \"True\" vpn.owned-object.ike.create \"com.checkpoint.objects.classes.dummy.CpmiIke\" vpn.owned-obj
ect.sslNe.create \"com.checkpoint.objects.classes.dummy.CpmiSslNetworkExtender\" vpn.owned-object.sslNe.owned-object.sslEnable \"False\" vpn.owned-object.sslNe.owned-object.gwCertificate \"def
aultCert\" vpn.owned-object.isakmpUniversalSupport \"True\"");



}
    close (INPUT);

CodePudding user response:

To debug this I replaced your big system() call with the line:

print "$node / $node1\n";

And I get the following output:

Athens_GA_VRoom / 151.110.32.30
Belmond_IA_VRoom / 151.110.33.250

So it is processing all of the lines in your input file. I'm not sure why you're not seeing that.

A few other tips that might help you.

  • Having a shell shebang line as well as the Perl one is pointless and confusing.
  • Don't comment out use strict (it's telling you that you haven't declared the @filename array).
  • push(@filename, $_) is doing nothing useful as you haven't set $_ to anything (so it contains undef).
  • The first argument to split() is a regex. So split(/,/, ...) rather than split(',', ...) (your version works but mine is, I think, clearer).
  • Why split $line twice into two separate arrays? All you really want is my ($name, $ip) = split(/,/, $line).
  • If you use qq(...) instead of "..." to build the string in your system() call then you can remove all of the \ that make it look too noisy.
  • Careful indentation will make your code easier to read.

CodePudding user response:

The following demo code demonstrates how you could alter your code to make it more readable.

You can use system call with a command stored in a variable and arguments stored in an array.

#!/usr/bin/env perl

use strict;
use warnings;
use feature 'say';

my $fname = "ssg_test1.csv";

open my $fh, '<', $fname
        or die "Couldn't open $fname";

my @fields = split(',',<$fh>);

while( <$fh> ) {
        chomp;
        my($name,$ipaddr) = split(',',$_);
        say "$name :: $ipaddr";
        #system($cmd,@args);
}

close $fh;

exit 0;

For example system call could look like following

my $cmd = 'mgmt_cli';
my @args = qw(
    add generic-object 
    create com.checkpoint.objects.classes.dummy.CpmiGatewayPlain 
    name $name 
    ipaddr $ipaddr 
    thirdPartyEncryption True 
    osInfo.osName Gaia
    vpn.create com.checkpoint.objects.classes.dummy.CpmiVpn
    vpn.owned-object.vpnClientsSettingsForGateway.create com.checkpoint.objects.classes.dummy.CpmiVpnClientsSettingsForGateway
    vpn.owned-object.vpnClientsSettingsForGateway.owned-object.endpointVpnClientSettings.create com.checkpoint.objects.classes.dummy.CpmiEndpointVpnClientSettingsForGateway
    vpn.owned-object.vpnClientsSettingsForGateway.owned-object.endpointVpnClientSettings.owned-object.endpointVpnEnable True
    vpn.owned-object.ike.create com.checkpoint.objects.classes.dummy.CpmiIke
    vpn.owned-object.sslNe.create com.checkpoint.objects.classes.dummy.CpmiSslNetworkExtender
    vpn.owned-object.sslNe.owned-object.sslEnable False
    vpn.owned-object.sslNe.owned-object.gwCertificate defaultCert
    vpn.owned-object.isakmpUniversalSupport True
);

system($cmd, @args);

Recommendation: please find some time for reading free book Modern Perl to improve your Perl programming style

  •  Tags:  
  • perl
  • Related