Universal append to hosts file

Does anyone have actionscript to append to host files on all platforms? Mac/Win/UNIX

I know that we have examples in old posts for Windows (some of which don’t apply to 64 bit OS’s anymore even) but need to add an entry to a wide array of endpoints

And yes I have searched bigfix.me and while there are good analysis samples for seeing if an entry is there https://bigfix.me/relevance/details/3017247, there’s nothing good for the subsequent actionscript

I was thinking of something clever like copying the existing hosts file to __appendfile and appending to it then copying it back

I’ve used that technique before, yes.

May also be easy to do something like

wait /bin/sh -c 'echo newentry >> /etc/hosts'

Or

action uses wow64 redirection {not x64 of operating system}
wait CMD.exe /c echo newentry >> {pathname of file "drivers\etc\hosts" of native system folder}

(Wrapped in a condition for each OS type)

Edit: also assuming there’s no incorrect “old” version of the entry

2 Likes

A possible appendfile approach?

Detection

exists it whose (not exists lines whose (it starts with "192.168.100.47") of it) of (if (windows of it) then (file "hosts" of folder "drivers\etc" of native system folder) else (if(unix of it) then (file "/etc/hosts") else (if(mac of it) then (file "/etc/hosts") else ("/etc/hosts")))) of operating system

Action

parameter "hostsfile"="{pathname of (if (windows of it) then (file "hosts" of folder "drivers\etc" of native system folder) else (if(unix of it) then (file "/etc/hosts") else (if(mac of it) then (file "/etc/hosts") else (false)))) of operating system}"

parameter "newhostsfile"="{((concatenation "%0a" of lines of file it) & "%0a192.168.100.47%09myhost.mydomain.com") of (parameter "hostsfile")}"

delete __appendfile
appendfile {parameter "newhostsfile"}

delete {parameter "hostsfile" & ".bak"}
copy {parameter "hostsfile"} {parameter "hostsfile" & ".bak"}
delete {parameter "hostsfile"}
copy __appendfile {parameter "hostsfile"}
2 Likes

I have used an interation to synthesize the new file from the old one, and a Line Filter to identify the line that I want to remove and replace with a second line.

You could have the line break separator be variable by OS (to handle %0d on some).

1 Like

I think this is close to what I was thinking but what about the same relevance but the action below is more what I was thinking. Moving the existing hostsfile in and using the native appendfile logic to handle the OS differences.

Thoughts on this?

parameter "hostsfile"="{pathname of (if (windows of it) then (file "hosts" of folder "drivers\etc" of native system folder) else (if(unix of it) then (file "/etc/hosts") else (if(mac of it) then (file "/etc/hosts") else (false)))) of operating system}"

delete __appendfile
copy "{parameter "hostsfile"}" __appendfile
appendfile {"192.168.100.47%09myhost.mydomain.com"}

delete {parameter "hostsfile" & ".bak"}
copy {parameter "hostsfile"} {parameter "hostsfile" & ".bak"}
delete {parameter "hostsfile"}
copy __appendfile {parameter "hostsfile"}
5 Likes

Nice! I didn’t realize you could could copy to __appendfile. Much slicker :grin:

I would trap the case where the line is already there and has the same, or different host or IP.
I think you have to iterate and concatenate to deal with that edge case.

Agreed, I think the relevance can take care of that if that line isn’t present

So it turns out I can’t be as fancy as copying something into __appendfile as the client will start at line 1 when it writes out so I have to use a variation of @SLB’s original

parameter "hostsfile"="{pathname of (if (windows of it) then (file "hosts" of folder "drivers\etc" of native system folder) else (if(unix of it) then (file "/etc/hosts") else (if(mac of it) then (file "/etc/hosts") else (false)))) of operating system}"
delete __appendfile
parameter "newline"="{if windows of operating system then "%0D%0A" else "%0A"}"
appendfile {((concatenation (parameter "newline") of lines of file it) & parameter "newline" & "%09127.0.0.1%09myhost.mydomain.com") of (parameter "hostsfile")}
delete {parameter "hostsfile" & ".bak"}
copy {parameter "hostsfile"} {parameter "hostsfile" & ".bak"}
delete {parameter "hostsfile"}
copy __appendfile {parameter "hostsfile"}
1 Like

Consider this revision to deal with the “replace” instead of "add"
It should detect lines in the old hosts where the hostname is defined and drop them from the new file before adding the new host

parameter "hostAdd"="myhost.mydomain.com"
parameter "IPforHostAdd"="127.0.0.1"
parameter "hostsfile"="{pathname of (if (windows of it) then (file "hosts" of folder "drivers\etc" of native system folder) else (if(unix of it) then (file "/etc/hosts") else (if(mac of it) then (file "/etc/hosts") else (false)))) of operating system}"
delete __appendfile
parameter "newline"="{if windows of operating system then "%0D%0A" else "%0A"}"
appendfile {((concatenation (parameter "newline") of lines whose (not (it ends with ("%09" & parameter "hostAdd") OR it ends with (" " & parameter "hostAdd"))) of file it) & parameter "newline" & "%09" & parameter "IPforHostAdd" & "%09" & parameter "hostAdd") of (parameter "hostsfile")}
delete {parameter "hostsfile" & ".bak"}
copy {parameter "hostsfile"} {parameter "hostsfile" & ".bak"}
delete {parameter "hostsfile"}
copy __appendfile {parameter "hostsfile"}
3 Likes

I will have a modified version myself. Also it appears if you have macOS you shouldn’t be using /etc/hosts rather you should use /private/etc/hosts so thats a change, and the “false” in that condition also doesn’t work - I’m testing with “nothing” there right now

2 Likes

So this is what worked on all platforms EXCEPT solaris 11!
Solaris 11 apparently you have to modify /etc/inet/hosts and /etc/hosts but…

parameter "hostsfile"="{pathname of file "hosts" of (if (windows of it) then (folder "drivers\etc" of native system folder) else (if(unix of it) then (folder "/etc") else (if(mac of it) then (folder "/private/etc") else (nothing)))) of operating system}"
delete __appendfile
parameter "newline"="{if windows of operating system then "%0D%0A" else "%0A"}"
appendfile {((concatenation (parameter "newline") of lines of file it) & parameter "newline" & "127.0.0.1%09myhost.mydomain.com") of (parameter "hostsfile")}
delete {parameter "hostsfile" & ".bak"}
copy {parameter "hostsfile"} {parameter "hostsfile" & ".bak"}
delete {parameter "hostsfile"}
copy __appendfile {parameter "hostsfile"}
3 Likes