[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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)
  • 5
  • 4
1 Solution
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:
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.
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
} until ($iter -eq $arySAList.length)  

Open in new window

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

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=$
Bryan ButlerCommented:
You will need to replace <server#> with your server names in quotes.
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
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.  
nestorenkoAuthor Commented:
The script doesn't works like I need but the expert tried to provide me the solution
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.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now