Operating System - Linux
1757061 Members
2205 Online
108858 Solutions
New Discussion юеВ

Re: shell script to find ips

 
SOLVED
Go to solution
Shivkumar
Super Advisor

shell script to find ips

Dear Sirs,

I have server names list and want to find out ip address of the servers.

can someone suggest a shell script ?

Thanks,
Shiv
7 REPLIES 7
Rodney Hills
Honored Contributor
Solution

Re: shell script to find ips

Pipe the list into "xargs" which will run the nslookup command.

cat serverlistfile | xargs -n1 nslookup

HTH

Rod Hills
There be dragons...
Alan Meyer_4
Respected Contributor

Re: shell script to find ips

I have the following script(get.ip.sh) located on each server;

HOST=`hostname |cut -f1 -d"."`
DOM=`domainname`
[ -z "$DOM" ] && DOM="none"
/etc/lanscan |grep lan |awk '{print $5}'|while read LAN;do
IP=`/etc/ifconfig $LAN 2>&1 |grep inet |awk '{print $2}'`
MASK=`/etc/ifconfig $LAN 2>&1 |grep inet |awk '{print $4}'`
typeset -L4 M12
typeset -R4 M34
M12=$MASK
M34=$MASK
typeset -L2 M1
typeset -R2 M2
typeset -L2 M3
typeset -R2 M4
M1=`echo $M12|tr '[a-z]' '[A-Z]'`; m1=`echo ibase=16\;$M1|bc`
M2=`echo $M12|tr '[a-z]' '[A-Z]'`; m2=`echo ibase=16\;$M2|bc`
M3=`echo $M34|tr '[a-z]' '[A-Z]'`; m3=`echo ibase=16\;$M3|bc`
M4=`echo $M34|tr '[a-z]' '[A-Z]'`; m4=`echo ibase=16\;$M4|bc`
MASK="$m1.$m2.$m3.$m4"
[ "$IP" != "" ] && echo "$HOST $DOM $LAN $IP $MASK"
done



then, from a central server, I remsh to each server in the list and execute the script;

for server in node1 node 2 node3 ;do
remsh $server -n /usr/local/bin/get.ip.sh
done

This will generate results like the following format nodename nisdomain lan# IP netmask;

node1 nisdom lan2 170.208.6.203 255.255.255.192
node1 nisdom lan0 170.208.5.6 255.255.255.240
node2 nisdom lan0 170.208.5.4 255.255.255.240
node2 nisdom lan1 170.208.6.209 255.255.255.192
node3 nisdom lan0 10.12.2.150 255.255.255.128
" I may not be certified, but I am certifiable... "
Alan Meyer_4
Respected Contributor

Re: shell script to find ips

incidently, if someone has a better way to convert the netmask from ffffffc0 to 255.255.255.192 in a shell script, I'd love to hear about it.


zpp
" I may not be certified, but I am certifiable... "
James R. Ferguson
Acclaimed Contributor

Re: shell script to find ips

Hi:

Alan, you can use perl (embedded in your shell) to convert your address:

a=ffffffc0 #...in your shell...

i=`perl -le 'print join(".",unpack("C4",pack("H8",$ARGV[0])))' $a`

echo ${i} #...returns the dotted notation

Regards!

...JRF...

Alan Meyer_4
Respected Contributor

Re: shell script to find ips

Thanks James....


Zero Points Please
" I may not be certified, but I am certifiable... "
Howard Marshall
Regular Advisor

Re: shell script to find ips

Hay Alan,

I don't know if this is really better but it takes fewer lines and fewer calls to outside commands. Some people would consider that a plus I guess

#!/usr/bin/ksh


LAN=lan0

/etc/ifconfig ${LAN} |\
awk '/inet/ { printf "%s %s %s %s",\
substr($4,1,2),substr($4,3,2),substr($4,5,2),substr($4,7,2) }'|\
read a b c d

((A=16#$a));((B=16#$b));((C=16#$c));((D=16#$d))

echo $A.$B.$C.$D
Alan Meyer_4
Respected Contributor

Re: shell script to find ips

Thanks Howard.

ZPP
" I may not be certified, but I am certifiable... "