Some Shell Scripts

Shell Script to add zone to DNS server.

NS1 = ns1.example.net
NS2 = ns2.example.net

#!/bin/bash
# check to see if the script is being run as root or not
grep=`which grep`
rndc=`which rndc`
if [ "$(id -u)" != "0" ]; then
echo "You must be root in order to run this script. You are `whoami`."
exit 1
fi
# if this is a cPanel server, exit the script as a DNS zone can be created from WHM
if [ -d "/usr/local/cpanel" ]; then
echo "This is a cPanel box. You can create the DNS zone from WHM."
exit 1;
# if this is a Plesk server, exit the script as the DNS zone can be created from Plesk itself
elif [ -d "/usr/local/psa" ]; then
echo "This is a Plesk box. You can create the DNS zone from the Plesk Control Panel."
exit 1;
else
domain=$1
echo "Please enter the domain name"
read domain
ip=$2
echo "Please enter the IP address for the domain"
read ip
test=`echo "${ip}." | $grep -E "([0-9]{1,3}\.){4}"`
if [ "$test" ]
then
echo "$ip" | nawk -F. '{
if ( (($1>=0) && ($1<=255)) &&    (($2>=0) && ($2<=255)) &&    (($3>=0) && ($3<=255)) &&    (($4>=0) && ($4<=255)) ) { print($0 " is a valid IP address. Using this IP." ); } else { print($0 ": Please specify a correct IP address." ); exit 1; } }' else echo "${ip} is not a valid IP address, exiting script!" exit 1; fi fi //echo "The zone file for the domain $domain will be created using $ip." //admin=$3 //echo "Please enter the administrator for this domain's DNS in a admin.domain.com form" //read admin //ns1=$4 //echo "Please enter the primary nameserver for $domain" //read ns1 //ns2=$5 //echo "Please enter the secondary nameserver for $domain" //read ns2 # ok, so enough with the chit-chat, let's move on to the DNS stuff serial=`date +%Y%m%d00` folder=`echo $domain|cut -c1` mkdir /var/named/$folder touch /var/named/$folder/$domain.zone echo -e "\$ORIGIN ." >> /var/named/$domain.zone
echo -e "\$TTL 600     ; 10 minutes" >> /var/named/$folder/$domain.zone
echo "$domain   IN     SOA    ns1.$domain. root.ns1.example.net. (" >> /var/named/$folder/$domain.zone
echo "               $serial    ; serial, todays date + todays serial" >> /var/named/$folder/$domain.zone
echo "               7200           ; refresh, seconds" >> /var/named/$folder/$domain.zone
echo "               3600            ; retry, seconds" >> /var/named/$folder/$domain.zone
echo "               43200         ; expire, seconds" >> /var/named/$folder/$domain.zone
echo "               3600 )            ; minimum, seconds" >> /var/named/$folder/$domain.zone
echo "                  IN       A    $ip" >> /var/named/$folder/$domain.zone
echo "                  IN       NS    ns1.example.net" >> /var/named/$folder/$domain.zone
echo "                  IN       NS    ns2.example.net" >> /var/named/$folder/$domain.zone
echo "                  IN       MX    5 mail.$domain." >> /var/named/$folder/$domain.zone
echo -e "\$ORIGIN $domain." >> /var/named/$folder/$domain.zone
echo "mail.$domain. IN     A     $ip" >> /var/named/$folder/$domain.zone
echo "www           IN     CNAME $domain." >> /var/named/$folder/$domain.zone
echo "ftp           IN     A     $ip" >> /var/named/$folder/$domain.zone
echo "sql           IN     A     $ip" >> /var/named/$folder/$domain.zone
echo "ns1           IN     A     $ip" >> /var/named/$folder/$domain.zone
echo "ns2           IN     A     $ip" >> /var/named/$folder/$domain.zone
echo "Done creating DNS zone, adding the zone to named.conf in named.zone file"
echo "zone \"$domain\" IN {" >> /etc/named/named.zone
echo "     type master;" >> /etc/named/named.zone
echo "     file \"/var/named/$folder/$domain.zone\";" >> /etc/named/named.zone
echo "     allow-transfer {" >> /etc/named/named.zone
echo "     10.10.11.243;" >> /etc/named/named.zone
echo "     };" >> /etc/named/named.zone
echo "};" >> /etc/named/named.zone
echo "Zone addded to named.conf. Restarting rndc & named"
sleep 2
$rndc reload
/etc/init.d/named restart
echo "All done"


Script to check if IP is blacklisted

#!/bin/sh
# -- $Id: blcheck.xml,v 1.8 2007/06/17 23:38:00 j65nko Exp $ --

#*/15 * * * * sh /root/spam.sh 38.111.101.66|mail -s "Spam Report in 15 Min" inct@rohtan.com
#*/15 * * * * sh /root/spam.sh 38.111.101.100|mail -s "Spam Report in 15 Min" inct@rohtan.com

# Check if an IP address is listed on one of the following blacklists
# The format is chosen to make it easy to add or delete
# The shell will strip multiple whitespace

BLISTS="
    cbl.abuseat.org
    dnsbl.sorbs.net
    bl.spamcop.net
    zen.spamhaus.org
    combined.njabl.org
"

# simple shell function to show an error message and exit
#  $0  : the name of shell script, $1 is the string passed as argument
# >&2  : redirect/send the message to stderr

ERROR() {
  echo $0 ERROR: $1 >&2
  exit 2
}

# -- Sanity check on parameters
[ $# -ne 1 ] && ERROR 'Please specify a single IP address'

# -- if the address consists of 4 groups of minimal 1, maximal digits, separated by '.'
# -- reverse the order
# -- if the address does not match these criteria the variable 'reverse will be empty'

reverse=$(echo $1 |
  sed -ne "s~^\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\)\.\([0-9]\{1,3\}\)$~\4.\3.\2.\1~p")

if [ "x${reverse}" = "x" ] ; then
      ERROR  "IMHO '$1' doesn't look like a valid IP address"
      exit 1
fi

# Assuming an IP address of 11.22.33.44 as parameter or argument

# If the IP address in $0 passes our crude regular expression check,
# the variable  ${reverse} will contain 44.33.22.11
# In this case the test will be:
#   [ "x44.33.22.11" = "x" ]
# This test will fail and the program will continue

# An empty '${reverse}' means that shell argument $1 doesn't pass our simple IP address check
# In that case the test will be:
#   [ "x" = "x" ]
# This evaluates to true, so the script will call the ERROR function and quit

# -- do a reverse ( address -> name) DNS lookup
REVERSE_DNS=$(dig +short -x $1)

echo IP $1 NAME ${REVERSE_DNS:----}

# -- cycle through all the blacklists
for BL in ${BLISTS} ; do

    # print the UTC date (without linefeed)
    printf $(env TZ=UTC date "+%Y-%m-%d_%H:%M:%S_%Z")

    # show the reversed IP and append the name of the blacklist
    printf "%-40s" " ${reverse}.${BL}."

    # use dig to lookup the name in the blacklist
    #echo "$(dig +short -t a ${reverse}.${BL}. |  tr '\n' ' ')"
    LISTED="$(dig +short -t a ${reverse}.${BL}.)"
    echo ${LISTED:----}

done

# --- EOT ------

This entry was posted in Linux, Shell Script. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *