Where to go?

Hi all,
I was trying to find a thread to post my questions about script but it was a bit confusing.
Sorry if I am posting here.

I am running a Dynamic DNS and want to write a script that every time I use nsupdate (in another scrip that I already wrote) it put the content of DNS data base in a diffreent file:
example:
my zone file is

Code:
$ORIGIN .
$TTL 86400	; 1 day
foo306			IN SOA	ns1.foo306. admin.foo306. (
				2008070104 ; serial
				10800      ; refresh (3 hours)
				3600       ; retry (1 hour)
				604800     ; expire (1 week)
				86400      ; minimum (1 day)
				)
			NS	ns1.foo306.
			MX	1 ns1.foo306.
$ORIGIN het306.
$TTL 600	; 10 minutes
dynamicdns		A	6.6.6.6
$TTL 86400	; 1 day
localhost		A	127.0.0.1
ns1			A	4.4.4.4
roxanne			A	5.5.5.5
$TTL 600	; 10 minutes
roxy			A	2.2.2.2
			A	3.3.3.3

but in my text file I just want have

Code:
dynamicdns			6.6.6.6
localhost			127.0.0.1
ns1				4.4.4.4
roxanne				5.5.5.5
roxy				2.2.2.2
				3.3.3.3

What should I do. I am not familiar with command, grep, cut....
Please help.

Thanks in advance.

Roxanne
P.S. all numbers are pretended.
 
Code:
cat zonefile | perl -ne 's/\;.*//; /^[\s\t]*([^\s\t]*)[\s\t]+A[\s\t]+([a-z0-9_\.\-]+)[\s\t]*$/; print "$1\t$2\n" if $2;'
 
Alt said:
Code:
cat zonefile | perl -ne 's/\;.*//; /^[\s\t]*([^\s\t]*)[\s\t]+A[\s\t]+([a-z0-9_\.\-]+)[\s\t]*$/; print "$1\t$2\n" if $2;'

Some suggestions: [\s\t]* is redundant; \t is included in \s. And [^\s]* can be replaced with \S*. Don't need the character classes, [\s]* might as well be \s*. Don't need to escape semicolon, don't need cat, don't really need (I think) to match anything after the IP address. That all results in:

Code:
perl -ne 's/;.*//; /^\s*(\S*)\s+A\s+(.*)/; print "$1\t$2\n" if $2;' zonefile

(Tested, but only on the sample data.)
 
Thanks you all. May I ask another similar question? I am getting IP addresses from client. For each of them I need to check if they start with 192.168.10.X and if yes do some special task to it. How can I divide it in each octet and check if it is for example 192 and next octed it 168 and so forth?

Cheers
 
Thanks for reply.

However I need a way to make an ip address in a reverse order to update reverse zone.

means
ip is 192.168.10.88
if it is 192.168.10.X then ip is X.10.168.192

Cheers
 
roxanne said:
Thanks for reply.

However I need a way to make an ip address in a reverse order to update reverse zone.

means
ip is 192.168.10.88
if it is 192.168.10.X then ip is X.10.168.192

Okay, more specific is good. I strongly recommend "Mastering Regular Expressions" by Jeffrey Friedl. A very good book, fun to read, and worth buying. There's possibly an easier way of doing this with a Perl module, but here's a quick hack that's cheap enough:

test.pl
Code:
#!/usr/bin/perl

use strict;
use warnings;

chomp;
if (/^192.168.10/) {
    /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;  # capture octets
    print "ip: $_, reverse ip: $4.$3.$2.$1\n";
}

file-of-ips
Code:
1.2.3.4
10.0.0.1
192.168.10.1
192.168.10.2
192.168.1.1
192.168.10.4

Sample run:
Code:
% perl -n test.pl file-of-ips
ip: 192.168.10.1, reverse ip: 1.10.168.192
ip: 192.168.10.2, reverse ip: 2.10.168.192
ip: 192.168.10.4, reverse ip: 4.10.168.192
 
Code:
%perl -n test.pl ip
Can't locate warning.pm in @INC (@INC contains: /usr/local/lib/perl5/5.8.9/BSDPA        N /usr/local/lib/perl5/site_perl/5.8.9/mach 
/usr/local/lib/perl5/site_perl/5.8.9         /usr/local/lib/perl5/5.8.9/mach /usr/local/lib/perl5/5.8.9 .) at test.pl line 4        .
BEGIN failed--compilation aborted at test.pl line 4.

What this error means? I used which perl and it is the one that you put in script.
 
I hope I could understand your commands as I am having similar problem more and more.

Could you please tell me how I can make this file

Code:
230.168.192.in-addr.arpa name server ns1.foo306.
230.168.192.in-addr.arpa name server ns1.bar306.
105.230.168.192.in-addr.arpa domain name pointer footest.foo306.
106.230.168.192.in-addr.arpa domain name pointer bartest.bar306.

like this

Code:
230.168.192.in-addr.arpa  ns1.foo306.
230.168.192.in-addr.arpa ns1.bar306.
105.230.168.192.in-addr.arpa footest.foo306.
106.230.168.192.in-addr.arpa bartest.bar306.


or even like this

Code:
      .230.168.192      ns1.foo306.
      .230.168.192      ns1.bar306.
105   .230.168.192  footest.foo306.
106   .230.168.192  bartest.bar306.
.
.
.

The IP and Host name is changing every time I add or delete an entry.

Cheers
 
Maybe you should just have coffee together since you're both in the same university in the same country.. Do homework together!
 
Here is another way to reverse IP.

Use Net::IP #You can get header file from here... http://search.cpan.org/dist/Net-IP/IP.pm

Code:
my $ip = new Net::IP ($IPadd) || die; # Where IPadd is IP you want to reverse
$revIP = ($ip->reverse_ip());
#If IPadd = 136.186.230.111 the result will be 111.230.186.136.in-addr.arpa

Nice and Easy :)

Cheers
 
Back
Top