Showing posts with label shell script. Show all posts
Showing posts with label shell script. Show all posts

Friday, April 1, 2016

Shell Scripting for Dovecot configuring in Linux

Dovecot is an open-source IMAP and POP3 server for Linux/UNIX-like frameworks, composed essentially with security in mind. Timo Sirainen began Dovecot and first discharged it in July 2002. Dovecot engineers basically mean to deliver a lightweight, quick and simple to-set-up open-source mailserver.
 
 
echo "****  Configuring Dovecot    **************************"
   if rpm -qa|grep dovecot
   then
   {
   echo "**************   Dovecot Installation  ****************"
   echo  '!include conf.d/*.conf' > /etc/dovecot/dovecot.conf
   echo "protocols = imap pop3 lmtp">>/etc/dovecot/dovecot.conf
   echo 'mbox_write_locks = fcntl
   mail_location = maildir:~/Maildir
   '>/etc/dovecot/conf.d/10-mail.conf
   echo 'disable_plaintext_auth = no
   !include auth-system.conf.ext
   auth_mechanisms = plain login'>/etc/dovecot/conf.d/10-auth.conf
 
   echo "********* Service Dovecot Restart *********************"
   clear
   echo ""
   echo ""
   service dovecot restart
   echo ""
   echo "  *************************************************************"
   echo "  *    Dovecot configuration has benn completed               *"
   echo "  *                Enjoy it                                   *"
   echo "  *************************************************************"
   }
 else
  echo "   *************************************************************"
  echo "   *           There is no rpm for dovecot                     *"
  echo "   *           So please install rpm first                     *"
  echo "   *************************************************************"
  fi
 
 

For Basic Configuration This is enough to receive mail. We will must add rules in IPTABLES for this.
 

Scripting for Postfix Mail Server configuration

echo "    *************** First RPM check ***********"
if rpm -qa|grep postfix
 then
 {
  echo "Write down your domain name"
  read "l"
  echo "write down your hostname"
  read "h"
  echo "write down your network like 192.168.1.0/24"
  read "n"
  echo ""
  echo '
  inet_interfaces = all
  myhostname = '$h'
  myorigin = '$l'
  mydomain = '$l'
  mydestination = $myhostname, $mydomain, $myorigin
  relayhost = [smtp.'$l']
  mynetworks = '$n' 127.0.0.0/8
  ' > /etc/postfix/main.cf


  #************************
  echo "which one u use ?"
  echo "  1: Squirrel"
  echo "  2: Open Webmail"
  echo "  3: Other"
  read a
  if [ $a -eq 1 ]
    then 
    {
     echo "home_mailbox = Mailbox" >>/etc/postfix/main.cf
    }
  elif [ $a -eq 2 ]
    then
    {
     echo "#home_mailbox = !default" >>/etc/postfix/main.cf
    }
  else
    {
     echo "home_mailbox = Maildir">>/etc/postfix/main.cf
    }
  fi
  echo ""
  clear
  echo ""
  echo ""
  service postfix restart
  echo ""
  echo "************************************************************"
  echo "*    Postfix configuration has been completed              *"
  echo "*                   Enjoy It                               *"
  echo "************************************************************"
 }
else 
 {
 clear
 echo ""
 echo ""
 echo "  *************************************************************"
 echo "  *          No rpm installed for postfix                     *"
 echo "  *          Please install the rpm first                     *"
 echo "  *************************************************************"
 }
fi
echo "******  Do u want to configure dovecot ?  **********************"
echo "  1 > press 1 for yes"
echo "  2 > press 2 for no "
echo ""
read a
if [ $a -eq 1 ]
 then
  {
   ./dovecot.sh
  }
else 
 echo "***** Ok Dovecot is not configured *******"
fi
 

Wednesday, February 17, 2016

DNS server configuration by shell scripting

******************************************************
Just copy the content and save it to dns.sh file. set permission like
******************************************************    
     # chmod 777 dns.sh
     #./dns.sh
*******************************************************



echo "write down the domain name"
read "l"

echo "write down the iP address blockwise"
echo "First Block:"
read "i1"
echo "Second Block"
read "i2"
echo "Third Block"
read "i3"
echo "Fourth Block"
read "i4"
echo "Write down Subnet Mask"
read "s"
sleep 1

echo "*****************************************************************"
echo "*     '$i1'.'$i2'.'$i3'.'$i4'                     *"
echo "*                   '$s'                                        *"
echo "* ***************************************************************"
echo ""
echo "" 
echo "write down the host name"

read "h"
echo ""
hostname $h
echo 'hostname'
hostname
hostname 
echo > /etc/resolv.conf '
search '$l'
nameserver '$i1'.'$i2'.'$i3'.'$i4''
echo > /etc/sysconfig/network-scripts/ifcfg-eth0 '
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR='$i1'.'$i2'.'$i3'.'$i4'
NETMASK='$S'
'


service network restart

echo > /etc/sysconfig/network '
NETWORKING=yes
HOSTNAME='$h''

#echo >> /etc/resolv.conf '
#nameserver '$i1'.'$i2'.'$i3'.'$i4''

echo > /etc/named.conf '

options {
 listen-on port 53 { 127.0.0.1; '$i1'.'$i2'.'$i3'.'$i4'; };
 listen-on-v6 port 53 { ::1; };
 directory  "/var/named";
 dump-file  "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
// allow-query     { localhost; };
 recursion yes;

 dnssec-enable yes;
 dnssec-validation yes;
 dnssec-lookaside auto;

 bindkeys-file "/etc/named.iscdlv.key";
};

logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};

zone "'$l'" IN {
 type master;
 file "'$l'.for";
 allow-update { none; };
};

zone "'$i3'.'$i2'.'$i1'.in-addr.arpa" IN {
 type master;
 file "'$l'.rev";
 allow-update { none; };
};
'

touch /var/named/$l.for

touch /var/named/$l.rev

echo "***************************************************************"

echo > /var/named/$l.for '
$TTL 1D
@    IN SOA '$h'.'$l'. root.'$l'. (
     0  ; serial)
     1D  ; refresh
     1H  ; retry
     1W  ; expiry
     3H )  ; minimum

         IN NS '$h'.'$l'.
'$h'  IN A '$i1'.'$i2'.'$i3'.'$i4'
www.'$l'. IN A '$i1'.'$i2'.'$i3'.'$i4''

sleep 1
echo "****************************************************************"

echo ""
echo ""

echo > /var/named/$l.rev '
$TTL 1D
@       IN      SOA     '$h'.'$l'. root.'$l'.  (
                                      0          ; Serial
                                      1D         ; Refresh
                                      1H         ; Retry
                                      1W         ; Expire
                                      3H  )      ; Minimum
              IN      NS      '$h'.'$l'.

'$i4'       IN      PTR     '$h'.'$l'. '
echo ""
killall named
echo ""
echo "*****************************************************************"
service named start
echo ""
echo ""
chown root.named /var/named/$l.for
chown root.named /var/named/$l.rev

echo "******************************************************************"
ping www.$l -c 4
echo "******************************************************************"
clear
 
echo ""
echo ""
echo "**************************************************************"
echo "*      DNS Server Configuration has been Completed           *"
echo "*      Please Check Your Configuration by nslookup command   *"
echo "**************************************************************"
echo ""

Sunday, February 14, 2016

Script for finding a file and copy to a Specific directory

#!/bin/bash
# Script for finding a file and copy to a Specific directory
read -p "write the name of file: " a

file=`find / -name $a`
if [ $? -eq 0 ]
then
 {
  echo "*********************"
  echo "* $a File found which u r looking for     *"
  echo "*********************"
  read -p " where do u want to copy it: " b
  cp $file $b
}
else
 echo "file not found"
fi

###########################################
IF we find  More than one  files then we can use this script
###########################################
#!/bin/bash
 read -p "write down the file name : " file
find / -name $file>/shell/find
grep $file /shell/find
if [ $? -eq 0 ]
   then
  {
    valfile=`cat /shell/find`
    line=`sed -n 2p /shell/find`
    if [ ! -z "$line" ]
      then
      {
       echo ""
       read -p "Hey !!! select to copy from above" c
       echo ""
       read -p "Place where to copy :" d
       cp $c $d
       echo ""
       echo "File Copied to $d"
       echo ""
      }
    else
      {
       echo ""
       read -p "Place where to copy :" f
       cp $valfile $f
       echo ""
       echo "File Copied to $f"
       echo ""
      }
    fi
  }
  else
    echo ""
    echo "File not exixt"
    echo ""
  fi

Adding a linux user by shell scripting

#!/bin/bash

if [ $(id -u) -eq 0 ]; then #checking you either root or normal user
    echo -n " write the username: "
    read  username          #Taking username from keyboard
    echo -n " write the Password: "
    read -s  password       #Taking password from keyboard
  
    #check the username in passwd file if exist and printing value to null
    grep "$username" /etc/passwd >/dev/null
  
    if [ $? -eq 0 ]; then
        echo "***************************"
        echo " $username already exist  *"
        echo "***************************"
        exit 1
    else
        pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) #encrypting password
        useradd -m -p $pass $username
        [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
    fi
else
    echo ""
    echo "**********************************************************"
    echo " U are not root. Only root may add a user to the system  *"
    echo "**********************************************************"
    exit 2
fi

Friday, January 29, 2016

Migrating Mail Server from old server to new server

Postfix Mail Server Migration from old server to new server
***************************************************
First of all we will archive all existing mail userid and password.

old_server: 192.168.1.76
#########################

export UGIDLIMIT=500
#create a folder where we will store all existing mail,userid and their password. 
mkdir -p /root/migrate
cd /root/migrate

awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd > /root/passfile/passwd.mig
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/group > /root/passfile/group.mig
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534) {print $1}' /etc/passwd | tee - |egrep -f - /etc/shadow > /root/passfile/shadow.mig
cp /etc/gshadow /root/passfile/gshadow.mig

tar -zcvpf /root/migrate/home.tar.gz /home
tar -zcvpf /root/migrate/mail.tar.gz /var/spool/mail

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

After that the migrate folder will be copied to New server. We will configure postfix in new server.

new_server: 192.168.1.98
#########################################


export UGIDLIMIT=500
mkdir -p /root/backup
cd /root/backup
scp 192.168.1.76:/root/migrate/* .
cat passwd.mig >> /etc/passwd
cat group.mig >> /etc/group
cat shadow.mig >> /etc/shadow
cp gshadow.mig /etc/gshadow
cd /
tar -zxvf /root/backup/home.tar.gz
tar -zxvf /root/backup/mail.tar.gz