Skip to content

Commit 6b87bda

Browse files
committed
Extra Files addedgit status!
1 parent 94c8a2b commit 6b87bda

File tree

62 files changed

+982
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+982
-0
lines changed
23.4 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# Initializing variables with a value
4+
number1=5; number2=5
5+
6+
# Expression "[ $number1 -eq $number2 ]" that resolves to a Boolean value.
7+
if [ $number1 -eq $number2 ]; then
8+
# Code to run if condition is true
9+
echo "$number1 is equal to $number2"
10+
else
11+
# Code to run if condition is false
12+
echo "$number1 is not equal to $number2"
13+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# Loop through the items delimited by whitespace in the "names" variable.
4+
names="Name1 Name2 Name3"
5+
6+
for name in $names; do
7+
echo "Hello $name"
8+
done
9+
10+
# Loop through the items in the substituted sequence command.
11+
for number in `seq 1 10`; do
12+
echo "I am $number"
13+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# Defining the function "greet" that accepts a parameter
4+
function greet {
5+
echo "Hello $1!"
6+
}
7+
8+
# Calling the function "greet" with parameter "World"
9+
greet "World"
10+
11+
# Calling the function "greet" with parameter "User"
12+
greet "User"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
# Creating a readonly variable called "greeting"
4+
readonly greeting="Hello"
5+
6+
# Creating an ordinary variable called "current_time"
7+
current_time=`date +%X`
8+
9+
# Substitute the output of the command "whoami".
10+
echo "Hi, I am $(whoami)."
11+
12+
# Prompt the user to provide their name
13+
read -e -p "Who are you? " myName
14+
15+
# Substituting the variables and printing them
16+
echo "${greeting:-Hi}, $myName. Now the time is $current_time"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
# Reading the text from stdin and assigning it to the variable named "myName"
4+
read myName
5+
6+
# Substitute the variable and print the text
7+
echo "Hello ${myName}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# Initializing variables with a value
4+
number1=5; number2=5
5+
6+
# Evaluating the expression
7+
if [ $number1 -gt $number2 ]; then
8+
echo "$number1 is greater than $number2"
9+
elif [ $number1 -lt $number2 ]; then
10+
echo "$number1 is lesser than $number2"
11+
else
12+
echo "$number1 is equal to $number2"
13+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
# Initializing variables with a value
4+
number1=5; number2=5
5+
6+
# Evaluating the expression
7+
if [ $number1 -ne $number2 ]; then
8+
if [ $number1 -gt $number2 ]; then
9+
echo "$number1 is greater than $number2"
10+
else
11+
echo "$number1 is lesser than $number2"
12+
fi
13+
else
14+
echo "$number1 is equal to $number2"
15+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
# Send the text to the stdout
4+
echo "any standard text can be here"
5+
6+
# Send the text to the stderr
7+
echo "any error text can be here" >&2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
name="john"
4+
5+
# A string is given as input to case statement.
6+
case "$name" in
7+
8+
# Checks for the switch that matches and executes that action.
9+
john) echo "Welcome Admin" ;;
10+
alexa) echo "Welcome User" ;;
11+
12+
# If none is matched, "*" switch gets executed.
13+
*) echo "Access Denied" ;;
14+
esac
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
# Assigning the text "User!" to the variable
4+
myName="User!"
5+
6+
# Substituting it by enclosing the variable name in ${}
7+
echo "Hello ${myName}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
count="9"
4+
5+
# Evaluates whether "count" is greater than zero and if true, enters loop.
6+
while [ $count -gt 0 ]; do
7+
8+
# Print the "count"
9+
echo "$count"
10+
11+
# Decrease the value of count
12+
count=$[$count-1]
13+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
read -e -p "Log Directory: " log_directory
4+
5+
read -e -p "File Extension: " extension
6+
7+
read -e -p "Backup Directory: " backup_directory
8+
9+
tar czf archive.tar.gz $(find $log_directory -name "*.$extension")
10+
11+
mv archive.tar.gz $backup_directory/$(date +%F).tar.gz
12+
13+
rm $(find $log_directory -name "*.$extension")
14+
15+
exit 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
removelog="/var/log/remove.log"
4+
5+
if [ $# -eq 0 ] ; then
6+
echo "Usage: $0 [-s] list of files or directories"; exit 0;
7+
fi
8+
9+
if [ "$1" = "-s" ] ; then
10+
shift
11+
else
12+
echo "$(date): ${USER}: $@" >> $removelog
13+
fi
14+
15+
/bin/rm "$@"
16+
17+
exit 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
downloads_directory="$HOME/Downloads"
4+
5+
if [ -z "$1" ]; then
6+
echo "ERROR: Download List file is not specified in the arguments" >&2; exit 1;
7+
fi
8+
9+
if ! [ -z "$2" ]; then
10+
downloads_directory=$2
11+
fi
12+
13+
if [ ! -d $downloads_directory ] ; then
14+
mkdir -p $downloads_directory
15+
fi
16+
17+
18+
download_list=$1
19+
20+
cat $download_list | while read url; do
21+
echo "----------------------------------------------------------------------------------------------------"
22+
echo "$url"
23+
wget -P $downloads_directory $url
24+
done
25+
26+
exit 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
echo -e "\n\nUpdating Apt Packages and upgrading latest patches\n"
4+
sudo apt-get update -y && sudo apt-get upgrade -y
5+
6+
echo -e "\n\nInstalling Apache2 Web server\n"
7+
sudo apt-get install apache2 apache2-doc apache2-mpm-prefork apache2-utils libexpat1 ssl-cert -y
8+
9+
echo -e "\n\nInstalling PHP & Requirements\n"
10+
sudo apt-get install libapache2-mod-php7.0 php7.0 php7.0-common php7.0-curl php7.0-dev php7.0-gd php-pear php7.0-mcrypt php7.0-mysql -y
11+
12+
echo -e "\n\nInstalling MySQL\n"
13+
sudo apt-get install mysql-server mysql-client -y
14+
15+
echo -e "\n\nPermissions for /var/www\n"
16+
sudo chown -R www-data:www-data /var/www
17+
echo -e "\n\n Permissions have been set\n"
18+
19+
echo -e "\n\nEnabling Modules\n"
20+
sudo a2enmod rewrite
21+
sudo phpenmod mcrypt
22+
23+
echo -e "\n\nRestarting Apache\n"
24+
sudo service apache2 restart
25+
26+
echo -e "\n\nLAMP Installation Completed"
27+
28+
exit 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
if [ -z "$1" ]; then
4+
echo "ERROR: No argument supplied" >&2; exit 1;
5+
fi
6+
7+
archives_directory=$(realpath $1)
8+
if [ ! -d "$archives_directory" ]; then
9+
echo "ERROR: Archives directory does not exist" >&2; exit 1;
10+
fi
11+
12+
find $archives_directory -type f -name '*.tar.gz' -mmin +$((60 * 24 * 2)) -exec rm {} \;
13+
14+
path_to_script=$(realpath "$0")
15+
if ! (crontab -l | grep -Fxq "0 0 * * * $path_to_script $archives_directory"); then
16+
crontab -l | { cat; echo "0 0 * * * $path_to_script $archives_directory"; } | crontab -
17+
echo "Script added to Cron"
18+
fi
19+
20+
exit 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
if [ -z "$1" ]; then
4+
echo "ERROR: Credentials file not specified" >&2; exit 1;
5+
elif [ -z "$2" ]; then
6+
echo "ERROR: Backup directory not specified" >&2; exit 1;
7+
fi
8+
9+
credentials_file=$(realpath $1)
10+
backup_directory=$(realpath $2)
11+
if [ ! -f "$credentials_file" ]; then
12+
echo "ERROR: Credentials file does not exist" >&2; exit 1;
13+
elif [ ! -d "$backup_directory" ]; then
14+
echo "ERROR: Backup directory does not exist" >&2; exit 1;
15+
fi
16+
17+
source $credentials_file
18+
19+
if [ -z ${hostname:+word} ]; then
20+
echo "ERROR: hostname is not set" >&2; exit 1;
21+
elif [ -z ${username:+word} ]; then
22+
echo "ERROR: username is not set" >&2; exit 1;
23+
elif [ -z ${password:+word} ]; then
24+
echo "ERROR: password is not set" >&2; exit 1;
25+
fi
26+
27+
mysqldump -h$hostname -u$username -p$password --all-databases > backup.sql
28+
if [[ $? != 0 ]]; then
29+
echo "ERROR: Error in taking mysql backup" >&2; exit 1;
30+
fi
31+
32+
mv backup.sql $backup_directory/$(date +%F_%R).sql
33+
34+
path_to_script=$(realpath "$0")
35+
if ! (crontab -l | grep -Fxq "0 */12 * * * $path_to_script $credentials_file $backup_directory"); then
36+
crontab -l | { cat; echo "0 */12 * * * $path_to_script $credentials_file $backup_directory"; } | crontab -
37+
echo "Script added to Cron"
38+
fi
39+
40+
exit 0
41+
42+
#
43+
# Credentials File
44+
#
45+
# hostname=localhost
46+
# username=root
47+
# password=password
48+
#
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
if [ -z "$1" ]; then
4+
echo "ERROR: Location of the web server's log is not specified" >&2; exit 1;
5+
fi
6+
7+
log_file=$(realpath $1)
8+
9+
10+
if [ ! -f "$log_file" ]; then
11+
echo "ERROR: Log file does not exist" >&2; exit 1;
12+
fi
13+
14+
(
15+
echo -e 'Apache Web Server Access Logs - Summary\n'
16+
echo -e 'STATUS\t\t-\tCOUNT'
17+
cat $log_file | sed 's/.*HTTP\/1\.1\" \(...\).*/\1/g' | sort | uniq -c | awk '{printf " %s\t\t-\t %s\n", $2, $1}'
18+
echo -e '\nIP ADDRESS\t-\tHITS'
19+
cat $log_file | sed 's/ .*//g' | sort | uniq -c | awk '{printf "%s\t-\t %s\n", $2, $1}'
20+
) > /tmp/log_summary
21+
22+
23+
cat /tmp/log_summary | mail -s "Apache Web Server Access Logs - Summary" $email_id
24+
if [[ $? != 0 ]]; then
25+
echo "ERROR: Error in sending email to $email" >&2; exit 1;
26+
fi
27+
28+
path_to_script=$(realpath "$0")
29+
if ! (crontab -l | grep -Fxq "0 * * * * $path_to_script $log_file"); then
30+
crontab -l | { cat; echo "0 * * * * $path_to_script $log_file"; } | crontab -
31+
echo "Script added to Cron"
32+
fi
33+
34+
exit 0
35+
# MailUtils should be configured
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
if [ -z "$1" ]; then
4+
echo "ERROR: Webserver's port is not specified in the arguments" >&2; exit 1;
5+
fi
6+
7+
listening_port=$1
8+
9+
netstat -lnt | grep -q ":$1 "
10+
11+
if [[ $? != 0 ]]; then
12+
echo "ERROR: Web server is not running";
13+
/etc/init.d/apache2 restart
14+
fi
15+
16+
path_to_script=$(realpath "$0")
17+
if ! (crontab -l | grep -Fxq "*/1 * * * * $path_to_script $listening_port"); then
18+
crontab -l | { cat; echo "*/1 * * * * $path_to_script $listening_port"; } | crontab -
19+
echo "Script added to Cron"
20+
fi
21+
22+
exit 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
if [[ $(id -u) -ne 0 ]] ; then
4+
echo "ERROR: Please run as root." ; exit 1 ;
5+
fi
6+
7+
if [ ! -f "/opt/forbidden_commands.txt" ]; then
8+
echo "ERROR: The file /root/forbidden_commands.txt does not exist" >&2; exit 1;
9+
fi
10+
11+
for user in $(getent passwd | cut -d : -f 6 | grep '/home' | sed 's:$:/.bashrc:'); do
12+
cat $user | grep -q 'validate_commands'
13+
if [[ $? != 0 ]]; then
14+
echo '
15+
function validate_commands {
16+
cat /opt/forbidden_commands.txt | while read command; do
17+
if [ "$BASH_COMMAND" == "$command" ]; then
18+
echo "Sorry! \"$BASH_COMMAND\" cannot be executed";
19+
exit 1;
20+
fi
21+
done
22+
}
23+
trap validate_commands DEBUG' >> $user
24+
fi
25+
done
26+
27+
exit 0

0 commit comments

Comments
 (0)