Shell Scripting

neoindian

Disciple
I belive that i can post shell scripting in here. :) If not then mods please move this post to appropos place.:huh:

Guys i read all the c,c++ and stuff but lets start discussing shell scripting. Like u can post a real scripting probs, i try to solve it and then post the soln.Others can also give their solns. It wl b fun , and we can polish our scripting skills

Come join the party.
 
@neo write a bash script to analyse a Apache log file for signs of certain attacks, say Code Red. And put all the IP's requesting default.ida file into a file with the number of times an IP has requested for the file.

And if u know a bit of linux, then add a IPTable rule also to drop packets from such an IP.

I did this but its in Perl.
 
First and foremost i dont have access to apache server in office. SO used a demo log file.

Script is very basic. It answers the first part of ur qs. It scans the log file and logs all the ips which requested default.ida(code red attack), but the number of times they requested is not included.

And i will read up on IPTable rules and modify the script. This is a very raw script.

#! /bin/bash

getlist(){

line="$@"

count=`echo $line|grep 'GET'|grep 'default.ida'|wc -l`

if [ $count = 1 ]; then

echo $line | tr -s " " | cut -d " " -f1 >> list.txt

fi

}

FILE=""

if [ "$1" == "" ]; then

FILE="/dev/stdin"

else

FILE="$1"

fi

if [ ! -r $FILE ]; then

echo "$FILE : cannot read"

exit 2

fi

exec 3<&0

exec 0<$FILE

while read line

do

getlist $line

done

exec 0<&3

exit 0

The list.txt file contains the list of ips.
 
Arrgh sorry to mention . U just mention the absolute path of the log file at commant prompt

For example "./script.sh fullpath/log"

else it can be hard coded. But was following gud programming practice.
 
Ahah... nice thread Nishant!

Ok, let me put forth a simple challenge.

Write a program which prints itself. Meaning the output should exactly be the mirror of the source code. So that if i did this.....

(assuming your file is called challenge.sh)

if [ `cat challenge.sh` = `./challenge.sh` ]; then

echo "Passed !"

fi

Any takers?
 
one clarification. Suppose u type ./challenge.sh at cmd prompt, then whatever is the content of that script shld print itself.

Am i right or wrong? Need to clear this before i write a script
 
#! /bin/bash

getlist(){

line="$@"

echo $line

}

FILE=""

if [ "$0" == "" ]; then

FILE="/dev/stdin"

else

`${"$0"#"./"}`

FILE="$0"

fi

if [ ! -r $FILE ]; then

echo "$FILE : cannot read"

exit 2

fi

exec 3<&0

exec 0<$FILE

while read line

do

getlist $line

done

exec 0<&3

exit 0

If u just run this script it basically prints its source code. Thats the purpose . Hey Karan is this what u wanted.
 
Nah Neo. That is not what i said.

Getting the IP accessing default.ida is a simple one line statement in bash ( with pipes ).

I want to know how many times an ip has requested the file. Anyways that is not difficult in Perl but wanted to know the same in Bash.
 
Josh it can be done. U just need to search each line in log file for the ip and the GET request and thn for deafult.ida request . The list of ips u already have extracted in the list.txt. So its done this way.

So for each ip making a GET request, u have to loop through the log file

This is very rudimentary. Thr is a simple way to do this but darn cant remember the xact cmnd
 
KiD0M4N said:
Write a program which prints itself. Meaning the output should exactly be the mirror of the source code. So that if i did this.....
useless geeky knowledge follows:

programs that print their source code on running are called quines

lots of info aobut them on google coded in just about all languages written yet
 
Back
Top