djzah71 has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I'm your typical dumb ass newbie to annoy you with stupid questions. I really like perl and am trying to learn it but find myself faulting back to what I sorta know which is bourne and korn scripting.

I have this scenario I have been trying to work on which works like this... ./importns.ksh domains.txt

Purpose of the importns.ksh script is to import Name Servers into Vital QIP as well as BIND Settings and make the primary server stealth.

input file (domains.txt) contains domains as well as reverse records but in the importns.ksh script, it runs two commands.. one command the domain as is listed in domains.txt is fine, the second command it runs if the domains.txt has a reverse record, it needs to alter the variable.

Enough chit chat... I have two scripts that I tried to convert into one with no luck.

Script1 is the original importns.ksh that worked great when I had just typical domains (meaning no reverses records).

#!/bin/ksh # Import NS Servers, version 0.1 # Author - Dennis Hosang # Usage: ./importns.ksh domains.txt 3(ns1-3) OR 5(ns1-5) #for DOMAIN in `cat $1` while IFS=\, read DOMAIN NS do echo "BEGIN $DOMAIN" echo "Add $DOMAIN to QIP" echo "Create input file" echo "Zone=$DOMAIN" > input.txt if [[ $NS == 3 ]]; then echo "dnsServers=QIPDNSMASTER001.hosangit.com +P 0,ns1.hosangit.com S 0,ns2.hosangit.com S 0,ns3.hosangit.com S 0" > +> input.txt elif [[ $NS == 5 ]]; then echo "dnsServers=QIPDNSMASTER001.hosangit.com +P 0,ns1.hosangit.com S 0,ns2.hosangit.com S 0,ns3.hosangit.com S 0,ns +4.hosangit.com S 0,ns5.hosangit.com S 0" >> input.txt fi echo "RefreshTime=3600" >> input.txt echo "ExpirationTime=604800" >> input.txt echo "RetryPeriod=1800" >> input.txt echo "MinimumTTL=3600" >> input.txt echo "NegativeCacheTTL=300" >> input.txt echo "ZoneMail=IPSE-InfrastructureMgmt@hosangit.com" > +> input.txt echo "Extensions" >> input.txt echo " Prefix of zone db file=" >> input.txt echo " Postfix of zone db file=" >> input.txt echo "BIND-8.X Options" >> input.txt echo " allow-query=Any" >> input.txt echo " allow-transfer=Any" >> input.txt echo " allow-update=Any" >> input.txt echo " check-names=Warn" >> input.txt echo " notify=Yes" >> input.txt echo " also-notify= " >> input.txt echo " zone block of named.conf=" >> input.txt echo "BIND-9.X Options" >> input.txt echo " allow-notify=Use Server Value" >> input.txt echo " allow-query=Any" >> input.txt echo " allow-transfer=Any" >> input.txt echo " allow-update=Use Server Value" >> input.txt echo " notify=Yes" >> input.txt echo " zone block of named.conf=" >> input.txt echo "LUCENT DNS 3.X Options" >> input.txt echo " Import External Updates=False" >> input.txt echo " allow-query=Any" >> input.txt echo " allow-transfer=Any" >> input.txt echo " allow-update=Any" >> input.txt echo " check-names=Warn" >> input.txt echo " notify=Yes" >> input.txt echo " also-notify=" >> input.txt echo " zone block of named.conf=" >> input.txt echo "LUCENT DNS 4.X Options" >> input.txt echo " Import External Updates=False" >> input.txt echo " allow-notify=Use Server Value" >> input.txt echo " allow-query=Any" >> input.txt echo " allow-transfer=Any" >> input.txt echo " allow-update=Any" >> input.txt echo " notify=Yes" >> input.txt echo " also-notify=" >> input.txt echo " zone block of named.conf=" >> input.txt echo "WINDOWS 2000 DNS Options" >> input.txt echo " aging=False" >> input.txt echo " allow-transfer=Use List" >> input.txt echo " List=" >> input.txt echo " allow-update=No" >> input.txt echo " no-refresh-interval=0" >> input.txt echo " notify=Use List" >> input.txt echo " List=" >> input.txt echo " refresh-interval=0" >> input.txt echo " zone-options=" >> input.txt echo "input.txt created for $DOMAIN" echo "Run qip-setzoneprof to import NS servers for $DOMAIN" /qip/usr/bin/qip-setzoneprof -n $DOMAIN -u qipusr -p +pass1234 -s enterprise -g 192.168.24.76 -o "HPES Internet" -f input. +txt echo "Create input2.txt for Stealth Server Setting for + $DOMAIN" echo "INSERTDOMAINHERE" > QIPDNSMASTER001-input2.txt echo "Primary=QIPDNSMASTER001.hosangit.com" >> QIPDNSM +ASTER001-input2.txt echo "Secondary=" >> QIPDNSMASTER001-input2.txt echo "BIND-9.X Options=Use Zone Value" >> QIPDNSMASTER +001-input2.txt echo "Stealth Server=True" >> QIPDNSMASTER001-input2.t +xt echo "Done creating input2.txt" echo "Setting Stealth Server option for $DOMAIN" /bin/sed "s/INSERTDOMAINHERE/$DOMAIN/g" QIPDNSMASTER00 +1-input2.txt > ./output/QIPDNSMASTER001."$DOMAIN" /qip/usr/bin/qip-setzoneserveroptions -u qipusr -p pas +s1234 -s enterprise -g 192.168.24.76 -o "HPES Internet" -t domain -f +./output/QIPDNSMASTER001."$DOMAIN" echo "FINISHED with $DOMAIN" echo "------------------------------------------------ +-----------------" done <$1
Typical INPUT FILE
250.26.192.in-addr.arpa,3 251.26.192.in-addr.arpa,3 252.26.192.in-addr.arpa,3 253.26.192.in-addr.arpa,3 254.26.192.in-addr.arpa,3 hosangit.com djzah.com
So the script works fine against hosangit.com or djzah.com but not for the reverse records. So referencing above, the command I created the following script (parseip.ksh) that appeared to work at first to put the address in an array and then rearrange and decipher if its a /24 or /16
#!/bin/ksh # #while read INPUT ; do for INPUT in `cat ${1}` do if [[ $INPUT == *.in-addr.arpa ]]; then IFS=. read -a Array <<< "$INPUT" #echo "$IFS" # notice that IFS didn't change afterward +s if [[ ${Array[4]} == arpa ]]; then domain="${Array[2]}.${Array[1]}.${Array[0]}.0/ +24" elif [[ ${Array[3]} == arpa ]]; then domain="${Array[1]}.${Array[0]}.0.0/16" fi else echo "Whoops" fi echo $1 echo $domain done <$1
NOTE: It worked fine when testing on my mac but when I ran on the redhat server I am getting an error about the read -a command.
./parseip.ksh[6]: read: -a: unknown option Usage: read [-Aprsv] [-d delim] [-u fd] [-t timeout] [-n nbyte] [-N nb +yte] [var?prompt] [var ...]
This is the long way of asking for some direction on how I could possibly do the same thing with perl. I believe it will solve my read -a error as well as be cleaner

Replies are listed 'Best First'.
Re: Method to Parse IP 2 Array
by wjw (Priest) on May 22, 2014 at 21:02 UTC

    Perhaps something like this?

    #!/usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { chomp $line; if ($line =~ m/in-addr/) { my @line = split(/\./, $line); if ($line[2] =~m/in-addr/) { process_line("$line[1]\." ."$line[0]\." . "0\." . "0" . " +\/24"); } else { process_line("$line[2]\." . "$line[1]\." ."$line[0]\." . " +0" . "\/16" ); } } else { process_line($line); } } sub process_line { print shift . "\n"; # do whatever... } __DATA__ 250.26.192.in-addr.arpa,3 251.26.192.in-addr.arpa,3 252.26.192.in-addr.arpa,3 253.26.192.in-addr.arpa,3 254.26.192.in-addr.arpa,3 26.192.in-addr.arpa,2 hosangit.com djzah.com

    Output

    192.26.250.0/16 192.26.251.0/16 192.26.252.0/16 192.26.253.0/16 192.26.254.0/16 192.26.0.0/24 hosangit.com djzah.com

    Hope that is helpful....

    ...the majority is always wrong, and always the last to know about it...

    Insanity: Doing the same thing over and over again and expecting different results...

      Thank You, that makes more sense. Could you give me direction on how to use an input.txt file instead of putting everything under the __DATA__ section

        Anonymous is correct, you could use those modules, in the interest of sticking with very basic code...

        #!/usr/bin/perl use strict; use warnings; my $file = "input.txt"; open(IN, "<", $file) or die (Can't open $file, $!\n"); my @in = <IN>; chomp @in; #all records(lines) from the file are now in array @in an +d line endings have been removed(\n or \r\n) close IN; #so you don't need to hold the file open anymore, close i +t while (my $line = @in){ #you could do a foreach here too, the + rest of this should be familiar to you if ($line =~ m/in-addr/) { my @line = split(/\./, $line); if ($line[2] =~m/in-addr/) { process_line("$line[1]\." ."$line[0]\." . "0\." . "0" . " +\/24"); } else { process_line("$line[2]\." . "$line[1]\." ."$line[0]\." . " +0" . "\/16" ); } } else { process_line($line); } } sub process_line { print shift . "\n"; # do whatever... }

        It is assumed that your text file is not gigabits in size, thus read it into an array instead of holding the file open. You could also just hold the file open and read it line by line, then close it when processing is complete. My preference is to not do that. And it is nothing but a preference...

        I did not test this time, so you may find an typo or two, but am sure you can handle that with warnings turned on and all...

        If you are just learning Perl, you might want to fire this up using 'perl -d (whatever you name this script). The debugger (-d) is a nice way to step through and find out how things work. You can find info in the debugger here, in the pods, or on the web...

        enjoy

        ...the majority is always wrong, and always the last to know about it...

        Insanity: Doing the same thing over and over again and expecting different results...