[This is not an answer, and I wanted to post this as comment, but my low “reputation” score doesn’t allow it:]
Keyboard Maestro has an excellent forum now. A quick search in the Keyboard Maestro forum showed me that you didn’t post your question there, so I think it’s worth mentioning it.
To add some content:
The macro won’t be a problem, but the sudo password:
It is tempting, but don’t just store your sudo password in a Keyboard Maestro variable or in a named clipboard.
Keyboard Maestro provides some special treatment for “password variables”. From the KM documentation:
Variables with names that start or end with “Password” or “PW” are considered passwords – their values will not be stored (except in memory) and they cannot be read directly by shell scripts or AppleScripts, though their consequences can easily be extracted, eg with the Set Clipboard to Text action. The Prompt For User Input dialog will display such variables in a password field.
So this may be a possibility to access the hosts file in an automated way: Get the sudo password with the “Prompt for User Input” action and store it for example in a variable PW
. But I can’t tell how secure this really is.
- Another possibility would be to manipulate the sudoers file with visudo, so that you can write to the hosts file without entering the sudo password. But I wouldn’t do that in case of the hosts file!