Sponsored Content
Top Forums Shell Programming and Scripting Convert IP address (within a line) to hostname Post 302400198 by scolazz on Tuesday 2nd of March 2010 01:45:51 PM
Old 03-02-2010
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 Smilie
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert ip address to ip number

I want to make a script to read a list of ip addresses from a file then convert those ip addresses to ip number. ip number is made by removing the dots then transfer to a number , so the ip number transfered to binary then to decimal which will represents the ip number 0 : 4294967295 Also I... (17 Replies)
Discussion started by: moutaz1983
17 Replies

2. UNIX for Dummies Questions & Answers

IP address/hostname problem

I have a problem that when I am pinging with the IP address it is giving me whether a server is alive or not but when I am trying it with hostname it is giving unknown host I have checked in /etc/hosts file and the IP address and hostname are both there...still the problem is there.. Can... (1 Reply)
Discussion started by: uLearner
1 Replies

3. Shell Programming and Scripting

Read Hostname and Return IP Address

Dear Experts, I have a text file on my itanium box which contains list of nearly 1000 hostnames. I do not have the IP Address of them. I want read each entry from the text file and want to do a nslookup to the DNS Server and get the IP Adresses and put them in another file in the format... (1 Reply)
Discussion started by: PrasannaKS
1 Replies

4. Shell Programming and Scripting

bash: convert mac address to 16 character format

Hi there Im not quite sure how i can do this, but i am retrieving the mac address from boxes, which in some instances is arriving in its shortened format (i.e. dropping the leading zeros)... for example 0:3:BA:1:E:84 Im trying to figure out a way of converting the single character... (3 Replies)
Discussion started by: rethink
3 Replies

5. UNIX for Dummies Questions & Answers

Routing or Iptables connections by hostname or ip address

edit; I found a solution that works, see thread #3 https://www.unix.com/302417065-post3.html Hi there, I have a small dedicated server that has four ip addresses and by default my httpd sends request using the servers main ip for all outbound connections. I'm quite amateur at routing and... (4 Replies)
Discussion started by: mooofa
4 Replies

6. Emergency UNIX and Linux Support

HP UX - ILO Console hostname different than Machine Hostname...

Hi All, So we added a new HP-UX 11.31 machine. Copied OS via Ignite-UX (DVD)over from this machine called machine_a. It was supposed to be named machine_c. And it is when you log in...however when I'm in the ILO console before logging in, it says: It should say: What gives? And how do... (4 Replies)
Discussion started by: zixzix01
4 Replies

7. UNIX for Dummies Questions & Answers

Solaris - How to get IP Address from a hostname?

We are using Solaris as our database OS. Can you please help me to know how can i get the IP address of a solaris machine in my network from its hostname? e.g. We have a remote database server with the hostname as xyz. Without connecting to the database OS, can i get the IP address of xyz OS from... (1 Reply)
Discussion started by: Anmol Sharma
1 Replies

8. AIX

AIX How to exchange hostname and IP address between two AIX servers?

Hi all, I am trying to exchange hostname and IP address of two AIX machines. But i am confused as how to change it ? do i need to use "smitty mktcpip" or "smitty tcpip" ? what is the difference between smitty mktcpip and smitty tcpip ? Also anymore steps to follow or just updating... (3 Replies)
Discussion started by: lramsb4u
3 Replies

9. AIX

Mutt from address and hostname

Mutt 1.2.3.1i AIX 5.3.12.1 We need to send email to external addresses. But are having problems with the sent-from hostname. They're going out with @server.mycompany.com. We need them to be @mycompany.com. I'm setting from and hostname with -e 'set from="From... (1 Reply)
Discussion started by: Papa Lee
1 Replies

10. Solaris

SMTP relay one (1) hostname with two (2) IP Address

Hi, Anyone can help? Currently : I'm am using a smtp smart relay, 1 hostname but different IP Address 192.168.2.108 and 192.168.2.109). I'm using "smtp.mailhostname.com mailhost" and having an IP Address 192.168.2.108, but there is a certain minutes/seconds that the IP Address will... (2 Replies)
Discussion started by: fspalero
2 Replies
UCARP(8)						      System Manager's Manual							  UCARP(8)

NAME
ucarp -- Automatic IP failover SYNOPSIS
ucarp [-i, --interface=INTERFACE] [-s, --srcip=IPADDRESS] [-v, --vhid=VHID] [-p, --pass=PASSWORD] [-o, --passfile=PASSFILE] [-P, --preempt] [-n, --neutral] [-a, --addr=IPADDR] [-h, --help] [-b, --advbase=SECS] [-k, --advskew=SKEW] [-u, --upscript=SCRIPT] [-d, --downscript=SCRIPT] [-r, --deadratio=RATIO] [-z, --shutdown] [-B, --daemonize] [-f, --facility=FACILITY] [-x, --xparam PARAM] [-S, --ignoreifstate] [-M, --nomcast] DESCRIPTION
ucarp allows a pair of hosts to share common IP addresses in order to provide automatic failover of an address from one machine to another. It is a portable userland implementation of the secure and patent-free Common Address Redundancy Protocol, (CARP), OpenBSD's alternative to VRRP. OPTIONS
ucarp supports the following command line options: -i INTERFACE --interface=INTERFACE The network interface to bind to. -s IPADDRESS --srcip=IPADDRESS The persistent source address, (real IP), associated with this interface. -v VHID --vhid=VHID The id of the virtual server [1-255]. -p PASSWORD --pass=PASSWORD The shared password, (this gets encrypted and is not sent in the clear). -o PASSFILE --passfile=PASSFILE File to read the shared password from. The file specified should contain the password on the first line of the file. -P --preempt Turn on preemptive failover. This causes an instance of ucarp to assume master status right immediately. -n --neutral Do not run the downscript on startup when the initial state is backup. -a IPADDRESS --addr=IPADDRESS The IP address of the virtual server. -h --help Display a brief summary of the command line options. -b SECONDS --advbase=SECONDS Interval in seconds that advertisements will occur, (defaults to 1 second). -k SKEW --advskew=SKEW Advertisement skew [1-255], (defaults to 0). -u COMMAND --upscript=COMMAND Specifies the command to run after ucarp has successfully become master, the interface name gets passed as an argument. Typi- cally a script used to bring up the virtual address, log the result, add routes, clear arp cache entries, etc. -d COMMAND --downscript=COMMAND Specifies the command that is run after ucarp has transitioned to the backup state, the interface name is passed as an argument. This is typically a script used to bring down the virtual interface, log the action, remove routes, etc. -r RATIO --deadratio=RATIO Ratio used by the backup to determine how long to wait for an unresponsive master before considering it dead. -z --shutdown Use of this command causes the command specified by the -d argument to be invoked when ucarp shuts down. -B --daemonize Causes ucarp to detach from the terminal and run in the background as a daemon. -f --facility Set the syslog facility, defaults to daemon. -x PARAMETER --xparam=PARAMETER Specify an extra parameter to be supplied to the up/down scripts. -S --ignoreifstate Ignore unplugged network cables. This option is useful when ucarp nodes are connected with a crossover cable. Without this option the master will transition to backup when the other node is powered down, as it no longer has a link (NO-CARRIER). -M --nomcast Use broadcast instead of multicast advertisements. EXAMPLES
A host with a real IP of 10.1.1.10 configured to be the master in a preemptive configuration with a virtual IP of 10.1.1.252. ucarp -i eth0 -s 10.1.1.10 -v 10 -p secret -a 10.1.1.252 --upscript=/etc/vip-up.sh --downscript=/etc/vip-down.sh -P The backup might be configured something like this. ucarp -i eth0 -s 10.1.1.11 -v 10 -p secret -a 10.1.1.252 --upscript=/etc/vip-up.sh --downscript=/etc/vip-down.sh A machine with a real IP of 192.168.1.19 is the preferred master for a virtual IP of 10.1.12.7, broadcasts are sent every 5 seconds. ucarp -b 5 -s 192.168.1.19 -v 27 -p badpass -a 10.1.12.7 -u /etc/vip-up.sh -d /etc/vip-down.sh -z The hot standby with an IP of 192.168.1.20 uses the following command, (note the advskew of 50 putting it at a disadvantage and making the first machine preferred). ucarp -b 5 -k 50 -s 192.168.1.20 -v 27 -p badpass -a 10.1.12.7 -u /etc/vip-up.sh -d /etc/vip-down.sh -z SIGNALS
Sending the ucarp process a SIGUSR1 will have it log a status line to syslog, eg "Sep 13 12:59:56 localhost ucarp[2654]: [INFO] MASTER on eth0 id 1" or "Sep 13 13:00:25 localhost ucarp[2644]: [INFO] BACKUP on eth0 id 1" Sending the ucarp process a SIGUSR2 will cause it to demote itself from master to backup, pause 3 seconds, then proceed as usual to listen for other masters, and promote itself if necessary. This could be useful if you wish another node to take over master. AUTHOR
ucarp was written by Frank Denis, <j@ucarp.org>. This manual page was written by Eric Evans <eevans@debian.org> for the Debian system (but may be used by others). Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License, Version 2 or any later version published by the Free Software Foundation. On Debian systems, the complete text of the GNU General Public License can be found in /usr/share/common-licenses/GPL. UCARP(8)
All times are GMT -4. The time now is 08:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy