Operating System - HP-UX
1829489 Members
1829 Online
109991 Solutions
New Discussion

Searching machines in a subnet

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

Searching machines in a subnet

Hi,

I would like to search the list of machines within the following subnets in a NIS domain:
180.30.201.xx
180.30.202.xx
180.30.203.xx
180.30.204.xx
(and the list continues until 180.30.211.xx)

I could do the following continuosly:
grep "180.30.201" /var/yp/src/hosts

But the method above would list all machines within the subnet 180.30.201.xx, 180.30.202, etc...

I would like to have only the first instance of a machine within those subnets obtained from the /var/yp/src/hosts file. For each instance of machines obtained, I would like it to be piped into a file.

Could someone show me how do I write a script to do that?

Thanks.
4 REPLIES 4
Steve Steel
Honored Contributor
Solution

Re: Searching machines in a subnet

Hi


Add |head -n 1 >> /tmp/log to the grep to get 1 line

let x=201
while [ "$x" -le "211" ]
do
grep 18.130.$x /var/yp/src/hosts|head -n 1
let x=$x+1
done

Will work as a script

script > /tmp/lis


steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Judy Traynor
Valued Contributor

Re: Searching machines in a subnet

You can ping the subnet
i.e., ping 180.30.201.255 - that would get you all the hosts on the 201 subnet that were up.
Then Head that - if you just want the first one, and >> it to a file, (or use spool)

Then nslookup on the ip address to arrive at the host name. Or ypcat -k ipaddress hosts - if they are all in the same nis domain.

Sail With the Wind
Jordan Bean
Honored Contributor

Re: Searching machines in a subnet


Something like this should work.

grep -E '180\.30\.2(0[0-9]|1[01])' | sort -unt. -k 1i,3

Jordan Bean
Honored Contributor

Re: Searching machines in a subnet


Oops. I forgot to input the file.

grep -E '180\.30\.2(0[0-9]|1[01])' < /var/yp/src/hosts | sort -unt. -k 1i,3