PDA

View Full Version : BASH Programming - Introduction HOW-TO



abhaythehero
01-21-2011, 08:27 AM
http://goo.gl/sM4ir

b0nd
01-21-2011, 08:37 AM
Here (http://bashscript.blogspot.com/) is a good active blog on bash scripting. Covers scripts from basic to advanced ones.

jamess497
10-18-2011, 01:08 PM
Thanks a lot for this it has helped me a great deal keep it up...

Hackuin
10-26-2011, 07:57 PM
Bash/shell Programming is a "ART", one beautiful "Art of Linux world".
Once you get you hands over this scripting, you will be amazed with the flow of your daily work, especially, if you use *nix as a standard OS for regular/official use. Or, if you aren't a *nix admin, you have no-clue, the actual beauty of this particular scripting language.

Few suggestions:
You may get zillions of links over internet for learning bash/shell scripting. What you actually need fore-mostly is the frequency over various/alternative "COMMANDS" and differences, for example consider an redirector.



#!/bin/bash
#: Simple Script for performance example.

cat ULTRA_LONG_DATA_FILE | while read LINE
do
echo "$LINE" >> NEW_LONG_FILE
done


Above script just read each line of the file "ULTRA_LONG_DATA_FILE" and stores that line in a file "NEW_LONG_FILE". Look at the script below:



#!/bin/bash
#: Simple Script for performance example with redirector.

while read LINE
do
echo "$LINE" >> NEW_LONG_FILE
done < ULTRA_LONG_DATA_FILE


This script does the same thing, but, its quick (allmost 4x times faster then 1st script) for large files when compare to first script. Now again, look at the next script.



#!/bin/bash
#: Simple Script for performance example with file discriptor.
exec 4<&1
exec 1> NEW_LONG_FILE

while read LINE
do
echo "$LINE"
done < ULTRA_LONG_DATA_FILE
exec 1<&4
exec 1>&-

This actually is a killer, its quicker then both (allmost 10x times then 1st script).

The moral is simple, if you understand the concept of what "pipe", "redirector", and "file discriptor" is then you can apply it in your script appropriately/according to your specifics. Similarly, it is same with the commands, if you don't know the alternative commands available for same purpose you will opt to be limited. So, my first suggestion is to "STUDY/LEARN ALL THE COMMANDS AVAILABLE FOR *NIX". Simple example would be for certain situations you may need to list, "ESTABLISHED CONNECTION ON A MACHINE", you will go use command like, "netstat -an|grep ESTA", but, alternative you could just use four letter command for that and its, "ss -n" which produce's the same result.

For learning, basic to advance commands and alternative commands available on *nix, I have uploaded two books.

Introduction to the command line second edition - The fat free guide to Unix and Linux commands. --by Nicholas Marsh.
Linux in a Nutshell -- by Ellen Siever, Stephen Figgins, Robert Love & Arnold Robbins.

Get these books over [ Here (http://www.megaupload.com/?d=2IM8YXDH) ]

More over that, I would suggest to actually implement and start doing small scripts, as you are opting for bash/shell scripting, you would found/love for *nix world, start writing interesting scripts. For example, I almost have plenty of scripts on my machines, which I use them almost twice/thrice a week, below is one of them,


#!/bin/bash
#
# Author: hackuin () solidgeeks com.
# Date: Thu Jun 25 19:29:01 IST 2011.
# Description: Script for Auto-Info.
# Copyright (C) Under GNU Public License.
#

# Usage Info()
if [ ! $# == 1 ]
then
echo "Usage: $0 [ File-Name ]"
exit 1
fi
################################################## ###############

# For My Notes:
# Function zone_transfer()

################################################## ###############

# Host Discovery.
discover() {
while read LINE
do
IP_HOST=`nmap -sP $LINE |grep report |awk '{print $6}'|tr -d "()"`
nmap -sP $LINE |grep late >/dev/null
if [ "$?" == "0" ]
then
echo -e "Host: $LINE [ $IP_HOST ] seems to be UP"
echo -e "$LINE $IP_HOST is UP" >> UP_FILE
else
echo -e "Host: $LINE [ $IP_HOST ] seems to be DOWN"
echo -e "$LINE $IP_HOST is UP" >> DOWN_FILE
fi

done < SUSPECT_FILE
echo -e "\t UP_FILE contains Host with are UP."
echo -e "\t DOWN_FILE contains Host with are DOWN. \n\n"
echo -e "Host Discovery DONE " "\t\t\t\t\t\t\t$C_G[ OK ]$C_END"


}


whoisinfo() {
#: Actually, taken from my one of the script for offical use, which I wrote
#: Long time back, As I do only recuired few of the info() from the whois
#:+ records, for notice, I did greped for specific info().

while read LINE
do
IP_HOST=`nmap -sP $LINE |grep report |awk '{print $6}'|tr -d "()"`
echo -e "Whois Information of $LINE" > $LINE.WHO_HOST
echo -e "----------------------------------------------" >> $LINE.WHO_HOST
whois $LINE |grep -E 'Dom|Nam|Num|Cou|Ema|Spo|Cre|Las|URL' >> $LINE.WHO_HOST
echo -e "Whois Information of $IP_HOST [ $LINE ]" > $LINE.WHO_IP
echo -e "----------------------------------------------" >> $LINE.WHO_IP
whois $IP_HOST >> $LINE.WHO_IP
WHO_RED=`cat $LINE.WHO_IP |grep show|cut -c3-`
cat $LINE.WHO_IP |grep show >/dev/null
if [ "$?" == "0" ]
then
wget $WHO_RED
echo -e "HTML File Saved" "\t\t\t\t\t\t\t$C_G[ OK ]$C_END"
fi

done < SUSPECT_FILE
}

# Zone Transfers, usually using fierce tool by Robert "RSnake" Hansen from,
#: Ha.ckers.org, which also come with BT default tools at
#: /pentest/enumeration/dns/

zone_transfer() {
while read LINE
do
ZONEFILE="/home/hackuin/Hack/ZONE-TRANSFERS"
echo -e "Geting Zone-Transfer Records"
echo -e "Zone-Transfer for $LINE" >> ZONE-TRANSFERS
echo -e "----------------------------------------------" >> ZONE-TRANSFERS
cd
echo -e "Zone-Transfering......"
perl fierce.pl -dns $LINE >> $ZONEFILE
echo -e "Zone-Transfer Records are saved at $ZONEFILE"
echo -e "Zone Transfer DONE " "\t\t\t\t\t\t\t$C_G[ OK ]$C_END"

done < SUSPECT_FILE
}
host_scanning() {
cd ~/Hack/
while read LINE
do
echo -e "Scanning $LINE"
echo -e "Result_Scan of $LINE" > NMAP.$LINE
echo -e "-----------------------------------------------" >> NMAP.$LINE
nmap -sV $LINE | grep -E 'open|OS' >> NMAP.$LINE
echo
echo -e "Scanning of $LINE " "\t\t\t\t\t\t$C_G[ OK ]$C_END"
done < SUSPECT_FILE


echo -e "Here is the Interesting information"
echo -e "-----------------------------------------------"
while read LINE
do
cat NMAP.$LINE
echo

done < SUSPECT_FILE
echo -e "-----------------------------------------------"
}

loading() {
for (( DOT = 0; DOT < 5; DOT++))
do
echo -e ".\c"
sleep 1
done
echo
}


C_R="\e[01;31m" ## Colors
C_B="\e[01;30m"
C_G="\e[01;32m"
C_END="\e[00m"


clear
echo -e "Host Discovery Started........."
discover
if [ "$?" == "0" ]
then
echo -n "Getting Results"
loading
fi

echo -e "Whois info() STARTED......."
whoisinfo
echo -e "Whois info() DONE" "\t\t\t\t\t\t\t$C_G[ OK ]$C_END"
zone_transfer

echo -n "Starting Basic Hacking Steps"

loading
echo
echo -e "Scaning the host for Open Ports & Version Detection"
host_scanning

# END


There nothing much fancy about the script, but, due to my nature of work, as I said, I do same thing but, with different domains, so I did wrote it and all I need is to just replace the old "SUSPECT_FILE" with the new file. Which give me little info about the domain for further analysis/use the result in other scripts and soo.
There are plenty of other scripts which I wrote due of my work-flow to be redused, and get little more time for smoke ;) [ thats actually a funny part ].

I would suggest to read few of the facinating books, which I have upload below for your venture of bash/shell scripting.

Beginning shell scripting programmer to programmer -by Eric Foster-Johnson, John C. Welch, Micah Anderson.
Linux shell scripting cookbook - by Sarath Lakshman.
Shell scripting recipes a problem solution approach, expert voice in open source - by Chris F.A.Johnson.

Get these books over [ Here (http://www.megaupload.com/?d=IAO8BSFF) ]

Best of Luck.
Happy Scripting.

-Hackuin