Updating the WPAD file should work just fine for GUI browsers like Firefox (assuming they're configured to use it).
Command-line tools like lynx or wget or curl are much harder:
- They can't use a PAC or WPAD file
- You can't even change the environment variables in a process's parent process, let alone globally.
- And there's no way to tell a shell to re-source a file when it changes.
Except that there is a way to do the last item. In bash, at least. You can use the PROMPT_COMMAND variable to tell bash to source a script every time it displays the shell prompt. For example:
Modify your script so that it outputs the *_proxy variable settings to a file, e.g. /var/tmp/proxy-settings.sh. Make sure that file is world-readable, with chmod a+r.
something like:
cat <<__EOF__ > /var/tmp/proxy-settings.sh
export http_proxy="http://$router:8080"
export https_proxy="https://$router:8080"
export ftp_proxy="http://$router:8080"
EOF
Then write a shell script to source that file if it has changed, save it as, e.g., /usr/local/bin/update-proxy-vars.sh. Again, this should be word readable (chmod a+r). It doesn't need to be executable, because it's going to be sourced, not executed:
# each individual shell needs it's own timestamp, because each one will
# read the proxy settings file at different times.
mkdir -p /tmp/.proxy-timestamps
tsfile="/tmp/.proxy-timestamps/time/var/tmp/proxy-settings.sstamp.$$"
if [ -e "$tsfile" ] ; then
timestamp=$(< "$tsfile")
fi
# get the timestamp of the proxy-settings.sh file
# not all versions of `stat` can do this, and those that do
# have different and incompatible options. The following works
# for GNU `stat`.
proxy_timestamp=$(stat -c %Y /var/tmp/proxy-settings.sh)
# has the timestamp changed? if so, source the file.
if [ "$timestamp" != "$proxy_timestamp" ] ; then
. /var/tmp/proxy-settings.sh
echo "$proxy_timestamp" > "$tsfile"
fi
Finally, set PROMPT_COMMAND to source this script every time it displays a prompt.
PROMPT_COMMAND='. /usr/local/bin/update-proxy-vars.sh'
You should also run rm -f /tmp/.proxy-timestamps/timestamp.$$ in your ~/.bash_logout script, to tidy up when bash exits.just
This is, IMO, ugly and a nasty hack, and it slows down bash (which is no speed demon to begin with) because it has to run the update-proxy-vars.sh every time it displays a prompt. Every single time you run a command. Or just hit enter with no command. Every prompt, it's going to have to run this script, read in a timestamp file, run stat, compare timestamps, and maybe source another file if the timestamp has changed.
It would probably be faster to just source /var/tmp/proxy-settings.sh every time, if it exists, whether it has changed or not, and not bother with the timestamps.
Just set:
PROMPT_COMMAND='[ -e /var/tmp/proxy-settings ] && . /var/tmp/proxy-settings.sh'
But it works, which is the main thing, right?
Or you could get into the habit of remembering to manually source a file like /var/tmp/proxy-settings.sh in every shell that needs it when it's needed.
You can probably do the same thing using the $PS1 variable in other shells (like ash, dash, zsh, ksh, etc). But this version using PROMPT_COMMAND is ugly and hacky enough. Doing it with PS1 would be even worse.
source. Usually, the proxy variables are set globally in/etc/environment, but there is no magic - your shell sources/etc/environmentwhen it starts up. Perhaps there are programs that read from/etc/environmentdirectly, but that would depend on the program. Most programs just inspect the variables, I would guess./etc/environment: superuser.com/questions/664169/…. In any case, which programs do you want to configure withhttp_proxy? Perhaps there are other ways to configure them.lynx,curl,wget, etc is much harder. They can't use a PAC or WPAD file. You can't even change the environment variables in a process's parent process, let alone globally. And there's no way to tell a shell to re-source a file when it changes. The only thing you can do is to update/etc/environment(or some other file) and then remember to manually source that file in every shell that needs it.$PROMPT_COMMANDto source a file if it has changed...but that will slow down every shell prompt while it does that. On every command you run. And it won't effect already-running tools, it can only change the *_proxy variables when the prompt is displayed. And it would only work in bash, not ash/dash/zsh/ksh/etc (you could probable use $PS1 in those, but that's an even uglier hack). NOTE:sourceaka.does work in PROMPT_COMMAND...I wasn't sure, so I tested it withPROMPT_COMMAND=". /tmp/pc-test.sh". So, ugly but doable.