I have two simple script that enables/disables Cisco AnyConnect when I don't want it trying to connect on each login/network transition. All was fine and dandy, but I wanted to add a line to the hosts file as well. The reason I'm using "echo $password | sudo -S" for most of the commands is because this script is being run from the script menu in Mac OS X. The terminal window does not open to address sudo password prompts.
#!/bin/bash
#Start_AnyConnect.command
password=`/usr/bin/osascript <<EOT
with timeout of (30 * 60) seconds
tell application "Finder"
activate
set myReply to text returned of (display dialog "Enter your password to authorize AnyConnect startup script" default answer "" with hidden answer)
end tell
end timeout
EOT`
echo $password | sudo -S echo -e "127.0.0.1\twpad.company.com" >> /etc/hosts
sleep 2
echo $password | sudo -S mv "/Library/LaunchAgents_Disabled/com.cisco.anyconnect.gui.plist" "/Library/LaunchAgents/com.cisco.anyconnect.gui.plist"
echo $password | sudo -S mv "/Library/LaunchDaemons_Disabled/com.cisco.anyconnect.vpnagentd.plist" "/Library/LaunchDaemons/com.cisco.anyconnect.vpnagentd.plist"
echo $password | sudo -S launchctl load /Library/LaunchDaemons/com.cisco.anyconnect.vpnagentd.plist
sleep 5
open /Applications/Cisco/Cisco\ AnyConnect\ Secure\ Mobility\ Client.app
exit 0
The problem I'm having is that
echo $password | sudo -S echo -e "127.0.0.1\twpad.company.com" >> /etc/hosts
appends "-e 127.0.0.1\twpad.company.com
" in stead of "127.0.0.1 wpad.company.com
" to the hosts file.
If I run the following command by itself it works as expected:
sudo echo -e "127.0.0.1\twpad.company.com" >> /etc/hosts
Is there another way to do this?
Thank you!