Friday, April 15, 2011

How to limit the connections from one ip address using iptables

The following command will block all the exceeding connections [ more than 3] from any ip address to this machine. This can be used against DDOS attack.
#iptables  -A INPUT -p all   -m connlimit --connlimit-above 3 -j REJECT

Suppose if you want to block only ssh connections, then use the following command.
iptables  -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT

for allowing 2 telnet connections per client host

#iptables -A INPUT -p tcp --syn --dport 23 -m connlimit --connlimit-above 2 -j REJECT

you can also do as give below
#iptables  -A INPUT -p tcp --syn --dport 23 -m connlimit ! --connlimit-above 2 -j ACCEPT

For limiting  the number of parallel http requests to 16 per class C sized network (24 bit netmask)
#iptables  -A INPUT-p tcp --syn --dport 80 -m connlimit --connlimit-above 16 --connlimit-mask 24 -j REJECT

Tuesday, April 12, 2011

understanding if, if-else, elsif, unless, for, foreach and while loops in perl scripting

The following post explains  if, if-else, elsif, unless, for, foreach and while loops in perl scripting

syntax :
if ( condition ) { statement; }
if ( condition ) { statement1; } else { statement2)
if ( condition1 ) { statement1; } elsif ( condition2 ) { statement2; }
unless ( condition ) { statement;}
for (Initial_value; condition; operation_on_variable) { statement; }
for (min_value..max_value) { statement; }
foreach (@array_name) { print "$_\n"; }

See the example program for more clear view.

#!/usr/bin/perl
#Purpose - To understand the concept of conditionals.
#START
#modules used
use strict;

#Variables used
my $value1 = 5;
my $value2 = 10;
my $value3 = 15;
my $value4 = 15;
my $i = 0;
my @array = ("Linux", "Windows", "Solaris", "Unix");

    #---------------------------------------------------------------------
    #if loop
    if (1 < 2) {
        print "1 is less than 2\n";
    }
   
    #---------------------------------------------------------------------
    #if-else loop
    if (2 < 1){
        print "The system is mad\n";
    }
    else {
        print "2 is not less than 1, This machine is brilliant\n";
    }
   

    #---------------------------------------------------------------------
    #elsif loop
    if ($value3 > $value4) {
        print "$value3 is greater than $value4\n";
    }
    elsif ($value3 < $value4) {
        print "$value3 is less than $value4\n";
    }
    else {
        print "$value3 is equal to $value4\n";
    }
   
   
    #---------------------------------------------------------------------
    #unless loop
    unless ($value1 == $value2) {
        print "Oh the $value1 and $value2 are not same\n";
    }
   
    #---------------------------------------------------------------------
    #For loop
    for ($i=1; $i<=5; $i++) {print "$i\n";}
    for (1..5) {print "new_for_loop\n";}

   
    #foreach loop
    foreach (@array) {print "$_\n";}


    #---------------------------------------------------------------------
    #While loop
    while ($value1 < $value2) {
        print "$value1 is less than $value2\n";
        $value1 +=1;
        print "With 1 increment $value1\n";
    if ($value1 == $value2) {print "Now the values are same\n";}
    }

#END

Understanding push, pop, unshift, shift and sort in perl scripting

The following post explains the concepts of push, pop, unshift, shift and sort functions in perl scripting.

Array : Array is a collection of elements. its elements are numbered from 0 to n-1.

pop - Removes the last element from the array.
push - insert given element(s) to the end of the array.
unshift - Inserts the given element(s) to the beginning of the array.
shift - removes the first element of the array.
sort - sorts the array in alphabetical order.

Syntax:
pop @array_name;
push @array_name, element(s)_to_be_inserted;
unshift (@array_name, "element(s)_to_be_inserted");
shift @array_name;
my @array_name = sort @array_name;

Here is a simple example.

#!/usr/bin/perl
#Purpose - To understand the concepts of Common array functions.
#PUSH POP SHIFT UNSHIFT and SORT

#START

#Modules using
use strict;

my @array = ("1","2","3","4");
    print "The initial array elements are : @array\n";

#popping an element from the array.
my $popped_element = pop @array;
    print "The array elements after popping are : @array\n";
    print "The popped element is : $popped_element\n";


#Pushing the popped element back to the array.
push @array, $popped_element;
    print "The array elements after pushing are : @array\n";
push @array, 5, 6, 7, 8;
        print "The array elements after pushing 5, 6, 7, 8 are : @array\n";


#Unshift : Unshifting means it'll add the specified element to the begining of the array
    print "unshifting the array with element : 0\n";
unshift (@array, "0");
    print "Array elements after unshifting are : @array\n";


#shift : shift removes the first element from the array.
shift @array;
    print "The elements after the operation shift are : @array\n";


#Sorting the elements of an array.
my @array1 = ("Randeep", "Saju", "Remil", "Ajith", "Nibul" );
    print "The array1 before sorting is : @array1\n";
my @array1 = sort @array1;
    print "The array1 after sorting is : @array1\n";
#END