Mar 072014
 

I love irc.  I love tor.  I love freenode via tor.  But one thing I hate is that sometimes I can’t connect and I would have to open up my torrc file and change the MapAddress cname.  So, I created a script today which randomly cycles through the names and changes it for me…..it uses a bash array to accomplish this.

Here it is:

#!/bin/bash
 
# By Ed Wiget
# this changes the MapAddress used by tor for freenode network from a list of cnames in an array
# 20140307 - original script
 
# location of the conf file
CONF=~/tor-browser_en-US/Data/Tor/torrc
 
# get the current used address
ADDU=`cat ${CONF} | grep MapAddress | awk '{print$3}' | awk -F. '{print$1}'`
 
# a random seed for our array
RANDOM=$$$(date +%s)
 
# our array or cnames
PADD=(p4fsi4ockecnea7l lgttsalmpw3qo4no 5jebommkgbfl6agc lbkwyb2csfcgoxwa)
 
# create a blank array
BLAH=()
 
# Function to check if item already exists in array
function checkArray {
 for item in ${BLAH[@]}; do
 	[[ "$item" == "$1" ]] && return 0 # Exists in BLAH
 done
 return 1 # Not found
}
 
# Main loop
while [ "${#BLAH[@]}" -ne "${#PADD[@]}" ]; do
	rand=$[ $RANDOM % ${#PADD[@]} ] 
	checkArray "${PADD[$rand]}" || BLAH=(${BLAH[@]} "${PADD[$rand]}")
done
 
# now select a random value from above
SELECTED=${BLAH[$RANDOM % ${#BLAH[@]} ]}
 
# for testing, enable the next line
# each time the script runs it should change
#echo ${SELECTED}
 
# remove whitespace before it
NEWV2=`echo ${SELECTED} | sed -e 's/^[[:space:]]*//'`
 
# remove whitespace after it
NEWV3=`echo ${SELECTED} | sed -e's/[[:space:]]*$//'`
 
echo "we are replacing ${ADDU} with ${NEWV3} in ${CONF}"
 
# here we say replace the ADDU word with NEWV in the conf file on line 19
sed -i "19s/${ADDU}/${NEWV3}/" ${CONF}