Something like this should work:
/etc/NetworkManager/dispatcher.d
#!/bin/bash
ip=10.0.1.13
nc -w 1 $ip 3142
proxy_file="/etc/apt/apt.conf.d/02local_proxy"
if [ $? -eq 0 ]; then
echo "Acquire::http { Proxy \"http://$ip\"; };" > $proxy_file
echo 'Acquire::https { Proxy "false"; };' >> $proxy_file
else
rm -f $proxy_file
fi
Fix permissions
sudo chmod +x /etc/NetworkManager/dispatcher.d/99SetAptProxy
Notes:
- The "nc" command tests that it can connect to the 3142 port on the given IP address.
- This script is run everytime the networking interfaces are changed by network manager.
- Feel free to alter the way that you detect for the proxy, this works for me, but it is a security vulnerability if you install packages on a foreign network, for example.