Script help (117 Views)
Reply
Occasional Contributor
Diesel
Posts: 6
Registered: ‎08-17-2005
Message 1 of 4 (117 Views)
Accepted Solution

Script help

Hello,

I would like to know how to search through the host table to match computers on a particular subnet, then to give the total number matched at the end using a while loop or something (my scripting knowledge is very limited).

Any ideas would be greatly appreciated.
Outstanding Contributor
Pete Randall
Posts: 16,205
Registered: ‎11-03-1996
Message 2 of 4 (117 Views)

Re: Script help

How about a simple grep/wc?

grep xxx.xxx.xxx /etc/hosts |wc -l


Pete

Pete
Honored Contributor
Jeff_Traigle
Posts: 1,354
Registered: ‎03-04-2004
Message 3 of 4 (117 Views)

Re: Script help

This could get a bit more complex depending on how your subnets are configured. Generally, you can do something like this, however:

grep -c ^WWW.XXX.YYY. /etc/hosts

-c returns the count instead of the matches (saves the piping of grep to wc)

^ only matches if the address is at the beginning of the line (avoids grabbing commented lines)
--
Jeff Traigle
Occasional Contributor
Diesel
Posts: 6
Registered: ‎08-17-2005
Message 4 of 4 (117 Views)

Re: Script help

Thanks guys! That's what I needed.

And thanks Jeff for the detailed explanation. It helps the newbies like myself.

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.