Convert IP address (within a line) to hostname | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Convert IP address (within a line) to hostname

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-25-2010
scolazz scolazz is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 2 March 2010, 1:45 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Convert IP address (within a line) to hostname

I have a tricky problem, and I'm quite the scripting newb.

I have Cisco ACLs that have IP addresses in them. I'd like to convert the IP's to hostnames for easier analysis. A sample ACL input file would be (I've obfuscated the IPs):


Code:
access-list acl-secure-out line 1 extended permit icmp any any 
access-list acl-secure-out line 2 extended permit tcp host 200.00.000.000 object-group ser-secure-bed eq 2049 
access-list acl-secure-out line 2 extended permit tcp host 200.00.000.000 host 10.0.000.1 eq 2049

Here is the trick - I only want to convert host IPs - so, any IP that follows the word 'host' in the ACL (which I have as a text file).

So far, I've used awk to try something like this:


Code:
awk '$8 ~ /host/ && $10 ~ /host/ {print $0, system ("dig +short -x"$9)system("dig +short -x"$11)}'

but that gives me strange output:


Code:
xxxx.xx.xxx.net.
xxxx.xx.yyyy.com.
00ess-list acl-secure-out line 2 extended permit tcp host 200.00.000.000 host 10.0.000.0 eq 2049

For some reason, the system command output prints out before the whole line, and munges up the line output (see '00ess-list' at the beginning of the 3rd line). I know that command wouldn't do exactly what I wanted, but I think I'm close.

The system command is working OK - it's looking up the hostname just fine, I just need to find a way to do that, and replace the IP in the line with the hostname, but only when the IP is preceded by the word 'host'.

I really hope I'm making sense here Any thoughts?
Sponsored Links
    #2  
Old 02-25-2010
EAGL€ EAGL€ is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 19 August 2014, 9:37 AM EDT
Location: izmir
Posts: 314
Thanks: 29
Thanked 13 Times in 13 Posts
I see two IP addresses coming after "host" in your file, so do you want to get each IP addresses that occurs after each "host" word? Can you illustrate the output you want?
Sponsored Links
    #3  
Old 02-25-2010
scolazz scolazz is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 2 March 2010, 1:45 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by EAGL€ View Post
I see two IP addresses coming after "host" in your file, so do you want to get each IP addresses that occurs after each "host" word? Can you illustrate the output you want?
Yes, exactly what I want. Sample output would look something like this:


Code:
access-list acl-secure-out line 2 extended permit tcp host hostname1.example.com object-group ser-secure-bed eq 2049 
access-list acl-secure-out line 2 extended permit tcp host hostname2.example.com host hostname3.example.com eq 2049

The trick here is, some lines have one host, some have two, and others have none. I need all of the lines in the output file though, with just the IPs resolved.

Thanks again!
    #4  
Old 03-02-2010
scolazz scolazz is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 2 March 2010, 1:45 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
well, perl to the rescue:


Code:
#!/usr/bin/perl
open MYFILE, $ARGV[0] or die $!;
while (<MYFILE>) {
chomp;
#perform work here
($field1,$field2,$field3,$field4,$field5,$field6,$field7,$field8,$field9,$field10,$field11,$field12,$field13) = split(' ');
if ($field10 eq "host" and $field8 eq "host") {
   my $srcip = `dig +short -x $field9`;
   my $dstip = `dig +short -x $field11`;
   chomp $srcip;
   chomp $dstip;
   print "$field1 $field2 $field3 $field4 $field5 $field6 $field7 $field8 $field9($srcip) $field10 $field11($dstip) $field12 $field13\n";
   } 
elsif ($field8 eq "host" and $field10 ne "host") {
   my $srcip = `dig +short -x $field9`;
   chomp $srcip;
   print "$field1 $field2 $field3 $field4 $field5 $field6 $field7 $field8 $field9($srcip) $field10 $field11 $field12 $field13\n";
   } 
elsif ($field10 eq "host") {
   my $srcip = `dig +short -x $field11`;
   chomp $srcip;
   print "$field1 $field2 $field3 $field4 $field5 $field6 $field7 $field8 $field9 $field10 $field11($srcip) $field12 $field13\n";
   }
else { 
   print "$field1 $field2 $field3 $field4 $field5 $field6 $field7 $field8 $field9 $field10 $field11 $field12 $field13\n";}
   }
close (MYFILE);

you put your input file on the command line as an argument. If the word 'host' appears in certain locations, then the next field is assumed to be an IP address. The script then prints the line, with the resolved IP in parenthesis after the raw IP. Obviously, you can redirect output to stdout or to a file.

Not elegant, but it seems to work
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
bash: convert mac address to 16 character format rethink Shell Programming and Scripting 3 02-11-2010 11:35 AM
Read Hostname and Return IP Address PrasannaKS Shell Programming and Scripting 1 11-15-2008 04:17 PM
IP address/hostname problem uLearner UNIX for Dummies Questions & Answers 1 03-17-2008 09:20 AM
Convert ip address to ip number moutaz1983 Shell Programming and Scripting 17 01-03-2008 06:55 AM
Solaris - unknown hostname - how can I change hostname? XNOR UNIX for Dummies Questions & Answers 1 03-29-2007 10:52 PM



All times are GMT -4. The time now is 11:42 AM.