Posts tagged bash
Advanced Bash Scripting – Part 2
3
The problem statement is as follows :
The Playfair Cipher encrypts text by substitution of digrams (2-letter groupings). It is traditional to use a 5 x 5 letter scrambled-alphabet key square for the encryption and decryption.
Each letter of the alphabet appears once, except “I” also represents “J”. The arbitrarily chosen key word, “CODES” comes first, then all the rest of the alphabet, in order from left to right, skipping letters already used.
To encrypt, separate the plaintext message into digrams (2-letter groups). If a group has two identical letters, delete the second, and form a new group. If there is a single letter left over at the end, insert a “null” character, typically an “X.”
To read more about the question, check http://tldp.org/LDP/abs/html/writingscripts.html
The solution for this question is pretty long (over 100 lines in shell script). You can check the final solution at http://sp2hari.com/bash/playfair-cipher.html
Explanation of function/code used in the solution.
locateInKeySquare: Searches for the character passed as the parameter in the keySquare and returns the position of the character.
addToKeySquare: Adds a character c passed as the parameter to the keySquare. This checks if the character is already present, if it is J and changes it to uppercase (if needed)
printKeySquare: Prints the keySquare from 1D to 2D format.
The rest of the code adds the keyWord to keySquare. The the remaining words are added to the keySquare. After that, we extract the dialects from the plaintext and based on the 3 rules, we encrypt the text.
Hope this is helpful..
Advanced Bash Scripting – Part 1
6Hello everyone, I’m planning to solve the problems in the Advanced Bash-Scripting Guide, say like one problem per week. Why am I doing this?
1. The problems there are very interesting.
2. Working in windows all the time, I feel I’m losing my shell scripting abilities. This will keep me updated.
The problem we are going to solve this week is “Testing Passwords”. The problem statement is as follows.
Write a script to check and validate passwords. The object is to flag “weak” or easily guessed password candidates.
A trial password will be input to the script as a command-line parameter. To be considered acceptable, a password must meet the following minimum qualifications:
1. Minimum length of 8 characters
2. Must contain at least one numeric character
3. Must contain at least one of the following non-alphabetic characters: @, #, $, %, &, *, +, -, =
Optional:
Do a dictionary check on every sequence of at least four consecutive alphabetic characters in the password under test. This will eliminate passwords containing embedded “words” found in a standard dictionary.
Enable the script to check all the passwords on your system. These probably do not reside in /etc/passwd.
The command I’m planning to use for this is grep, with a few for loop constructs and string operations.
Let’s check the constraints one by one.
#Minimum length of 8 characters
if [ ${#password} -lt 8 ]
then
echo "$password: $weakString"
return
fi
#Must contain at least one numeric character
if [ `echo $password | grep -c -E "[0-9]+"` -eq 0 ]
then
echo "$password: $weakString"
return
fi
#Must contain at least one of the following non-alphabetic characters: @, #, $, %, &, *, +, -, =
if [ `echo $password | grep -c -E "[@#$%&*=+-]+"` -eq 0 ]
then
echo "$password: $weakString"
return
fi
#Do a dictionary check on every sequence of at least four consecutive alphabetic characters in the password under test. This will eliminate passwords containing embedded "words" found in a standard dictionary.
for((i=4;i<=${#password};i++))
do
for((j=0;j<=${#password}-$i;j++))
do
if [ `grep -c -E "^${password:$j:$i}$" dict.txt` -gt 0 ]
then
echo "$password: $weakString"
return
fi
done
done
That's about it, we have checked all the constraints. Now, we will combine all the checks in one function and then we will pass all the command line parameters to this function one by one.
You can take a look at the final script here.
Can you make it any better or reduce the code size? Feel free to add it in the comments section.
Install multiple firefox extensions
3I’m a full time web developer now. Million thanks to Firefox, web development is made much much easier. To make the web development process easier, there are many many extensions.
A few of them I use for my development,
1. Firebug
2. Web Developer
3. ColorZilla
4. MeasureIt
5. ViewSourceChart
6. FireCookie
7. YSlow
8. Delicious.
9. Twitterfox
10. CSS Viewer
One problem I face when I have to work in a new machine or a new user account is installing these extensions. Not that I face this problem everyday, but this is something which is very irritating. One simple solution will be to backup the extensions folder and protect it safely, so that I can use it on any machine I use. Finally, I wrote a simple shell script which does this job.
#!/bin/bash
declare -a EXT LINK#Let us populate the extension and the link for the extension here
EXT[0]=”ColorZilla”
EXT[1]=”Firebug”
EXT[2]=”WebDeveloper”
EXT[3]=”MeasureIt”
EXT[4]=”ViewSourceChart”
EXT[5]=”CSSViewer”
EXT[6]=”FireCookie”
EXT[7]=”YSlow”
EXT[8]=”SenSEO”
EXT[9]=”Delicious”
EXT[10]=”Twitterfox”LINK[0]=”https://addons.mozilla.org/en-US/firefox/downloads/latest/271/addon-271-latest.xpi”
LINK[1]=”https://addons.mozilla.org/en-US/firefox/downloads/latest/1843/addon-1843-latest.xpi”
LINK[2]=”https://addons.mozilla.org/en-US/firefox/downloads/latest/60/addon-60-latest.xpi”
LINK[3]=”https://addons.mozilla.org/en-US/firefox/downloads/latest/539/addon-539-latest.xpi”
LINK[4]=”https://addons.mozilla.org/en-US/firefox/downloads/latest/655/addon-655-latest.xpi”
LINK[5]=”https://addons.mozilla.org/en-US/firefox/downloads/latest/2104/addon-2104-latest.xpi”
LINK[6]=”https://addons.mozilla.org/en-US/firefox/downloads/latest/6683/addon-6683-latest.xpi”
LINK[7]=”https://addons.mozilla.org/en-US/firefox/downloads/latest/5369/addon-5369-latest.xpi”
LINK[8]=”https://addons.mozilla.org/en-US/firefox/downloads/file/57302/senseo-0.8.9-fx.xpi”
LINK[9]=”https://addons.mozilla.org/en-US/firefox/downloads/latest/3615/addon-3615-latest.xpi”
LINK[10]=”https://addons.mozilla.org/en-US/firefox/downloads/latest/5081/addon-5081-latest.xpi”if [ ! -d "/tmp/firefoxext-$USER/" ]
then
echo “Creating directory in tmp”
mkdir -p “/tmp/firefoxext-$USER/”
fii=0
count=${#EXT[@]}
while [ "$i" -lt "$count" ]
do
echo “Installing ${EXT[i]}”
filename=`echo ${LINK[i]} | awk -F “/” ‘{print $NF}’`;
rm -rf “/tmp/firefoxext-$USER/tmp”
mkdir -p “/tmp/firefoxext-$USER/tmp”
if [ ! -f "/tmp/firefoxext-$USER/$filename" ]
then
echo “Downloading $filename”
wget –quiet -O “/tmp/firefoxext-$USER/$filename” ${LINK[i]}
fi
unzip -qq -d “/tmp/firefoxext-$USER/tmp” “/tmp/firefoxext-$USER/$filename”
fffolder=`cat “/tmp/firefoxext-$USER/tmp/install.rdf” | grep “em:id” | head -n 1 | awk -F “>” ‘{print $2}’ | awk -F “<" '{print $1}'`if [ -z $fffolder ]
then
i=`echo $i+1 | bc`;
continue;
fi
for j in `ls -1 "$HOME/.mozilla/firefox"`
do
if [ -d "$HOME/.mozilla/firefox/$j" ]
then
rm -rf "$HOME/.mozilla/firefox/$j/extensions/$fffolder"
mkdir -p "$HOME/.mozilla/firefox/$j/extensions/$fffolder"
cp -r "/tmp/firefoxext-$USER/tmp/"* "$HOME/.mozilla/firefox/$j/extensions/$fffolder"
fi
done
i=`echo $i+1 | bc`;
done
You can download the latest version of this script from here.
Logic : Download the xpi file. Unzip it. Copy it to extensions folder.
Disclaimer: Download and run the above script at your own risk. I have tested it with many cases. Worst case, this will remove all your extensions from all your profiles (OMG) (Happened to me once). Also, I would advice you not to run this script in windows. I haven’t tested it there.












