diff --git a/AWS/s3bucketsize.sh b/AWS/s3bucketsize.sh new file mode 100644 index 0000000..515783c --- /dev/null +++ b/AWS/s3bucketsize.sh @@ -0,0 +1,18 @@ +#!/bin/bash +#Purpose: To Know S3 Bucket Size Shell Script +#Version:1.0 +#Created Date: 09-Jan-2019 +#Modified Date: +#WebSite: https://www.server-computer.com +#Author: Ankam Ravi Kumar +# START # +echo -e "Please Enter your Bucket Name: \c" +read -r BUCKETNAME +aws s3api list-objects --bucket $BUCKETNAME --output json --query "[sum(Contents[].Size)]" > $PWD/s3bucket +sed -i 's/\[//' $PWD/s3bucket +sed -i 's/]//' $PWD/s3bucket +sed -i 's/ //' $PWD/s3bucket +cat $PWD/s3bucket |head -2 |tail -1 |awk '{print int($1/1024)" KB"}' +cat $PWD/s3bucket |head -2 |tail -1 |awk '{print int($1/1024/1024)" MB"}' +cat $PWD/s3bucket |head -2 |tail -1 |awk '{print int($1/1024/1024/1024)" GB"}' +cat $PWD/s3bucket |head -2 |tail -1 |awk '{print int($1/1024/1024/1024/1024)" TB"} diff --git a/Arthemetic-Operators.sh b/Arthemetic-Operators.sh index b1c412a..0a29f3f 100644 --- a/Arthemetic-Operators.sh +++ b/Arthemetic-Operators.sh @@ -5,16 +5,17 @@ #Modified Date: #Author: Ankam Ravi Kumar # START # + echo -e "Please enter some value: \c" read -r a echo -e "Please enter another value: \c" read -r b -echo "a+b value is $(($a+$b))" -echo "a-b value is $(($a-$b))" -echo "axb value is $(($a*$b))" -echo "a/b value is $(($a/$b))" -echo "a%b value is $(($a%$b))" +echo "a+b value is $(($a+$b))" #it will add both the values +echo "a-b value is $(($a-$b))" #it will subtract b form a +echo "axb value is $(($a*$b))" #it will multiply both a and b +echo "a/b value is $(($a/$b))" #it will divide b from a +echo "a%b value is $(($a%$b))" #it will give the remainder when a is divided by b echo "Completed successfully" diff --git a/AutomateLoginSSH.sh b/AutomateLoginSSH.sh new file mode 100644 index 0000000..2550268 --- /dev/null +++ b/AutomateLoginSSH.sh @@ -0,0 +1,20 @@ +#!/usr/bin/expect -f +## Testing expect command + +## echo the test +puts "\nGet HostName\n" + +## execute ssh command to connect to remote host +spawn ssh 192.168.175.130 "hostname" + +## Look for password string +expect "password:" + +## Send the password +send "redhat\r" + +puts "\nGet df command output\n" +spawn ssh 192.168.175.130 "df -h" +expect "password:" +send "redhat\r" +interact diff --git a/CpuMemDisk.sh b/CpuMemDisk.sh new file mode 100644 index 0000000..441fe0c --- /dev/null +++ b/CpuMemDisk.sh @@ -0,0 +1,28 @@ +#!/bin/bash +## Collect Multiple Servers CPU, MEM and DISK Utilization and store in single file +# Purpose: To Collect Multiple Servers CPU, MEM, DISK usage in single report +# Version:1.0 +# Created Date: 2019-05-02 +# Modified Date: +# WebSite: https://arkit.co.in +# Author: Ankam Ravi Kumar + +HOSTNAME=$(hostname) +DATET=$(date "+%Y-%m-%d %H:%M:%S") +CPUUSAGE=$(top -b -n 2 -d1 | grep "Cpu(s)" | tail -n1 | awk '{print $2}' |awk -F. '{print $1}') +MEMUSAGE=$(free | grep Mem | awk '{print $3/$2 * 100.0}') +DISKUSAGE=$(df -h / | awk '{print $5}' |tail -n 1 |sed 's/%//g') + +echo 'HostName, Date&Time, CPU(%), MEM(%), DISK(%)' >> /opt/usagereport +echo "$HOSTNAME, $DATET, $CPUUSAGE, $MEMUSAGE, $DISKUSAGE" >> /opt/usagereport + +for i in `cat /scripts/hostlist` +do +RHOST=$(ssh $i hostname) +RDATET=$(ssh $i 'date "+%Y-%m-%d %H:%M:%S"') +RCPUUSAGE=$(ssh $i top -b -n 2 -d1 | grep "Cpu(s)" | tail -n1 | awk '{print $2}' |awk -F. '{print $1}') +RMEMUSAGE=$(ssh $i free | grep Mem | awk '{print $3/$2 * 100.0}') +RDISKUSAGE=$(ssh $i df -P / |column -t | awk '{print $5}' |tail -n 1 |sed 's/%//g') + +echo "$RHOST, $RDATET, $RCPUUSAGE, $RMEMUSAGE, $RDISKUSAGE" >> /opt/usagereport +done diff --git a/Even_or_Odd_Number.sh b/Even_or_Odd_Number.sh new file mode 100644 index 0000000..a8294a1 --- /dev/null +++ b/Even_or_Odd_Number.sh @@ -0,0 +1,10 @@ +#!/bin/bash +## To find given number is Even number or Odd Number + +read -p "Enter a number: " number + +if [ $((number % 2)) -eq 0 ]; then + echo "$number is an even number." +else + echo "$number is an odd number." +fi diff --git a/ExamResults.sh b/ExamResults.sh new file mode 100644 index 0000000..b718bdf --- /dev/null +++ b/ExamResults.sh @@ -0,0 +1,34 @@ +#!/bin/bash +#Purpose: Validate and report Student subject marks +#Version:1.0 +#Created Date: 2024 sep +#Modified Date: +#Author: Ankam Ravi Kumar +# START # + +echo -e "Please Enter Maths Marks: \c" +read -r m +echo -e "Please Enter Physics Marks: \c" +read -r p +echo -e "Please Enter Chemistry Marks: \c" +read -r c + +if [ $m -ge 35 -a $p -ge 35 -a $c -ge 35 ]; then +total=`expr $m + $p + $c` +avg=`expr $total / 3` +echo "Total Marks = $total" +echo "Average Marks = $avg" + if [ $avg -ge 75 ]; then + echo "Congrats you got Distinction" + elif [ $avg -ge 60 -a $avg -lt 75 ]; then + echo "Congrats you got First Class" + elif [ $avg -ge 50 -a $avg -lt 60 ]; then + echo "You got second class" + elif [ $avg -ge 35 -a $avg -lt 50 ]; then + echo "You Got Third Class" + fi +else +echo "Sorry You Failed" +fi + +# END # diff --git a/README.md b/README.md index 343b38a..fff2db0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,21 @@ -# shell-scripting-tutorial +# Shell Scripting Tutorial + +Keep in touch with for any kind of technical questions + +[Shell Scripting for Amazon Web Services to Manage it's resources](https://github.com/techtutorials/shell-scripting-tutorial/tree/FOTV/AWS) + +https://techtutorials.github.io/shell-scripting-tutorial/ + +* [Facebook](https://www.facebook.com/Linuxarkit/) +* [Email List](https://feedburner.google.com/fb/a/mailverify?uri=arkit) +* [Linkedin](https://in.linkedin.com/in/ravi-kumar-94530121) +* [Twitter](https://twitter.com/aravikumar48) +* [Youtube](https://www.youtube.com/Techarkit?sub_confirmation=1) +* [Email Address](aravikumar48@gmail.com) +* [WhatsApp Group](http://bit.ly/wappg) +* [Linux Telegram Group](http://bit.ly/linux-telegram) +* [Reddit TechTutorials](http://bit.ly/redditark) +* [Tumblr](https://www.tumblr.com/blog/aravikumar48) A complete begineers guide to learn shell scripting from scratch which includes Videos, Practice scenarios and project idea. I will create one file for one topic with code. @@ -12,102 +29,87 @@ alias and unalias, arch, arp, at, awk, bc, blkid, cal, cat, cd, chage, chattr, c After that start learning shell scripting using below topics -[Shell Scripting Video Tutorial](https://www.youtube.com/watch?v=7GNUzvjS_mE&list=PL8cE5Nxf6M6b8qW7CSMsdKbEsPdG9pWfu) - -Difference between scripting and programming - -What is shell scripting and it's advantages - -PATH environment variable - -What is sub-shell - -[Make Shell Script Template](https://www.youtube.com/watch?v=7KEQJ7jtkTg) - -[Quotes single, double and reverse - Know difference between each](https://www.youtube.com/watch?v=9_fhRI-dos4) - -Grab User input and Print - Using read command - -Bash colors - -Script exit status - -[Variables and it's rules](https://www.youtube.com/watch?v=839s_OtTqDA) - -[Special Variables](https://www.youtube.com/watch?v=PfxzX4XNYRE) - -Environment Variables, system variables and user defined variables - -Constant variables, Local & Global variables and Special variables - -Positional Parameters - -[Count number command line arguments $#](https://www.youtube.com/watch?v=YizjrX9ph10) - -[Arithmetic Operators](https://www.youtube.com/watch?v=qxNQ_D8txPo) - -[Relational Operators](https://www.youtube.com/watch?v=U-u1wx5VeTU) - -Relational ASCII operators - -[LogicalOperators](https://www.youtube.com/watch?v=m_F1FTKdUU4) - -Assignment Operators - -Boolean Operators - -Redirecting Input, output and errors - -Maths using expr command - -Real maths using bc command - -Do mathametics using let command - -Escape sequence - -[if statement](https://www.youtube.com/watch?v=gncu9vzmILw) - -[if-else statement](https://www.youtube.com/watch?v=nDhbOeEQeNY) - -[if-else-if statement](https://www.youtube.com/watch?v=UJET-9cmaqU) - -Nested if statement - -Case statement - -For Loop - -While Loop - -Until Loop - -Break, sleep, continue and Exit - -Functions - -Arrays - -Eval command - -Shifting parameters using shift command - -Tput making menu's - -Executing Multiple scripts from single script - -$(()) - Pass command to sub-shell - -logger logging messages to log file - -IFS - Input Field Separator - -Exec to send input to terminal - -Writing CPU Usage script - -Writing Disk Utilization script - -Trouble shooting debugging shell scripts - -Checking shell script errors and improvements using shellcheck.net site +- [Shell Scripting Video Tutorial](https://www.youtube.com/watch?v=7GNUzvjS_mE&list=PL8cE5Nxf6M6b8qW7CSMsdKbEsPdG9pWfu) +- [Shell Scripting course Overview](https://www.youtube.com/watch?v=7GNUzvjS_mE) +- [Linux Basics](https://www.youtube.com/watch?v=IFvMor-0eFM) +- [Linux Directory Structure](https://www.youtube.com/watch?v=rVxpe1_lNFE) +- [Linux Basic Commands](https://www.youtube.com/watch?v=yYC8aaQ3eZA) +- [Copy, Remove, Move and Time Commands](https://www.youtube.com/watch?v=G7XFreQkDB8) +- [Dif and Grep Commands](https://www.youtube.com/watch?v=RwcQ6JzTsmA) +- [Head, tail, sort and more commands](https://www.youtube.com/watch?v=OgV3qrPQulU) +- [tr and wc commands](https://www.youtube.com/watch?v=d40a5zFa8yI) +- [Disk utilities like fdisk, df and du commands](https://www.youtube.com/watch?v=vx1WZepOmKg) +- [Getting Help From Command Line user Interface](https://www.youtube.com/watch?v=GcYu-0IIJas) +- [w, who, hostnamem hostnamectl and uname commands](https://www.youtube.com/watch?v=7shAr5lp_Wc) +- [Search for files and directories using find and locate commands](https://www.youtube.com/watch?v=Rd6e-OrsHpo) +- [top command its output explanation](https://www.youtube.com/watch?v=UQ7rr4_47YY) +- [vi & vim text editor](https://www.youtube.com/watch?v=K3SUrcJ740Y) +- [sed, awk, vmstat and nestat commands](https://www.youtube.com/watch?v=4hJorSKg9E0) +- [vnstat command](https://www.youtube.com/watch?v=KlpE2Ok6Bxo) +- [Introduction to Graphical user interface](https://www.youtube.com/watch?v=Yck_xhz9ku0) +- [cut command](https://www.youtube.com/watch?v=kBZNJdw7RQQ) +- [Merge multiple files using paste command](https://www.youtube.com/watch?v=_Efd6PxhNq4) +- [Connect and Manage remote machine using SSH](https://www.youtube.com/watch?v=Dp9J7aktYDs) +- [Changing files and directory permissions](https://www.youtube.com/watch?v=NNAxqSyTsUI) +- [tar and zip commands](https://www.youtube.com/watch?v=lVQppyhgERA) +- [Scheduling future jobs using crontab](https://www.youtube.com/watch?v=OOOabNTnSwY) +- [difference between scripting and programming](https://www.youtube.com/watch?v=5UuTNosxNgI) +- [what is shell scripting and it's advantages](https://www.youtube.com/watch?v=m2DvuF_S4Ac) +- [PATH environment variable](https://www.youtube.com/watch?v=4TZyWegxzGY) +- [Symbols used shell scripting](https://www.youtube.com/watch?v=L8IxV7bvBHU) +- [Make Shell Script Template](https://www.youtube.com/watch?v=7KEQJ7jtkTg) +- [Quotes single, double and reverse - Know difference between each](https://www.youtube.com/watch?v=9_fhRI-dos4) +- [Bash colors](https://arkit.co.in/coloring-style-text-shell-scripting/) +- [Script exit status](https://arkit.co.in/shell-scripting-exit-status-shell-scripting-return-codes/) +- [Variables and it's rules](https://www.youtube.com/watch?v=839s_OtTqDA) +- [Special Variables](https://www.youtube.com/watch?v=PfxzX4XNYRE) +- [Environment Variables, system variables and user defined variables](https://www.youtube.com/watch?v=PfxzX4XNYRE) +- [Constant variables, Local & Global variables and Special variables](https://www.youtube.com/watch?v=839s_OtTqDA) +- [Positional Parameters](https://www.youtube.com/watch?v=PfxzX4XNYRE) +- [Count number command line arguments $#](https://www.youtube.com/watch?v=YizjrX9ph10) +- [Arithmetic Operators](https://www.youtube.com/watch?v=qxNQ_D8txPo) +- [Relational Operators](https://www.youtube.com/watch?v=U-u1wx5VeTU) +- [LogicalOperators](https://www.youtube.com/watch?v=m_F1FTKdUU4) +- [Boolean Operators](https://www.youtube.com/watch?v=U-u1wx5VeTU) +- [Maths using expr command](https://www.youtube.com/watch?v=qxNQ_D8txPo) +- [Real maths using bc command](https://www.youtube.com/watch?v=qxNQ_D8txPo) +- [if statement](https://www.youtube.com/watch?v=gncu9vzmILw) +- [if-else statement](https://www.youtube.com/watch?v=nDhbOeEQeNY) +- [if-else-if statement](https://www.youtube.com/watch?v=UJET-9cmaqU) +- [Nested if statement](https://www.youtube.com/watch?v=Kd1SJFnmj9k) +- [Case statement](https://www.youtube.com/watch?v=JJ7mAPU0KhI) +- [For Loop](https://www.youtube.com/watch?v=1fnAUUS4qg0) +- [While Loop](https://www.youtube.com/watch?v=nBMuVIRGpwY) +- [Until Loop](https://www.youtube.com/watch?v=zdk795qFgWk) +- [Functions](https://www.youtube.com/watch?v=jXv1otUXMG4) +- [Arrays](https://www.youtube.com/watch?v=2Fetj2V6rrM) +- [Eval command](https://www.youtube.com/watch?v=AjqBRGwLmLc&list=PL8cE5Nxf6M6b8qW7CSMsdKbEsPdG9pWfu&index=57&t=0s) +- [Shifting parameters using shift command](https://www.youtube.com/watch?v=48j0kxOFKZE) +- [IFS - Input Field Separator](https://www.youtube.com/watch?v=so8IRuhWjEM) +- [Writing CPU Usage script](https://www.youtube.com/watch?v=NQx43bY4lNo) +- [Writing Disk Utilization script](https://www.youtube.com/watch?v=yXhdDV13nrA) +- [Trouble shooting debugging shell scripts](https://www.youtube.com/watch?v=kgj-4_gmvi4) +- [Checking shell script errors and improvements using shellcheck.net site](https://www.youtube.com/watch?v=kgj-4_gmvi4) +- [Here Document to write paragraphs of text](https://www.youtube.com/watch?v=r9lb0ZxGFqE) +- [Getopts Function](https://www.youtube.com/watch?v=j-lEoC0DWI8) +- [Executing Multiple scripts from single script](https://youtu.be/hs-FK681D50) +- [logger logging messages to log file](https://youtu.be/_kMXvtn1RRQ) + +**Resource to Download** + +- [Shell Scripting Book](https://arkit-in.tradepub.com/free/w_wile48/) +- [Shell Scripting Book](https://arkit-in.tradepub.com/free/w_pack42/) +- [Shell Scripting Book](https://arkit-in.tradepub.com/free/w_advb01/) +- [Shell Scripting Book](https://arkit-in.tradepub.com/free/w_wile54/) + +**Write Your Own Method of Script for below Scenario** + +- **Scenario:** Everyday from Monday to Friday one directory will be created under /fullbackup/dailybackup/YYYY-MM-DD and it will move backup to its parent directory everyday midnight /fullbackup/archive/, However Saturday, Sunday and Monday directories will move to /fullbackup/archive path every monday evening. + +- **Directory Names Example:** 2018-12-24 2018-12-25 2018-12-26 2018-12-27 2018-12-28 + +- **Question:** I would like to delete directories older than two days from /fullbackup/archive path. How do you do it using any scripting methods. + +- **Problem Statement:** I was trying to use ```find /path/ -type d -mtime +2 -print0 | xargs -r0 rm --```. This command does not work as expected due to directory modified date for SAT, SUN and MON moved directories same for all as Monday date. + +- **How Do you solve it.??** Write Shell Script to accomplish this task. Should run through crontab and clear directories older than two days. diff --git a/add.sh b/add.sh new file mode 100644 index 0000000..8909fed --- /dev/null +++ b/add.sh @@ -0,0 +1,19 @@ +#!/bin/bash +################################################## +# Purpose: eval command Evaluating twice +# Version:1.0 +# Created Date: Wed Jun 13 22:09:59 IST 2018 +# Modified Date: +# WebSite: https://arkit.co.in +# Author: Ankam Ravi Kumar +################################################## +# START # + +echo "addition of X+Y" +echo "Enter X" +read X +echo "Enter Y" +read Y +echo "X+Y = $X+$Y = $[ X+Y ]" + +# END # diff --git a/agtb.sh b/agtb.sh new file mode 100644 index 0000000..0a67093 --- /dev/null +++ b/agtb.sh @@ -0,0 +1,20 @@ +#!/bin/bash +#Purpose: eval command Evaluating twice +#Version:1.0 +#Created Date: Wed Jun 13 22:09:59 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # + +echo -e "enter the a value: \c" +read a +echo -e "enter the b value: \c" +read b +if test "$a" -gt "$b" ; then + echo "$a is greater than $b" +else + echo "$b is greater than $a" +fi + +# END # \ No newline at end of file diff --git a/answers.sh b/answers.sh new file mode 100644 index 0000000..697f19b --- /dev/null +++ b/answers.sh @@ -0,0 +1,15 @@ +#!/usr/bin/expect + +set timeout -1 + +spawn ./questions.sh +expect "Hi\r" +send -- "Hi\r" + +expect "How are you?\r" +send -- "I am fine\r" + +expect "Whats your Name?\r" +send -- "My name is Ravi\r" + +expect eof diff --git a/array.sh b/array.sh new file mode 100644 index 0000000..7c61603 --- /dev/null +++ b/array.sh @@ -0,0 +1,20 @@ +#!/bin/bash +#Purpose: Array Example +#Version:1.0 +#Created Date: Mon May 28 22:59:22 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # + +fruits=( "Apple" "Orange" "Banana" "Sapota" ) +fruits[3]='Green Apple' +for fruit in ${fruits[@]} +do + echo "Fruit Name is $fruit" +done + +echo "Number of Fruits in Bucket is" ${#fruits[@]} +echo "All Fruits ${fruits[@]}" + +# END # diff --git a/casestatement.sh b/casestatement.sh new file mode 100644 index 0000000..e68b229 --- /dev/null +++ b/casestatement.sh @@ -0,0 +1,29 @@ +#!/bin/bash +#Purpose: Example for Case Statement +#Version:1.0 +#WebSite: https://arkit.co.in +#Created Date: Mon May 21 20:37:59 IST 2018 +#Modified Date: +#Author: Ankam Ravi Kumar +# START # +echo -e "Enter a number: \c" +read -r a +echo -e "Enter b number: \c" +read -r b + +echo "1. Sum of values" +echo "2. Substraction" +echo "3. Multiplication" +echo "4. Division" +echo "5. Modulo division" +echo -e "Enter Your Choice from above menu: \c" +read -r ch +case $ch in +1) echo "Sum of $a + $b = "`expr $a + $b`;; +2) echo "Subsctraction = "`expr $a - $b`;; +3) echo "Multiplication = "`expr $a \* $b`;; +4) echo "Division = "`expr $a / $b`;; +5) echo "Modulo Division = "`expr $a % $b`;; +*) echo "Invalid Option provided" +esac +# END # diff --git a/collectroothistory.sh b/collectroothistory.sh new file mode 100644 index 0000000..6e0c9e1 --- /dev/null +++ b/collectroothistory.sh @@ -0,0 +1,26 @@ +#!/bin/bash +## Collect Root Commands History + +# Mailing List +MAILLIST="YOUREMAIL@DOMAIN" + +# Log path +AUDLOG="/rootcommands" + +cp /root/.bash_history /tmp/history +sed -i 's/#//g' /tmp/history +for i in `cat /tmp/history |grep ^[0-9]` +do +CONVT=`date -d @$i` +sed -i "s/$i/$CONVT/g" /tmp/history +done + +sed -i 'N;s/\n/ /' /tmp/history +sleep 10 + +/bin/touch ${AUDLOG}$HOSTNAME-root-hist.log.`date +%h%d%y` +/bin/grep "$DATE" /tmp/history > ${AUDLOG}$HOSTNAME-root-hist.log.`date +%h%d%y` +/bin/chmod 0440 ${AUDLOG}$HOSTNAME-root-hist.log.`date +%h%d%y` + +# Mail notification +/bin/cat ${AUDLOG}$HOSTNAME-root-hist.log.`date +%h%d%y` |mail -s "HOST: $HOSTNAME - `whoami` Daily root Commands Log" ${MAILLIST} diff --git a/continue.sh b/continue.sh new file mode 100644 index 0000000..ee5694e --- /dev/null +++ b/continue.sh @@ -0,0 +1,31 @@ +#!/bin/bash +#Purpose: While loop Continue Statement +#Version:1.0 +#Website: https://arkit.co.in +#Created Date: Tue May 22 22:03:02 IST 2018 +#Modified Date: +#Author: Ankam Ravi Kumar +# START # +opt=y +while [ $opt = y -o $opt = Y ] +do +echo -e "Please enter the number: \c" +read -r num +if [ $num -le 50 ]; then +sq=`expr $num \* $num` +echo "Square of provided number $num: $sq" +else +echo "Number not in the given Range" +fi + +echo -e "Do you want to continue [y/n]: \c" +read -r wish +if [ $wish = y -o $wish = Y ]; then +continue +else +echo "Thank You for Exiting.." +exit +fi +done + +# END # diff --git a/convert_and_update_mysql.sh b/convert_and_update_mysql.sh new file mode 100644 index 0000000..98f4d25 --- /dev/null +++ b/convert_and_update_mysql.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +n=1 +until [ $n -gt 11196 ] +do +EXIRECORDS=$(mysql -u root -pPASSWORD -h 192.168.2.100 -e "SELECT sn,tarsize from DATABASE.Table1 where sn='"$n"'" |tail -n1 |grep $n) + +RECORDNUMBER=$(echo $EXIRECORDS |awk '{print $1}') +FOLDERSIZE=$(echo $EXIRECORDS |awk '{print $2}') + +KB=$(echo $EXIRECORDS |awk '{print $2}' |grep K |wc -l) + if [ $KB -ge 1 ]; then + K=$(echo $EXIRECORDS |awk '{print $2}' |sed 's/K//g') + BYTES=$($K * 1024 |bc |awk -F. '{print $1}') + mysql -u root -pPASSWORD -h 192.168.2.100 -e "UPDATE DATABASE.Table1 SET tarsize='"$BYTES"' where sn='"$RECORDNUMBER"'" + fi + +MB=$(echo $EXIRECORDS |awk '{print $2}' |grep M |wc -l) + if [ $MB -ge 1 ]; then + M=$(echo $EXIRECORDS |awk '{print $2}' |sed 's/M//g') + BYTES=$(echo $M*1024*1024 |bc |awk -F. '{print $1}') + mysql -u root -pPASSWORD -h 192.168.2.100 -e "UPDATE DATABASE.Table1 SET tarsize='"$BYTES"' where sn='"$RECORDNUMBER"'" + fi + +GB=$(echo $EXIRECORDS |awk '{print $2}' |grep G |wc -l) + if [ $GB -ge 1 ]; then + G=$(echo $EXIRECORDS |awk '{print $2}' |sed 's/G//g') + BYTES=$(echo $G*1024*1024*1024 |bc |awk -F. '{print $1}') + mysql -u root -pPASSWORD -h 192.168.2.100 -e "UPDATE DATABASE.Table1 SET tarsize='"$BYTES"' where sn='"$RECORDNUMBER"'" + fi + +TB=$(echo $EXIRECORDS |awk '{print $2}' |grep T |wc -l) + if [ $TB -ge 1 ]; then + T=$(echo $EXIRECORDS |awk '{print $2}' |sed 's/T//g') + BYTES=$(echo $T*1024*1024*1024*1024 |bc |awk -F. '{print $1}') + mysql -u root -pPASSWORD -h 192.168.2.100 -e "UPDATE DATABASE.Table1 SET tarsize='"$BYTES"' where sn='"$RECORDNUMBER"'" + fi + + +n=`expr "$n" + 1` + +done diff --git a/countargs.sh b/countargs.sh index 8b63dc5..e25e305 100644 --- a/countargs.sh +++ b/countargs.sh @@ -1,15 +1,17 @@ #!/bin/bash -#Purpose: Counting given postional parameters. -#Version:1.0 -#Created Date: Mon May 7 21:55:05 IST 2018 -#Modified Date: -#Author: Ankam Ravi Kumar +################################################## +# Purpose: Counting given postional parameters. +# Version:1.0 +# Created Date: Mon May 7 21:55:05 IST 2018 +# Modified Date: +# Author: Ankam Ravi Kumar +################################################## + # START # -#echo "Your current given parameters are $#" +echo "Your current given parameters are $#" if [ $# -lt 1 ];then echo "Program Usage is './scriptname.sh' options" else echo "Program executed successfully" fi - # END # diff --git a/cpualert.sh b/cpualert.sh new file mode 100644 index 0000000..a5c6b13 --- /dev/null +++ b/cpualert.sh @@ -0,0 +1,39 @@ +#!/bin/bash +#Purpose: Real time CPU utilization Monitoring +#Version:1.0 +#Created Date: Tue Jun 5 21:33:38 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # +PATHS="/" +HOSTNAME=$(hostname) +CRITICAL=98 +WARNING=90 +CRITICALMail="YOUREMAILaddresS@Domain.com" +MAILWAR="YOUREMAIL@Domain.in" +mkdir -p /var/log/cputilhist +LOGFILE=/var/log/cputilhist/cpusage-`date +%h%d%y`.log + +touch $LOGFILE + +for path in $PATHS +do + CPULOAD=`top -b -n 2 -d1 | grep "Cpu(s)" | tail -n1 | awk '{print $2}' |awk -F. '{print $1}'` +if [ -n $WARNING -a -n $CRITICAL ]; then +if [ "$CPULOAD" -ge "$WARNING" -a "$CPULOAD" -lt "$CRITICAL" ]; then +echo "`date "+%F %H:%M:%S"` WARNING - $CPULOAD on Host $HOSTNAME" >> $LOGFILE +echo "Warning Cpuload $CPULOAD Host is $HOSTNAME" | mail -s "CPULOAD is Warning" $MAILWAR +exit 1 +elif [ "$CPULOAD" -ge "$CRITICAL" ]; then +echo "`date "+%F %H:%M:%S"` CRITICAL - $CPULOAD on Host $HOSTNAME" >> $LOGFILE +echo "CRITICAL Cpuload $CPULOAD Host is $HOSTNAME" | mail -s "CPULOAD is CRITICAL" $CRITICALMail +exit 2 +else +echo "`date "+%F %H:%M:%S"` OK - $CPULOAD on $HOSTNAME" >> $LOGFILE +exit 0 +fi +fi +done + +# END # diff --git a/details.sh b/details.sh new file mode 100644 index 0000000..cf701a3 --- /dev/null +++ b/details.sh @@ -0,0 +1,15 @@ +#!/bin/bash +#Purpose: eval command Evaluating twice +#Version:1.0 +#Created Date: Wed Jun 13 22:09:59 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # + +echo "WEL COME TO $USERNAME" +echo "Your present working directory is `pwd`" +echo "current logged in users are `who`" +echo "Today date is `date`" + +# END # diff --git a/diskover-2.1.1.zip b/diskover-2.1.1.zip new file mode 100644 index 0000000..8bf687f Binary files /dev/null and b/diskover-2.1.1.zip differ diff --git a/diskspace.sh b/diskspace.sh new file mode 100644 index 0000000..f1de88f --- /dev/null +++ b/diskspace.sh @@ -0,0 +1,28 @@ +#!/bin/bash +#Purpose: Monitoring Disk Space Utilization and Send Email Alert +#Version:1.0 +#Created Date: Wed Jun 6 22:38:01 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # +THRESHOULD=40 +mailto="root" +HOSTNAME=$(hostname) + +for path in `/bin/df -h | grep -vE 'Filesystem|tmpfs' | awk '{print $5}' |sed 's/%//g'` +do + if [ $path -ge $THRESHOULD ]; then + df -h | grep $path% >> /tmp/temp + fi +done + +VALUE=`cat /tmp/temp | wc -l` + if [ $VALUE -ge 1 ]; then + mail -s "$HOSTNAME Disk Usage is Critical" $mailto < /tmp/temp + fi + +#rm -rf /tmp/temp + + +# END # diff --git a/echo.sh b/echo.sh new file mode 100644 index 0000000..f8e8f8c --- /dev/null +++ b/echo.sh @@ -0,0 +1,13 @@ +#!/bin/bash +#Purpose: eval command Evaluating twice +#Version:1.0 +#Created Date: Wed Jun 13 22:09:59 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # + +echo "current location files are `ls`" +echo "current working directory is `pwd`" + +# END # diff --git a/eval.sh b/eval.sh new file mode 100644 index 0000000..1d9bcf1 --- /dev/null +++ b/eval.sh @@ -0,0 +1,12 @@ +#!/bin/bash +#Purpose: eval command Evaluating twice +#Version:1.0 +#Created Date: Wed Jun 13 22:09:59 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # +COMMAND="ls -ltr /etc" +echo "$COMMAND" +eval $COMMAND +# END # diff --git a/extract.sh b/extract.sh new file mode 100644 index 0000000..f4378dc --- /dev/null +++ b/extract.sh @@ -0,0 +1,30 @@ +for C in -aes-256-ctr -aes-256-cbc +do + echo "===== CIPHER $C =====" + for o in \ + "-md md5 -kfile /secure/pwd.txt" \ + "-md sha256 -kfile /secure/pwd.txt" \ + "-pbkdf2 -pass file:/secure/pwd.txt" \ + "-pbkdf2 -md sha256 -pass file:/secure/pwd.txt" + do + echo "=== Trying: $C $o" + openssl enc -d $C $o -bufsize 8388608 \ + -in TARFILENAME.tar -out dec.gz 2>/dev/null + head -c 2 dec.gz | xxd + file dec.gz | head -n 1 + done +done + + +for o in \ + "-md md5 -kfile /secure/pwd.txt" \ + "-md sha256 -kfile /secure/pwd.txt" \ + "-pbkdf2 -pass file:/secure/pwd.txt" \ + "-pbkdf2 -md sha256 -pass file:/secure/pwd.txt" +do + echo "=== Trying: $o" + openssl enc -d -aes-256-ctr $o -bufsize 8388608 \ + -in TARFILENAME.tar -out /tmp/dec.gz 2>/dev/null + head -c 2 /tmp/dec.gz | xxd + file /tmp/dec.gz | head -n 1 +done diff --git a/for-loop.sh b/for-loop.sh index d399c5f..d431716 100644 --- a/for-loop.sh +++ b/for-loop.sh @@ -6,14 +6,14 @@ #Modified Date: #Author: Ankam Ravi Kumar # START # -for i in `cat hostfile` +for server in `cat /scripts/servers` do -ping -c 1 $i > /tmp/pingresults +ping -c 1 $server > /tmp/ping valid=`echo $?` -if [ $valid -gt 1 ]; then -echo "$i Host is not reachable" +if [ $valid -eq 0 ]; then +echo "$server is up" else -echo "$i Host is Up" +echo "$server is Down" fi done # END # diff --git a/1forloop.sh b/forloop.sh similarity index 100% rename from 1forloop.sh rename to forloop.sh diff --git a/function.sh b/function.sh new file mode 100644 index 0000000..48dbf17 --- /dev/null +++ b/function.sh @@ -0,0 +1,27 @@ +#!/bin/bash +#Purpose: Function example. Taking Backup of Particular File +#Version:1.0 +#Created Date: 2024 Sep 21 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar + +# START # +function takebackup (){ + if [ -f $1 ]; then + BACKUP="/home/aravi/$(basename ${1}).$(date +%F).$$" + echo "Backing up $1 to ${BACKUP}" + cp $1 $BACKUP + fi +} + +takebackup /etc/hosts + if [ $? -eq 0 ]; then + echo "BAckup Success" + fi +function testing (){ +echo "Just TEsting Function" +} + +testing +# END # diff --git a/functions.sh b/functions.sh new file mode 100644 index 0000000..61e1a95 --- /dev/null +++ b/functions.sh @@ -0,0 +1,21 @@ +#!/bin/bash +#Purpose: Example for Functions +#Version:1.0 +#Created Date: Sat May 26 00:17:19 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # +function takebackup (){ + if [ -f $1 ]; then + BACKUP="/tmp/$(basename ${1}).$(date +%F).$$" + echo "Backing up $1 to ${BACKUP}" + cp $1 $BACKUP + fi +} + +takebackup /etc/hosts + if [ $? -eq 0 ]; then + echo "Backup Success" +fi +# END # diff --git a/generate_win_host_config.sh b/generate_win_host_config.sh new file mode 100644 index 0000000..cc6259e --- /dev/null +++ b/generate_win_host_config.sh @@ -0,0 +1,24 @@ +#!/bin/bash +## Purpose: To Generate Nagios Configuration files within minute of time. +## Author: Ankam Ravi Kumar + +mkdir -p /scripts/WinServers +cat /scripts/serverlist.txt | while read LINE +do + HostIP=`echo $LINE | cut -d, -f1` + HostName=`echo $LINE | cut -d, -f2` + +NSCLIENTSTATE=$(/usr/local/nagios/libexec/check_nt -H $HostIP -p 12489 -v CLIENTVERSION -s Password | echo $?) +if [ $NSCLIENTSTATE -eq 0 ]; then + sed -e "s/XXXX/$HostName/g; s/ZZZZ/$HostIP/g" /scripts/Template-Windows.cfg > /scripts/WinServers/$HostName.cfg + +for i in D E F G H I J K L M N O P Q R S T U V W X Y Z; +do +/usr/local/nagios/libexec/check_nt -H $HostIP -p 12489 -v USEDDISKSPACE -s Password -l $i -w 90 -c 95 +COMMANDSTATUS=$(echo $?) +if [ $COMMANDSTATUS -eq 0 ] || [ $COMMANDSTATUS -eq 2 ];then +sed -e "s/XXXX/$HostName/g; s/ZZZZ/$i/g" /scripts/Drives.cfg >> /scripts/WinServers/$HostName.cfg +fi +done +fi +done diff --git a/getopts.sh b/getopts.sh new file mode 100644 index 0000000..4028ba9 --- /dev/null +++ b/getopts.sh @@ -0,0 +1,20 @@ +#!/bin/bash +#Purpose: Getopts Examples working with arguments +#Version:1.0 +#Created Date: Wed May 30 22:30:51 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # + +while getopts :a:b: options; do + case $options in + a) ap=$OPTARG;; + b) bo=$OPTARG;; + ?) echo "I Dont know What is $OPTARG is" + esac +done + +echo "Option A = $ap and Option B = $bo" + +# END # diff --git a/heredoc.sh b/heredoc.sh new file mode 100644 index 0000000..2f9a966 --- /dev/null +++ b/heredoc.sh @@ -0,0 +1,17 @@ +#!/bin/bash +#Purpose: Here Document Example +#Version:1.0 +#Created Date: Tue Jun 12 22:50:23 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # +ftp -n <<- EOF 2> /dev/null + open ftp.server.com + user ftp ftp + ascii + cd uploadfolder + mput file1 file1 file2 + bye +EOF + diff --git a/hi.sh b/hi.sh new file mode 100644 index 0000000..6652c64 --- /dev/null +++ b/hi.sh @@ -0,0 +1,22 @@ +#!/bin/bash +#Purpose: eval command Evaluating twice +#Version:1.0 +#Created Date: Wed Jun 13 22:09:59 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # + +tmp=`date | cut -c12-13` +if [ $tmp -lt 11 ] ; then +echo "Good Mornind have a nice day $USERNAME" +elif [ $tmp -gt 11 -a $tmp -lt 16 ] ; then +echo "Good Ofter noon $USERNAME" +elif [ $tmp -gt 15 -a $tmp -lt 19 ] ; then +echo "Good Evening $USERNAME" +else +echo "Good Night Sweet dreams $USERNAME" +fi +echo "Now the time is `date |cut -c12-19`" + +# END # diff --git a/if-elif-if.sh b/if-elif-if.sh index ee2e09b..da88e28 100644 --- a/if-elif-if.sh +++ b/if-elif-if.sh @@ -5,6 +5,7 @@ #Modified Date: #Author: Ankam Ravi Kumar # START # + echo -e "Please Enter a Value: \c" read -r a echo -e "Please Enter b Value: \c" diff --git a/if-else-statement.sh b/if-else-statement.sh index 1393446..7e5b04f 100644 --- a/if-else-statement.sh +++ b/if-else-statement.sh @@ -6,7 +6,7 @@ #Website: https://arkit.co.in #Author: Ankam Ravi Kumar # START # -echo -e "Enter any value> \c" +echo -e "Enter any value: \c" read -r a echo -e "Enter any value: \c" read -r b diff --git a/ifs.sh b/ifs.sh new file mode 100644 index 0000000..9663330 --- /dev/null +++ b/ifs.sh @@ -0,0 +1,20 @@ +#!/bin/bash +#Purpose: Internal Field Seperator +#Version:1.0 +#Created Date: Wed Jun 13 21:58:18 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # +LINE=`cat /etc/passwd |grep $1` +IFS=: +set $LINE +echo "User Name = $1" +echo "Password = $2" +echo "UID = $3" +echo "GID = $4" +echo "Description = $5" +echo "Home Directory = $6 " +echo " Current Shell = $7" + +# END # diff --git a/info.sh b/info.sh new file mode 100644 index 0000000..caac61a --- /dev/null +++ b/info.sh @@ -0,0 +1,17 @@ +#!/bin/bash +#Purpose: eval command Evaluating twice +#Version:1.0 +#Created Date: Wed Jun 13 22:09:59 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # + +echo "Hi you there" +echo "what is your name? (Type your name here and press Enter)" +read NM +echo "Hi $NM Good Morning" +echo "your currently logged in as $USERNAME" +echo "your present working directory is `pwd`" + +# END # diff --git a/memusage.sh b/memusage.sh new file mode 100644 index 0000000..45187ca --- /dev/null +++ b/memusage.sh @@ -0,0 +1,18 @@ +#!/bin/bash +## Monitoring Memory usage of the server +# Version:1.0 +# Created Date: 2022-Jan-07 +# WebSite: https://arkit.co.in +# Author: Ankam Ravi Kumar + +HOSTNAME=$(hostname) +DATED=$(date "+%Y-%m-%d %H:%M:%S") +THRESHOLD=80 +TOADDRESS=aravikumar48@gmail.com + +MEMUSAGE=$(free | grep Mem | awk '{print $3/$2 * 100.0}' |awk -F. '{print $1}') +if [ $MEMUSAGE -ge $THRESHOLD ]; then +echo "$HOSTNAME, $DATED, %MEMUSAGE" >> /var/log/memusage_history +echo "$HOSTNAME, $DATED, %MEMUSAGE" > /tmp/memusage +mail -s "$HOSTNAME $DATED Mem Usage: $MEMUSAGE" $TOADDRESS <<< /tmp/memusage +fi diff --git a/morethanxdays.sh b/morethanxdays.sh new file mode 100644 index 0000000..2a89a07 --- /dev/null +++ b/morethanxdays.sh @@ -0,0 +1,14 @@ +#!/bin/bash +## Delete the Directories older than 2 days based on directory name validation +## Refer YouTube Link for Explanation https://youtu.be/1Sh6PWcgXAA +ls -ltr /fullbackup/archive/ | awk '{print $9}' > /scripts/dirs +for i in `cat /scripts/dirs`; do +STARTTIME=$(date +%s -d"$i 00:00:00") +ENDTIME=$(date +%s) +echo $((ENDTIME-STARTTIME)) | awk '{print int($1/60)}' > /scripts/value +COUNT=`cat /scripts/value` +if [ $COUNT -gt 2880 ]; then +echo "Directories are older than 2days $i" >> /scripts/joblog +rm -rf /fullbackup/archive/$i +fi +done diff --git a/multiplication.sh b/multiplication.sh new file mode 100644 index 0000000..b135a70 --- /dev/null +++ b/multiplication.sh @@ -0,0 +1,17 @@ +#!/bin/bash +#Purpose: eval command Evaluating twice +#Version:1.0 +#Created Date: Wed Jun 13 22:09:59 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # + +echo "multification of X*Y" +echo "Enter X" +read X +echo "Enter Y" +read Y +echo "X*Y = $X*$Y = $[ X*Y ]" + +# END # \ No newline at end of file diff --git a/myfirstscript.sh b/myfirstscript.sh index ad53bc9..2c39c40 100644 --- a/myfirstscript.sh +++ b/myfirstscript.sh @@ -7,7 +7,7 @@ #Modified by: # START -echo "Welcome $USER" +echo "Welcome $USERNAME" echo "Your present working directory is `pwd`" echo "Today date is `date`" # END diff --git a/arkit.co.in-nested-if.sh b/nestedif.sh similarity index 99% rename from arkit.co.in-nested-if.sh rename to nestedif.sh index 5be4b8d..17d421b 100644 --- a/arkit.co.in-nested-if.sh +++ b/nestedif.sh @@ -5,6 +5,7 @@ #Modified Date: #Author: Ankam Ravi Kumar # START # + echo -e "Please Enter Maths Marks: \c" read -r m echo -e "Please Enter Physics Marks: \c" diff --git a/or-operator.sh b/or-operator.sh index 5a66bd7..2931df0 100644 --- a/or-operator.sh +++ b/or-operator.sh @@ -5,7 +5,8 @@ #Modified Date: #Author: Ankam Ravi Kumar # START # -echo -e "Enter First Numberic Value: \c" + +echo -e "Enter First Numeric Value: \c" read -r t echo -e "Enter Second Numeric Value: \c" read -r b @@ -13,7 +14,7 @@ read -r b if [ $t -le 20 -o $b -ge 30 ]; then echo "Statement is True" else -echo "Flase, Statement Try Again." +echo "False Statement, Try Again." fi # END # diff --git a/questions.sh b/questions.sh new file mode 100644 index 0000000..c93a791 --- /dev/null +++ b/questions.sh @@ -0,0 +1,11 @@ +#!/bin/bash +## Questions + +echo "Hi" +read $REPLY + +echo "How are you?" +read $REPLY + +echo "Whats your Name?" +read $REPLY diff --git a/regex.sh b/regex.sh new file mode 100755 index 0000000..376bb77 --- /dev/null +++ b/regex.sh @@ -0,0 +1,40 @@ +#!/bin/bash +#Purpose: regex examples +#Version: 1.0 +#Create Date: Sun Nov 27 00:27:33 EST 2022 +#Modified Date: + +# START # + +numString1="1234" +numString2="16789" +numString3="1579" + + +echo "Example 1" +if [[ $numString1 =~ ^1 ]]; then + echo "String \"$numString1\" starts with a \"1\", and matches regex: ^1" +fi + +echo "Example 2" +if [[ $numString2 =~ ^1 ]]; then + echo "String \"$numString2\" starts with a \"1\", and matches regex: ^1" +fi + +echo "Example 3" +if [[ $numString3 =~ ^1.7 ]]; then + echo "String \"$numString2\" starts with a \"1\", followed by any character, and followed by a 7. " + echo "This string matches the regex: ^1.7" +fi + +echo "Example 4" +if [[ ! $numString1 =~ ^1.7 ]]; then + echo "String \"$numString1\" does not start with a \"1\", followed by any character, and followed by a 7. " + echo "This string does not match the regex: ^1.7" +fi + +echo "Example 5" +if [[ $numString2 =~ 9$ ]]; then + echo "String \"$numString2\" ends with a \"9\", and matches the regex: 9$" +fi + diff --git a/remoteload.sh b/remoteload.sh new file mode 100644 index 0000000..83da24b --- /dev/null +++ b/remoteload.sh @@ -0,0 +1,20 @@ +#!/bin/bash +################################################## +# # +# Author: Ankam Ravi Kumar # +# Website: server-computer.com # +# Date: 23-02-2019 16:59:56 # +# Purpose: Capture and Store System Load Average # +# CPU Usage and Memory Usage # +################################################## +# Log File Path +LOGFILE=/var/log/systemload.log + +echo "" > /tmp/remotelog + +for i in `cat /opt/hostnames`; +do +cat /root/systemload.sh | ssh $i >> /tmp/remotelog +done + +cat /tmp/remotelog |grep -vE "^Last|^There" >> $LOGFILE diff --git a/serverinformation.sh b/serverinformation.sh new file mode 100644 index 0000000..85d4d14 --- /dev/null +++ b/serverinformation.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +mkdir -p /Inventory +sudo chmod -R 777 /Inventory/ +mkdir -p /tmp/asset +touch /tmp/asset/hostname.txt +if [ -f /tmp/asset/hostname.txt ];then + echo "File /tmp/asset/hostname.txt Exists" +else + mkdir /tmp/asset + touch /tmp/asset/hostname.txt +fi +touch /tmp/temptext +TEMP=/tmp/temptext +LOG=`ls /tmp/asset/hostname.txt` +echo "## Host Information" > $LOG +echo "Host Name : `hostname` " >> $LOG +echo "`sudo /sbin/ifconfig -a |grep "inet" | awk 'BEGIN { FS = ":" } ; { print $2 }'`" >> $TEMP +echo "IP Address : `egrep '^10' $TEMP |awk '{ print $1}'`" >> $LOG +echo "IP Address: `hostname -I`" >> $LOG +echo "ip a |grep inet |grep -v "::" |awk '{print $2}'" >> $LOG +echo "iDrac Details: `racadm getniccfg |grep "IP Address" |grep -v "::"`" >> $LOG +echo "Server Type: `servertype=$(lscpu | grep Hypervisor | wc -l); if [ $servertype -gt 0 ]; then echo "VitualMachine"; else echo "Physical"; fi`" >> $LOG +echo -en '\n' >> $LOG + +## Collecting Hardware Details ## +echo " " >> $LOG +echo "## Hardware Information" >> $LOG +echo " " >> $LOG +echo "Serial Number : `sudo lshal |grep system.hardware.serial`" >> $LOG +echo "Serial Number : `sudo /usr/sbin/dmidecode -s system-serial-number`" >> $LOG +echo "Serial Number : `sudo cat /sys/class/dmi/id/product_serial`" >> $LOG +echo "Model Number : `sudo lshal |grep system.hardware.product`" >> $LOG +echo "Model Number : `sudo /usr/sbin/dmidecode |grep "SKU Number"`" >> $LOG +echo "Model Number : `sudo cat /sys/class/dmi/id/product_name`" >> $LOG +echo "Hardware Vendor : `sudo lshal |grep system.hardware.vendor`" >> $LOG +echo "Hardware Vendor : `sudo cat /sys/class/dmi/id/chassis_vendor`" >> $LOG +echo "Hardware Info : `sudo dmesg |grep DMI`" >> $LOG + +## Redhat Version ## +echo " " >> $LOG +echo "## OS Version" >> $LOG +head -n1 /etc/issue >> $LOG +cat /etc/redhat-release >> $LOG +echo "Kernel Version: `uname -r`">> $LOG +echo "OS Version: `hostnamectl | egrep "Operating System" | cut -d ' ' -f5-`" >> $LOG + +## CPU Info ## +echo " " >> $LOG +echo " " >> $LOG +echo "## CPU Information" >> $LOG +grep "model name" /proc/cpuinfo |uniq >> $LOG +COUNTT=$(cat /proc/cpuinfo |grep "model name" | wc -l) +echo "$COUNTT Cores" >> $LOG + +## RAM/MEMORY Info ## +echo " " >> $LOG +echo " " >> $LOG +echo "## Memory Information" >> $LOG +grep MemTotal /proc/meminfo >> $LOG +y=`grep MemTotal /proc/meminfo |awk '{ print $2 }'` +mb="$(( $y / 1024 ))" +gb="$(( $mb / 1024 ))" +echo "RAM : $gb GB" >> $LOG + +## Swap Information ## +echo " " >> $LOG +echo "## Swap Information" >> $LOG +y1=$(free -k |grep Swap |awk '{print $2}') +mb1="$(( $y1 / 1024 ))" +gb1="$(( $mb1 / 1024 ))" +echo "Swap Size: $gb1 GB" >> $LOG + +## Disk Information ## +echo " " >> $LOG +echo "## Disk Information" >> $LOG +lsblk |grep -E 'part|disk' $LOG + +## LVM Information ## +echo " " >> $LOG +echo "## Physical Volumes" >> $LOG +pvs >> $LOG + +echo " " >> $LOG +echo "## Volume Groups" >> $LOG +vgs >> $LOG + +echo " " >> $LOG +echo "## Logical Volumes" >> $LOG +lvs >> $LOG +echo " " >> $LOG + +## Partition Information ## +echo "## DF Command Output" >> $LOG +echo " " >> $LOG +df -Ph -x tmpfs -x devtmpfs| sed s/%//g | awk '{ if($5 > 0) print $0;}' >> $LOG + +echo " " >> $LOG +echo "## Port Information" >> $LOG +ss -alntup |column -t |grep -E 'tcp|udp' >> $LOG + +echo " " >> $LOG +echo "## Service Information" >> $LOG +systemctl list-units --type=service --state=running |grep -vE 'systemd|selinux' >> $LOG + +echo " " >> $LOG +echo "## Docker Containers" >> $LOG +sudo docker ps -a >> $LOG + +echo " " >> $LOG +echo "## DNS Server Details" >> $LOG +cat /etc/resolv.conf >> $LOG + +echo "" >> $LOG +echo "## Server Uptime" >> $LOG +uptime >> $LOG + +sudo cp /tmp/asset/`hostname`.txt /Inventory/`hostname`-`date "+%Y-%m-%d"`.txt diff --git a/setbashfeature.sh b/setbashfeature.sh new file mode 100644 index 0000000..c918db2 --- /dev/null +++ b/setbashfeature.sh @@ -0,0 +1,17 @@ +#!/bin/bash +#Purpose: Set assigns its arguments to the positional parameters +#Version:1.0 +#website: https://arkit.co.in +#Created Date: Tue May 22 23:10:17 IST 2018 +#Modified Date: +#Author: Ankam Ravi Kumar +# START # +set `date` +echo "Today is $1" +echo "Month is $2" +echo "Date is $3" +echo "Time H:M:S $4" +echo "TimeZone is $5" +echo "Year is $6" +set -x +# END # diff --git a/shiftparameters.sh b/shiftparameters.sh new file mode 100644 index 0000000..5a825da --- /dev/null +++ b/shiftparameters.sh @@ -0,0 +1,14 @@ +#!/bin/bash +#Purpose: Shifting positional parameters automatically +#Version:1.0 +#Website: https://arkit.co.in +#Created Date: Tue May 22 22:55:50 IST 2018 +#Modified Date: +#Author: Ankam Ravi Kumar +# START # +set `date` +echo "Count $#" +echo "$1 $2 $3 $4 $5" +shift 2 +echo "$1 $2 $3 $4 $5" +# END # diff --git a/systemload.sh b/systemload.sh new file mode 100644 index 0000000..3f7d663 --- /dev/null +++ b/systemload.sh @@ -0,0 +1,19 @@ +#!/bin/bash +################################################## +# # +# Author: Ankam Ravi Kumar # +# Website: server-computer.com # +# Date: 23-02-2019 16:59:56 # +# Purpose: Capture and Store System Load Average # +# CPU Usage and Memory Usage # +################################################## +# Log File Path +LOGFILE=/var/log/systemload.log + +HOSTNAME=$(hostname) +DATE=$(date "+%d-%m-%Y %H:%M:%S") +SYSTEMLOAD=$(uptime | awk '{ print $8,$9,$10,$11,$12}') +CPULOAD=$(top -b -n 2 -d1 | grep "Cpu(s)" | tail -n1 |awk '{print $2}') +MEMORYUSAGE=$(free -m |grep Mem: |tail -n1 |awk '{print $2,$3}') + +echo "$DATE $HOSTNAME LoadAverage: $SYSTEMLOAD CPU: $CPULOAD Memory: $MEMORYUSAGE" >> $LOGFILE diff --git a/uadd.sh b/uadd.sh new file mode 100644 index 0000000..722e934 --- /dev/null +++ b/uadd.sh @@ -0,0 +1,14 @@ +#!/bin/bash +#Purpose: eval command Evaluating twice +#Version:1.0 +#Created Date: Wed Jun 13 22:09:59 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # + +echo "ecnter the user name $NM" +read NM +echo "`useradd -d /users/$NM $NM`" + +# END # diff --git a/ud.sh b/ud.sh new file mode 100644 index 0000000..1c34e9d --- /dev/null +++ b/ud.sh @@ -0,0 +1,16 @@ +#!/bin/bash +#Purpose: eval command Evaluating twice +#Version:1.0 +#Created Date: Wed Jun 13 22:09:59 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # + +echo "WEl COME TO $USER" +echo " Your present Wroking Directory is `pwd`" +echo "Present Processes are `ps -a`" +echo "Now Time is `date`" +echo "current logged in Details are `finger $USER`" + +# END # diff --git a/untiloop.sh b/untiloop.sh new file mode 100644 index 0000000..7994e64 --- /dev/null +++ b/untiloop.sh @@ -0,0 +1,19 @@ +#!/bin/bash +#Purpose: Until Loop Example for Host Ping +#Version:1.0 +#Created Date: Mon May 28 22:18:52 IST 2018 +#Modified Date: +#WebSite: https://arkit.co.in +#Author: Ankam Ravi Kumar +# START # +echo -e "Please Enter the IP Address to Ping: \c" +read -r ip +until ping -c 3 $ip +do + echo "Host $ip is Still Down" + sleep 1 +done + +echo "Host $ip is Up Now" + +# END # diff --git a/useradd.sh b/useradd.sh new file mode 100644 index 0000000..21f061b --- /dev/null +++ b/useradd.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# Script to add a user to Linux system +if [ $(id -u) -eq 0 ]; then + read -p "Enter username : " username + read -s -p "Enter password : " password + egrep "^$username" /etc/passwd >/dev/null + if [ $? -eq 0 ]; then + echo "$username exists!" + exit 1 + else + pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) + useradd -m -p $pass $username + [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!" + fi +else + echo "Only root may add a user to the system" + exit 2 +fi diff --git a/useradd_improved.sh b/useradd_improved.sh new file mode 100644 index 0000000..d41a150 --- /dev/null +++ b/useradd_improved.sh @@ -0,0 +1,37 @@ +#!/bin/bash +## Author: Ankam Ravi Kumar +## Date: 21st Sep 2024 +## Purpose: To Create a users in Linux + +check_command_success() { + if [ $? -ne 0 ]; then + echo "Error: $1" + exit 1 + fi +} + +if [ $(id -u) -ne 0 ]; then + echo "Error: Only root may add a user to the system." + exit 2 +fi + +# Prompt for the username and password +read -p "Enter username: " username +read -s -p "Enter password: " password +echo + +if id "$username" &>/dev/null; then + echo "Error: User '$username' already exists!" + exit 1 +fi + +encrypted_password=$(perl -e 'print crypt($ARGV[0], "password")' "$password") +check_command_success "Failed to encrypt the password." + +useradd -m -p "$encrypted_password" "$username" +check_command_success "Failed to add the user." + +passwd -e "$username" +check_command_success "Failed to set password expiry." + +echo "Success: User '$username' has been added to the system!" diff --git a/userexists.sh b/userexists.sh new file mode 100644 index 0000000..1b8dde7 --- /dev/null +++ b/userexists.sh @@ -0,0 +1,18 @@ +#!/bin/bash +##Purpose: Check given user Exits Or Not +##Date: 27th Oct 2016 +##Author: Ankam Ravi Kumar +##WebSite: https://arkit.co.in + +##Start +echo -e "Please Enter User name you want check: \c" +read user +grep $user /etc/passwd > /dev/null +if [ $? -eq 0 ]; then +grep $user /etc/passwd +echo "$user Exists in this Machine" +else +echo "$user does not exists" +fi + +##END diff --git a/variables.sh b/variables.sh index 7d765aa..7f3128d 100644 --- a/variables.sh +++ b/variables.sh @@ -6,6 +6,7 @@ #website: https://arkit.co.in #Author: Ankam Ravi Kumar # START # + A=10 Ba=23 BA=45 diff --git a/webserver_ubuntu.sh b/webserver_ubuntu.sh new file mode 100755 index 0000000..6a28ea9 --- /dev/null +++ b/webserver_ubuntu.sh @@ -0,0 +1,142 @@ +#!/usr/bin/env bash + +############################################################# +# # +# NOME: webserver_ubuntu.sh # +# # +# AUTOR: Amaury B. Souza (amaurybsouza@gmail.com) # +# # +# DESCRIÇÃO: O script faz a instalação da stack LAMP # +# # +# USO: ./webserver_ubuntu.sh # +############################################################# + +function menuprincipal () { + clear + echo " " + echo LAMP Stack Ubuntu $0 + echo " " + echo "Escolha uma opção abaixo para começar! + + 1 - Instalar Apache no sistema + 2 - Instalar o banco de dados MariaDB no sistema + 3 - Instalar o PHP7.2 no sistema + 4 - Instalar a stack LAMP completa no sistema + 0 - Sair do menu de instalação" +echo " " +echo -n "Opção escolhida: " +read opcao +case $opcao in + 1) + function apache () { + TIME=2 + echo Atualizando seu sistema... + sleep $TIME + apt update && apt upgrade -y + echo Iniciando a instalação do Apache no Ubuntu... + sleep $TIME + #sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT + #sudo ufw allow http + #sudo chown www-data:www-data /var/www/html/ -R + apt install -y apache2 apache2-utils + sudo systemctl start apache2 + sudo systemctl enable apache2 + echo " " + if [ $? -eq 0 ] + then + echo O Apache foi instalado no seu sistema. + else + echo Ops, ocorreu algum erro, vamos tentar de novo! + fi + } + apache + read -n 1 -p " para menu principal" + menuprincipal + ;; + + 2) + function maria () { + TIME=2 + echo Iniciando a instalação do MariaDB... + sleep $TIME + sudo apt -y install mariadb-server mariadb-client + sudo systemctl start mariadb + sudo systemctl enable mariadb + if [ $? -eq 0 ] + then + echo Agora vamos configurar o banco... + sleep $TIME + sudo mysql_secure_installation + echo " " + echo Opa, parabéns, o banco foi instalado e configurado! + sleep $TIME + else + echo Ops, vamos resolver isso? Acho que deu errado. + fi + } + maria + read -n 1 -p " para menu principal" + menuprincipal + ;; + + 3) + function php () { + echo Iniciando a instalação do PHP... + sudo apt install -y php7.2 libapache2-mod-php7.2 php7.2-mysql php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline + sudo a2enmod php7.2 + sudo systemctl restart apache2 + echo " " + echo O PHP 7.2 foi instalado, que legal! + #Para testar o PHP instalado... + #sudo vim /var/www/html/info.php + } + php + read -n 1 -p " para menu principal" + menuprincipal + ;; + + 4) + function lamp () { + TIME=2 + #apache + echo Vamos iniciar a instalação da stack LAMP no seu sistema... + sleep $TIME + echo Instalando o Apache... + sleep $TIME + apt install -y apache2 apache2-utils + sudo systemctl start apache2 + sudo systemctl enable apache2 + echo Instalando o banco de dados... + sleep $TIME + #banco de dados + sudo apt -y install mariadb-server mariadb-client + sudo systemctl start mariadb + sudo systemctl enable mariadb + #PHP + echo Instalando o PHP... + sleep $TIME + sudo apt install -y php7.2 libapache2-mod-php7.2 php7.2-mysql php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline + sudo a2enmod php7.2 + sudo systemctl restart apache2 + echo Instalação concluída com êxito! + sleep $TIME + } + lamp + read -n 1 -p " para menu principal" + menuprincipal + ;; + + 0) + function sair () { + TIME=2 + echo " " + echo Saindo do sistema... + sleep $TIME + exit 0 + } + sair + ;; + +esac +} +menuprincipal diff --git a/while-loop.sh b/while-loop.sh index b636866..3f314c7 100644 --- a/while-loop.sh +++ b/while-loop.sh @@ -1,8 +1,10 @@ -While Loop Example, print any given number table. - #!/bin/bash -## While Loop Example with 2 table -echo -e "Please provide one value:\c" +# While Loop Example with 2 table, print any given number table. +# See Full Explanation of this above shell script [while loop](https://www.youtube.com/Techarkit?sub_confirmation=1) + +#START + +echo -e "Please provide one value: \c" read -r c i=1 while [ $i -le 10 ] @@ -12,4 +14,4 @@ echo "$c * $i = $b" i=`expr $i + 1` done -See Full Explanation of this above shell script [while loop](https://www.youtube.com/Techarkit?sub_confirmation=1) +#END \ No newline at end of file