For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2004 > Convert ip-addresses in file with hostname









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Convert ip-addresses in file with hostname
Bas

2004-11-21, 8:55 am

Hi all!

I am working on a project in which there's something to be done with
which I need a little help: a file containing lines with ip-addresses
which should be replaced by the hostname of the servers instead of the
ip-addresses.

I tried several ways, without succes. Most close I came with a "for i"
statement
and a AWK {print $5} construction. However, I need some directives to
complete the job of the conversion from the ip-address to the
hostname. what do I use?
nslookup, dig, hostname?

I assumed something like this:

hosts=/tmp/temprep0

for ip in $hosts ; do
if [ ".$host" = 10.65. ] ; then
for i in `netstat -in | awk
'{if(substr($1,1,1)!="N")print$1}' | sed 's/\*//g'` ; do
ifconfig $i
done
continue
fi
sed 's/$/ /' < /etc/hosts > $tmp2 || die "sed failed"
grep -i "[ ]$host[ ]" $tmp2 > $tmp
if [ $? = 0 ] ; then
head -1 $tmp | awk '{printf("%-30s %s\n",$2,$1)}'
else
if [ -r /etc/resolv.conf ] ; then
nslookup $host 2>&1 | sed 's/[ ]*,[ ]*/ /' >
$tmp
fgrep '***' $tmp > /dev/null
if [ $? = 0 ] ; then
echo "$prog: host '$host' not found" 1>&2
continue
fi

Anyone?

Thanx in advance
Kevin Collins

2004-11-24, 8:55 pm

In article <cnklp8$28u$1@aken.eed.ericsson.se>, Michael Tosch wrote:
> In article <717da987.0411190122.48d918e7@posting.google.com>, bvweelde@chello.nl (Bas) writes:
>
> Sorry, I dont understand what your script does and what it should do
> instead. An example would be helpful.
>
> Is it just about IP-to-host translation?
> Then this works best:
>
> getent hosts $ip | awk '{print $2}'


That assumes you have 'getent', which on my HP-UX boxes I do not...

Kevin

Kevin Collins

2004-11-29, 3:58 pm

In article <cnklp8$28u$1@aken.eed.ericsson.se>, Michael Tosch wrote:
> In article <717da987.0411190122.48d918e7@posting.google.com>, bvweelde@chello.nl (Bas) writes:
>
> Sorry, I dont understand what your script does and what it should do
> instead. An example would be helpful.
>
> Is it just about IP-to-host translation?
> Then this works best:
>
> getent hosts $ip | awk '{print $2}'


That assumes you have 'getent', which on my HP-UX boxes I do not...

Kevin

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com