Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some help with a simple script 1

Status
Not open for further replies.

SgtB

IS-IT--Management
Oct 3, 2002
447
0
0
US
I want to say first off that I can't write perl at all. This script was given to me by "hads" at .
Its supposed to use the dig command to query a nameserver for all zones that are tied to a certain subnet. 204.20.203.x. The perl script replaces x with 1-255.

I'd like to modify the script to look at the last two octets instead of just the one. Like 204.20.203.1-255, 204.20.204.1-255 and so on.

Here's the script as I have it now. I really hate asking for this kind of help, but I really don't know the first thing about editing a perl script.

Anyway, here's the script:

#!/usr/bin/perl
#
use strict;
my $i;
my $out;
my $subnet = "64.4.13";

for ($i = 1; $i < 255; $i++) {
$out = `dig -x $subnet.$i +short`;
if (length($out) < 3) {
print &quot;Addr Not found\n&quot;;
}
else {
print $out;
}
}

Thanks!
 
First off does the site where you got this script from allow you to edit it to your own needs?

Shelby
 
He said I could use it, so I'm assuming he'd let me edit it. Its just something this person came up with off the top of his head.
 
I know time is short, but learning perl is well worth whatever time you put into it. Here's a very commented script which should you what you're looking for. I don't have dig on my system, so i could not test that aspect of the script, but the 2 loops work, and the ip address builder does produce legit addresses for dig to use. Any hangups, let me know and I'll test the script on another, dig-equipped machine. -Gabe

Code:
#!/usr/bin/perl
# 
use strict; 
my $octetthird; 
my $octetfourth;
my $ipbuild;
my $ipaddress; 
my $out;
my $subnet = &quot;64.4&quot;; 


#this loops the third octet 1-254
for ($octetthird = 1; $octetthird < 255; $octetthird++) { 

#this loops the fourth octet 1-254 for each value in the third octet
   for ($octetfourth = 1; $octetfourth < 255; $octetfourth++) {

#now that we have the last two octets, we build the ip address
#I built it in two parts, to make testing easier

#First, we concatenate the third octet to what you called the subnet
      $ipbuild = $subnet. &quot;.&quot; .$octetthird;

#Then, we concatenate the fourth octet to that
      $ipaddress = $ipbuild. &quot;.&quot; .$octetfourth;
 
#Now we can make our command:     
      $out = `dig -x $ipaddress +short`; 

#test for the result:
      if (length($out) < 3) {

#if it fails: 
         print &quot;Addr Not found\n&quot;; 
         } 
#if it succeeds:
      else { 
         print $out. &quot;\n&quot;; 
         }
   } 
}
 
Hey thanks for all the help!!! I know it might be a pain to write scripts for other people, but I really do appreciate it! Its working like a charm right now!

One question though. I can pretty much understand everything in the script. Its pretty easy to read. One thing is the &quot;\n&quot; at the end of the print lines. What are those used for?

Again thanks alot for all the help!!!
 
line breaks are accomplished with a &quot;\n&quot; If you were to just put in a carrage return you would get errors. Double space would be &quot;\n\n&quot; Hope that helps.

Shelby
 
Helps alot! Thanks for the clarification!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top