Skip to content

Instantly share code, notes, and snippets.

@bpholt
Created April 27, 2012 16:40
Show Gist options
  • Select an option

  • Save bpholt/2510663 to your computer and use it in GitHub Desktop.

Select an option

Save bpholt/2510663 to your computer and use it in GitHub Desktop.
Script to set an HTTP proxy if it is accessible on the network
#!/usr/bin/env bash
PROXY_AUTH=user:password
PROXY_HOST=proxy.example.com
PROXY_PORT=:80
PROXY_EXCLUDE=".example.com,localhost,127.0.0.1/32,10.0.0.0/8"
# First, remove any existing proxy definitions in /etc/yum.conf
if [ -w /etc/yum.conf ]; then
sed -i "/^proxy=.*/d" /etc/yum.conf
fi
if [ -n "${PROXY_HOST}" ]; then
# Check to see if we can ping the defined proxy host
ping -q -c 1 $PROXY_HOST > /dev/null 2>&1
if [ $? -eq 0 ]; then
# We can, so set the environment variable and Yum configuration
if [ -n "${PROXY_AUTH}" ]; then
http_proxy="http://$PROXY_AUTH@$PROXY_HOST$PROXY_PORT"
else
http_proxy="http://$PROXY_HOST$PROXY_PORT"
fi
https_proxy=$http_proxy
no_proxy=$PROXY_EXCLUDE
export http_proxy https_proxy no_proxy
if [ -w /etc/yum.conf ]; then
echo proxy=$http_proxy >> /etc/yum.conf
fi
echo Set proxy to $http_proxy
else
# We cannot, so unset the environment variable
unset http_proxy
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment