Reputation: 203
I have a config file that needs to import a list of IPs and display them like this
acl ip_1 myip 1.1.1.1
tcp_outgoing_address 1.1.1.1 ip_1
cl ip_2 myip 1.1.1.2
tcp_outgoing_address 1.1.1.2 ip_2
cl ip_2 myip 1.1.1.3
tcp_outgoing_address 1.1.1.3 ip_3
and so on.
I found this script but the problem with it is that it will only do ip_1 and ip_2. I have hundreds of IPs to import.
se strict;
use warnings;
open (my $tmph,"<", $ARGV[0]) or die "Error open file $ARGV[0]";
while (<$tmph>)
{
chomp;
my $line=$_;
if ($line=~/\d+\.\d+\.\d+\.\d+/)
{
print ("acl ip1 myip $line\n");
print ("tcp_outgoing_address $line ip1\n\n");
print ("acl ip2 myip $line\n");
print ("tcp_outgoing_address $line ip2\n\n");
}
}
close ($tmph);
Any help will be greatly appreciated.
Upvotes: 1
Views: 74
Reputation: 1641
I edited your code as follow
#!/usr/bin/perl
use strict;
use warnings;
my $n = 1;
open (my $tmph,"<", $ARGV[0]) or die "Error open file $ARGV[0]";
while (<$tmph>)
{
chomp;
my $line=$_;
if ($line=~/\d+\.\d+\.\d+\.\d+/)
{
print ("acl ip$n myip $line\n");
print ("tcp_outgoing_address $line ip$n\n\n");
$n++;
}
}
close ($tmph);
script output
perl do.pl ip.txt
acl ip1 myip 1.2.3.4
tcp_outgoing_address 1.2.3.4 ip1
acl ip2 myip 10.0.0.10
tcp_outgoing_address 10.0.0.10 ip2
acl ip3 myip 192.168.0.10
tcp_outgoing_address 192.168.0.10 ip3
acl ip4 myip 10.0.0.30
tcp_outgoing_address 10.0.0.30 ip4
Upvotes: 1