diff --git a/Assignment_1.sh b/Assignment_1.sh new file mode 100644 index 0000000..abbcd38 --- /dev/null +++ b/Assignment_1.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +echo -n "Enter your directory: " +read -r x +du -sh "$x" diff --git a/Assignment_10.sh b/Assignment_10.sh new file mode 100644 index 0000000..b4dbf6f --- /dev/null +++ b/Assignment_10.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +echo -n "Enter The Number: " +read -r n +if [ $((n % 2)) -eq 0 ]; then + echo "is a Even Number" +else + echo "is a Odd Number" +fi \ No newline at end of file diff --git a/Assignment_11.sh b/Assignment_11.sh new file mode 100644 index 0000000..ead1e4c --- /dev/null +++ b/Assignment_11.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +echo -n "Enter A Number: " +read -r n +arm=0 +temp=$n +while [ "$n" -ne 0 ]; do + r=$((n % 10)) + arm=$((arm + r * r * r)) + n=$((n / 10)) +done +echo $arm +if [ $arm -eq "$temp" ]; then + echo "Armstrong" +else + echo "Not Armstrong" +fi \ No newline at end of file diff --git a/Assignment_12.sh b/Assignment_12.sh new file mode 100644 index 0000000..aa5c9e1 --- /dev/null +++ b/Assignment_12.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +echo -n "Enter Any Number: " +read -r n +i=1; c=1 +while [ $i -le "$n" ]; do + i=$((i + 1)) + r=$((n % i)) + [ $r -eq 0 ] && c=$((c + 1)) +done + +if [ $c -eq 2 ]; then + echo "Prime" +else + echo "Not Prime" +fi diff --git a/Assignment_13.sh b/Assignment_13.sh new file mode 100644 index 0000000..3aa2a40 --- /dev/null +++ b/Assignment_13.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +x=0; y=1; i=2 +while true; do + i=$((i + 1)) + z=$((x + y)) + echo -n "$z " + x=$y + y=$z + sleep .5 +done \ No newline at end of file diff --git a/Assignment_14.sh b/Assignment_14.sh new file mode 100644 index 0000000..c7f549d --- /dev/null +++ b/Assignment_14.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +for ((i = 32; i >= 0; i--)); do + r=$((2 ** i)) + Probablity+=($r) +done + +[[ $# -eq 0 ]] && { + echo -e "Usage \n \t $0 numbers" + exit 1 +} + +echo -en "Decimal\t\tBinary\n" +for input_int; do + s=0 + test ${#input_int} -gt 11 && { + echo "Support Upto 10 Digit number :: skiping \"$input_int\"" + continue + } + + printf "%-10s\t" "$input_int" + + for n in ${Probablity[@]}; do + + if [[ $input_int -lt $n ]]; then + [[ $s == 1 ]] && printf "%d" 0 + else + echo -n 1 + s=1 + input_int=$((input_int - n)) + fi + done + echo -e +done \ No newline at end of file diff --git a/Assignment_15.sh b/Assignment_15.sh new file mode 100644 index 0000000..16715ce --- /dev/null +++ b/Assignment_15.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +# +# Author: Abhishek Shingane (abhisheks@iitbhilai.ac.in) +# Date: 11 Sep 2020 +# + +if ! [ -x "$(command -v jq)" ]; then + echo 'Error: jq is not installed. Install via https://stedolan.github.io/jq/download/' + exit 1 +fi + +if [[ $# -ne 1 ]]; then + echo 'Provide I.P as command line parameter. Usage: ' $0 ' 15.45.0.1 ' + exit 1 +fi +link=$(echo "http://ip-api.com/json/"$1) +data=$(curl $link -s) # -s for slient output + +status=$(echo $data | jq '.status' -r) + +if [[ $status == "success" ]]; then + + city=$(echo $data | jq '.city' -r) + regionName=$(echo $data | jq '.regionName' -r) + country=$( echo $data | jq '.country' -r) + echo $city, $regionName in $country. +fi \ No newline at end of file diff --git a/Assignment_16.sh b/Assignment_16.sh new file mode 100644 index 0000000..f67279c --- /dev/null +++ b/Assignment_16.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +name=$1 +path=$2 +tar -czvf "$name.tar.gz" "$path" +gpg -c "$name.tar.gz" +rm -rf "$name.tar.gz" \ No newline at end of file diff --git a/Assignment_17.sh b/Assignment_17.sh new file mode 100644 index 0000000..58a4c18 --- /dev/null +++ b/Assignment_17.sh @@ -0,0 +1,23 @@ + +#!/bin/bash +# +# Linux Shell Scripting Tutorial 1.05r3, Summer-2002 +# +# Written by Vivek G. Gite +# +# Latest version can be found at www.nixcraft.com/ +# +# Q1.Script to sum to nos +# + +if [ $# -ne 2 ] +then +echo “Usage – $0 x y” +echo ” Where x and y are two nos for which I will print sum” +exit 1 +fi +echo “Sum of $1 and $2 is `expr $1 + $2`” +# +# ./ch.sh: vivek-tech.com to nixcraft.com referance converted using this tool +# See the tool at www.nixcraft.com/uniqlinuxfeatures/tools/ +# \ No newline at end of file diff --git a/Assignment_18.sh b/Assignment_18.sh new file mode 100644 index 0000000..1f36cea --- /dev/null +++ b/Assignment_18.sh @@ -0,0 +1,12 @@ +# scmuser created the topic: how to restore the dump file using shell scripting + +# I have created a script file to dump the application files using the following script + +# Code +# ============= +#!/bin/bash + + +now=$(date +”%d-%m-%Y”) +#use 1 instead of 0 which is incremental backup +dump -0f $now /var/www/html/* \ No newline at end of file diff --git a/Assignment_2.sh b/Assignment_2.sh new file mode 100644 index 0000000..83b84b8 --- /dev/null +++ b/Assignment_2.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# test-file: Evaluate the status of a file +echo "Hey what's the File/Directory name (using the absolute path)?" +read FILE + +if [ -e "$FILE" ]; then + if [ -f "$FILE" ]; then + echo "$FILE is a regular file." + fi + if [ -d "$FILE" ]; then + echo "$FILE is a directory." + fi + if [ -r "$FILE" ]; then + echo "$FILE is readable." + fi + if [ -w "$FILE" ]; then + echo "$FILE is writable." + fi + if [ -x "$FILE" ]; then + echo "$FILE is executable/searchable." + fi +else + echo "$FILE does not exist" + exit 1 +fi +exit + + + +#!/bin/bash + +# Program to check the status of a file + +echo -n "Enter the file directory" +read -r File + +if [ -e "$FILE" ]; then + if [-f $FILE]; then + echo "$FILE is a regular file" + if + if [ -r $FILE ]; then + echo "$FILE is a readable file" + if + if [ -d $FILE ]; then + echo "$FILE is a directory" + if +else + echo "$FILE is not available " + exit 1 +fi +exit + + diff --git a/Assignment_3.sh b/Assignment_3.sh new file mode 100644 index 0000000..804c615 --- /dev/null +++ b/Assignment_3.sh @@ -0,0 +1,35 @@ +#!/bin/bash +date +echo "uptime:" +uptime +echo "Currently connected:" +w +echo "--------------------" +echo "Last logins:" +last -a | head -3 +echo "--------------------" +echo "Disk and memory usage:" +df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}' +free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}' +echo "--------------------" +start_log=$(head -1 /var/log/messages | cut -c 1-12) +oom=$(grep -ci kill /var/log/messages) +echo -n "OOM errors since $start_log :" $oom +echo "" +echo "--------------------" +echo "Utilization and most expensive processes:" +top -b | head -3 +echo +top -b | head -10 | tail -4 +echo "--------------------" +echo "Open TCP ports:" +nmap -p -T4 127.0.0.1 +echo "--------------------" +echo "Current connections:" +ss -s +echo "--------------------" +echo "processes:" +ps auxf --width=200 +echo "--------------------" +echo "vmstat:" +vmstat 1 5 \ No newline at end of file diff --git a/Assignment_4.sh b/Assignment_4.sh new file mode 100644 index 0000000..91e2077 --- /dev/null +++ b/Assignment_4.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +MAX=95 +EMAIL=server@127.0.0.1 + +# Calculate CPU usage and round to the nearest integer +USE=$(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {printf "%.0f", usage}') + +# Debug statement to print the calculated CPU usage +echo "Calculated CPU Usage: $USE" + +# Compare the integer value with the maximum threshold +if [ "$USE" -gt "$MAX" ]; then + echo "Percent used: $USE" | mail -s "Running out of CPU power" "$EMAIL" + echo "Email sent." +else + echo "CPU usage below threshold." +fi diff --git a/Assignment_5.sh b/Assignment_5.sh new file mode 100644 index 0000000..8d033da --- /dev/null +++ b/Assignment_5.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +MAX=95 +EMAIL=server@127.0.0.1 +PART=sda1 + +# Extract the usage percentage, making sure to handle the case where the partition is not found +DF_OUTPUT=$(df -h) +USE=$(echo "$DF_OUTPUT" | awk -v part="$PART" '$NF==part {print $(NF-1)}' | sed 's/%//') + +# Debug statement to print the actual output of the df command +echo "df output:" +echo "$DF_OUTPUT" + +# Check if USE is a valid number +if [[ "$USE" =~ ^[0-9]+$ ]]; then + # Compare the integer value with the maximum threshold + if [ "$USE" -gt "$MAX" ]; then + echo "Percent used: $USE" | mail -s "Running out of disk space" "$EMAIL" + fi + +fi \ No newline at end of file diff --git a/Assignment_6.sh b/Assignment_6.sh new file mode 100644 index 0000000..ccc31e7 --- /dev/null +++ b/Assignment_6.sh @@ -0,0 +1,115 @@ +#!/bin/bash + +IP4FW=/sbin/iptables +IP6FW=/sbin/ip6tables +LSPCI=/usr/bin/lspci +ROUTE=/sbin/route +NETSTAT=/bin/netstat +LSB=/usr/bin/lsb_release + +## files ## +DNSCLIENT="/etc/resolv.conf" +DRVCONF="/etc/modprobe.conf" +NETALIASCFC="/etc/sysconfig/network-scripts/ifcfg-eth?-range?" +NETCFC="/etc/sysconfig/network-scripts/ifcfg-eth?" +NETSTATICROUTECFC="/etc/sysconfig/network-scripts/route-eth?" +SYSCTL="/etc/sysctl.conf" + +## Output file ## +OUTPUT="network.$(date +'%d-%m-%y').info.txt" + +## Email info to?? ## +SUPPORT_ID="your_name@service_provider.com" + +chk_root() { + local meid=$(id -u) + if [ $meid -ne 0 ]; then + echo "You must be root user to run this tool" + exit 999 + fi +} + +write_header() { + echo "---------------------------------------------------" >>$OUTPUT + echo "$@" >>$OUTPUT + echo "---------------------------------------------------" >>$OUTPUT +} + +dump_info() { + echo "* Hostname: $(hostname)" >$OUTPUT + echo "* Run date and time: $(date)" >>$OUTPUT + + write_header "Linux Distro" + echo "Linux kernel: $(uname -mrs)" >>$OUTPUT + $LSB -a >>$OUTPUT + + [ -x ${HWINF} ] && write_header "${HWINF}" + [ -x ${HWINF} ] && ${HWINF} >>$OUTPUT + + [ -x ${HWINF} ] && write_header "${HWINF}" + [ -x ${HWINF} ] && ${HWINF} >>$OUTPUT + + write_header "PCI Devices" + ${LSPCI} -v >>$OUTPUT + + write_header "$IFCFG Output" + $IFCFG >>$OUTPUT + + write_header "Kernel Routing Table" + $ROUTE -n >>$OUTPUT + + write_header "Network Card Drivers Configuration $DRVCONF" + [ -f $DRVCONF ] && grep eth $DRVCONF >>$OUTPUT || echo "Error $DRVCONF file not found." >>$OUTPUT + + write_header "DNS Client $DNSCLIENT Configuration" + [ -f $DNSCLIENT ] && cat $DNSCLIENT >>$OUTPUT || echo "Error $DNSCLIENT file not found." >>$OUTPUT + + write_header "Network Configuration File" + for f in $NETCFC; do + if [ -f $f ]; then + echo "** $f **" >>$OUTPUT + cat $f >>$OUTPUT + else + echo "Error $f not found." >>$OUTPUT + fi + done + + write_header "Network Aliase File" + for f in $NETALIASCFC; do + if [ -f $f ]; then + echo "** $f **" >>$OUTPUT + cat $f >>$OUTPUT + else + echo "Error $f not found." >>$OUTPUT + fi + done + + write_header "Network Static Routing Configuration" + for f in $NETSTATICROUTECFC; do + if [ -f $f ]; then + echo "** $f **" >>$OUTPUT + cat $f >>$OUTPUT + else + echo "Error $f not found." >>$OUTPUT + fi + done + + write_header "IP4 Firewall Configuration" + $IP4FW -L -n >>$OUTPUT + + write_header "IP6 Firewall Configuration" + $IP6FW -L -n >>$OUTPUT + + write_header "Network Stats" + $NETSTAT -s >>$OUTPUT + + write_header "Network Tweaks via $SYSCTL" + [ -f $SYSCTL ] && cat $SYSCTL >>$OUTPUT || echo "Error $SYSCTL not found." >>$OUTPUT + + echo "The Network Configuration Info Written To $OUTPUT. Please email this file to $SUPPORT_ID." +} + +chk_root +dump_info + + diff --git a/Assignment_7.sh b/Assignment_7.sh new file mode 100644 index 0000000..7aea2eb --- /dev/null +++ b/Assignment_7.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +rsync -avz -e "ssh " /path/to/yourfile user@backupserver.com:/backup/ +echo "backup for $(date) " | mail -s "backup complete" user@youremail.com \ No newline at end of file diff --git a/Assignment_8.sh b/Assignment_8.sh new file mode 100644 index 0000000..56c7835 --- /dev/null +++ b/Assignment_8.sh @@ -0,0 +1,172 @@ +#!/usr/bin/env bash +# ------------------------------------------------------------------------ # +# Script Name: hardware_machine.sh +# Description: Show informations about machine hardware. +# Written by: Amaury Souza +# Maintenance: Amaury Souza +# ------------------------------------------------------------------------ # +# Usage: +# $ ./hardware_machine.sh +# ------------------------------------------------------------------------ # +# Bash Version: +# Bash 4.4.19 +# ------------------------------------------------------------------------ # + +function menuprincipal () { +clear +TIME=1 +echo " " +echo $0 +echo " " +echo "Choose an option below! + 1 - Verify desktop processor + 2 - Verify system kernel + 3 - Verify installed softwares + 4 - Operation system version + 5 - Verify desktop memory + 6 - Verify serial number + 7 - Verify system IP + 0 - Exit" +echo " " +echo -n "Chosen option: " +read opcao +case $opcao in + 1) + function processador () { + CPU_INFO=`cat /proc/cpuinfo | grep -i "^model name" | cut -d ":" -f2 | sed -n '1p'` + echo "CPU model: $CPU_INFO" + sleep $TIME + } + processador + read -n 1 -p " for main menu" + menuprincipal + ;; + + 2) + function kernel () { + #RED HAT: cat /etc/redhat-release + KERNEL_VERSION_UBUNTU=`uname -r` + KERNEL_VERSION_CENTOS=`uname -r` + if [ -f /etc/lsb-release ] + then + echo "kernel version: $KERNEL_VERSION_UBUNTU" + else + echo "kernel version: $KERNEL_VERSION_CENTOS" + fi + } + kernel + read -n 1 -p " for main menu" + menuprincipal + ;; + + 3) + function softwares () { + #while true; do + TIME=3 + echo " " + echo "Choose an option below for program's list! + + 1 - List Ubuntu programs + 2 - List Fedora programs + 3 - Install programs + 4 - Back to menu" + echo " " + echo -n "Chosen option: " + read alternative + case $alternative in + 1) + echo "Listing all programs Ubuntu's systems..." + sleep $TIME + dpkg -l > /tmp/programs.txt + echo Programs listed and available at /tmp + sleep $TIME + echo " " + echo "Back to menu!" | tr [a-z] [A-Z] + sleep $TIME + ;; + 2) + echo "Listing all programs Fedora's systems..." + sleep $TIME + yum list installed > /tmp/programs.txt + echo Programs listed and available at /tmp + sleep $TIME + ;; + 3) + echo Installing programss... + LIST_OF_APPS="pinta brasero gimp vlc inkscape blender filezilla" + #use aptitude command for programs loop. + apt install aptitude -y + aptitude install -y $LIST_OF_APPS + ;; + 4) + echo Back to main menu... + sleep $TIME + ;; + esac + #done + } + softwares + menuprincipal + ;; + + 4) + function sistema () { + VERSION=`cat /etc/os-release | grep -i ^PRETTY` + if [ -f /etc/os-release ] + then + echo "The system version: $VERSION" + else + echo "System not supported" + fi + } + sistema + read -n 1 -p " for main menu" + menuprincipal + ;; + + + 5) + function memory () { + MEMORY_FREE=`free -m | grep ^Mem | tr -s ' ' | cut -d ' ' -f 4` + #MEMORY_TOTAL= + #MEMORY_USED= + echo Verifying system memory... + echo "Memory free is: $MEMORY_FREE" + } + memory + read -n 1 -p " for main menu" + menuprincipal + ;; + + 6) + function serial () { + SERIAL_NUMBER=`dmidecode -t 1 | grep -i serial` + echo $SERIAL_NUMBER + } + serial + read -n 1 -p " for main menu" + menuprincipal + ;; + + 7) + function ip () { + IP_SISTEMA=`hostname -I` + echo IP is: $IP_SISTEMA + } + ip + read -n 1 -p " for main menu" + menuprincipal + ;; + + 0) + echo Exiting the system... + sleep $TIME + exit 0 + ;; + + *) + echo Invalid option, try again! + ;; +esac +} +menuprincipal \ No newline at end of file diff --git a/Assignment_9.sh b/Assignment_9.sh new file mode 100644 index 0000000..31e5439 --- /dev/null +++ b/Assignment_9.sh @@ -0,0 +1,10 @@ + +#!/usr/bin/env bash + +TEMP_FILE=/sys/class/thermal/thermal_zone0/temp + +ORIGINAL_TEMP=$(cat $TEMP_FILE) +TEMP_C=$((ORIGINAL_TEMP/1000)) +TEMP_F=$(($TEMP_C * 9/5 + 32)) + +echo $TEMP_F F \ No newline at end of file