Script to find out Host entries on Windows

Hi experts,
I Need a script that will check the host file on a list of servers, the list could be 500 servers in size.

So I need to find out whether the entries of server1 and/or server2 are defined in /etc/hosts.

output should include:
servername:   Host entry found for (either of these entries)
servername:   not found
servername:   could not connect (need to check to see which servers we will have to check manually)
nestorenkoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bryan ButlerCommented:
/etc/hosts implies unix, are you talking about windows?  

loop through list of servers and search /etc/hosts file
- for each one output <found/not/no connect message> (depending on server1 and/or server2 are defined, or cannot connect)

Do you access where you could loop through the drives with:
\\<server>\c$
0
nestorenkoAuthor Commented:
Hi I'm talking about Windows, the full path is C:\WINDOWS\system32\drivers\etc\hosts, so basically I have one list with about 500 servers, the scrript should be able to to looking on the host file on every server if there is any server1 or server2 entries and put it at the results.
0
Bryan ButlerCommented:
if you have access, and you can skip the "have access" message", and you can use powershell, and....well you get the picture.  I'm thinking something like this.  I can work on the "have access" if this is on track to begin with.


$servlist = Get-Content "serverlist.txt";   
$iter = 0; $found = $false
do {
    $file = "\\" + $servlist[$iter] + "\WINDOWS\system32\drivers\etc\hosts"
    if (test-path $file) {
        if (gci $file | findstr <server1>) { $servlist[$iter] + ":   Host entry found for  <server1>"; $found=$TRUE }
        if (gci $file | findstr <server2>) { $servlist[$iter] + ":   Host entry found for  <server2>"; $found=$TRUE  }
        if (!($found -eq $TRUE )) { $servlist[$iter] + " not found" }
    } else {
        $servlist[$iter] + ":   could not connect or file does not exist"
    }
    $found = $false
    $iter++
} until ($iter -eq $arySAList.length)  

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

nestorenkoAuthor Commented:
Hi thanks a lot for your helping, so when I tried to run this powershell script I received the error bellow.

PS C:\Scripts> ./test.ps1
The redirection operator '<' is not supported yet.
At C:\Scripts\test.ps1:7 char:34
+         if (gci $file | findstr <s <<<< erver1>) { $servlist[$iter] + ":   Host entry found for  <server1>"; $found=$
TRUE }
0
Bryan ButlerCommented:
You will need to replace <server#> with your server names in quotes.
0
nestorenkoAuthor Commented:
Hi thanks again, so I executed the following test.

1. Added server1 and server2 entries  into C:\WINDOWS\system32\drivers\etc\hosts file

2. Added my local computer name into serverlist.txt

3. Executed the script but it says "could not connect or file does not exist"

I think on this case the message should be "Host entry found for (either of these entries)", because there is the related entries into host files.

Other think my friend there is any way to redirect the outputs to a text file ?

Thanks and regards
0
Bryan ButlerCommented:
You can redirect it by adding ">> <filename>" to the end of the command line.  Let's say you saved the script to "checkhosts.ps1".  Then the cmd line:

> checkhost.ps1 >> saveoutputtothisfile.txt

But, it looks like you don't have \\<server>\c$ access so you will have to get permissions to get this script to work.  Otherwise you will get "Host entry found for (either of these entries)" everytime.  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nestorenkoAuthor Commented:
The script doesn't works like I need but the expert tried to provide me the solution
0
Bryan ButlerCommented:
I'm still here if you need any more help.  Please contact me directly though as I can't monitor questions easily once points are assigned.  I think the main thing is having permissions to access the servers directly using admin (\\<server>\c$) type access.  Otherwise you will need permissions for every "etc" folder on every server you want to query.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Server Apps

From novice to tech pro — start learning today.