Appending entry into the HOSTS file

(imported topic written by jcenteno91)

All,

I am trying to append an entry into the HOSTS file, it seems like it should be simple, but everything I have tried fails. I used the suggestion that Ben posted for “Appending Text To a File in a Fixlet/Task/Action” and wrote a couple of actions. After a few failures, I have edited the action to it’s simplest form:

run cmd.exe /C echo {“X.X.X.X Our-Server-01.any.domain.dcd”} >> {“c:\windows\system32\drivers\etc\hosts”}

(I did not set any relevance at all)

It fails also. Any help would be greatly appreciated.

(imported comment written by SystemAdmin)

Try this…

waithidden cmd.exe /C echo {“X.X.X.X Our-Server-01.any.domain.dcd%0d%0a”} >> {(pathname of system folder as string & “\drivers\etc\hosts”)}

Also if you want to write some relevance to check to see if it’s even in there to begin with, you can check out this other thread I had posted to…

http://forum.bigfix.com/viewtopic.php?id=748

-Paul

(imported comment written by jcenteno91)

Thanks, but it still reports that the action failed. Is there a log that I can look at that will tell me why it is failing?

(imported comment written by SystemAdmin)

Logs are kept in c:\Program Files\BigFix Enterprise\BES Client__BESData__Global\Logs

Whoops, I probably should have omitted the CRLF since the dos >> will add that…

waithidden cmd.exe /C echo {“X.X.X.X Our-Server-01.any.domain.dcd”} >> {(pathname of system folder as string & “\drivers\etc\hosts”)}

Ok, that should work fine now. I tried that myself. :slight_smile:

-Paul

(imported comment written by jcenteno91)

I tried the command line that was reposted and it failed. I am going to look at the logs to see if I can determine why.

(imported comment written by SystemAdmin)

Btw, what OS are you trying this on?

(imported comment written by jcenteno91)

This is for Windows XP. I checked the logs and it shows succeeded, and I verified that the HOSTS file had changed. Go figure!

(imported comment written by jeremylam)

Did you make this a task and not a Fixlet?

If you made this Fixlet message, the console will report “Failed” if the applicability relevance does not switch from true to false. Tasks return “Completed” if all lines of the action complete successfully.

(imported comment written by SystemAdmin)

Here’s a little relevance for PCs that don’t have “X.X.X.X Our-Server-01.any.domain.dcd”

exists file "hosts"
whose
(
not exists lines
whose
(
it != ""
and
it as left trimmed string starts with "X.X.X.X"
and
it as right trimmed string as lowercase ends with “our-server-01.any.domain.dcd”
)
of it
)
of folder “drivers\etc” of system folder

Obviously X.X.X.X is an IP address, which is why I don’t lowercase it. :slight_smile:

I’m also checking via “starts with” rather than “contains” because it could have the IP address commented out with a “#”.

Checking it != “” because there’s a caution using the trimmed string operator. See: http://forum.bigfix.com/viewtopic.php?id=4317

Hmm… Did the trimmed string operator get fixed in BES 8.0?

-Paul

3 posts were split to a new topic: How do I add a carriage return before a new hosts file entry (if an entry already exists)?