Script to set http_proxy without password stored in history

When working in an environment that has a proxy it is often very useful and sometimes completely necessary to set your http_proxy system variable so that your Linux machine can access the internet.

But there is a problem with just setting it manually. Take the following example.

You and your team are setting up a Linux machine for dev work and you all know the root credentials and are logging in as root just to set everything up. You want to install a package but can’t get though the proxy. Now here is the problem, you have to set http_proxy to a certain string that will contain your username and password  for you entire network account (email, PCs etc.). If you set the system variable straight from the command line your password will be saved for the rest of your team (or whoever has root access to that Linux machine) to view.

To get around this problem I created a very simple bash script that will set your http_proxy setting for your session and ensure there are no details left behind in the command line history file.

You will need to amend the script where I have highlighted in red.

Ensure you run this with source

(source executes the content of the file in the current shell thread)

source ./proxy_script.sh

Now to the script

#!/bin/bash

echo "Set http_proxy? y/n"
read yesOrNo

if [ $yesOrNo != "y" ]; then
return
fi

# Read username
echo -n Username:
read username

# Read password
echo -n Password:
read -s password

echo

histPath=$HISTFILE
unset HISTFILE

http_proxy="http://$username:$password@yourproxydomain:8080"
export http_proxy

export HISTFILE=$histPath

echo "Testing connection..."

curlOutput=$(curl -I http://google.com)

if [[ $curlOutput == *"407"* ]]
then
echo "No internet conenction, bad username / password";
return
fi

if [[ $curlOutput == *"200"* ]]
then
echo "internet connection OK";
return
fi

if [[ $curlOutput == *"302"* ]]
then
echo "internet connection OK";
return
fi

Leave a Reply

Your e-mail address will not be published. Required fields are marked *