Link to home
Start Free TrialLog in
Avatar of nestorenko
nestorenko

asked on

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)
Avatar of Bryan Butler
Bryan Butler
Flag of United States of America image

/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$
Avatar of nestorenko
nestorenko

ASKER

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.
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

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 }
You will need to replace <server#> with your server names in quotes.
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
ASKER CERTIFIED SOLUTION
Avatar of Bryan Butler
Bryan Butler
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The script doesn't works like I need but the expert tried to provide me the solution
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.