BASH SCRIPT
ANGGRAHITO, S.ST., S.T., M.B.A
Bash Script
BASIC COMMAND
• Echo
• Variables
• Shell Execution
• Loops
• Functions
• Copy
• Read File
NETWORKING
• My IP ADDRESS
• Pingsweep
• Problem
Copyright ©TAALENTA 2023
INFORMATION GATHERING
Information Gathering Tools:
• Nmap
• Nikto
• Whatweb
• Sublist3r
Copyright ©TAALENTA 2023
BASIC BASH SCRIPTING
Copyright ©TAALENTA 2023
BASIC BASH SCRIPT COMMAND
Syntax Description
#!/bin/bash Shebang that goes on the first line of every Bash script
Alternative (and better) shebang – using environment
#!/usr/bin/env bash
variable
Used to make comments, text that comes after it will not
#
be executed
chmod +x script.sh && ./script.sh Give script executable permissions and execute it
$# Stores the number of arguments passed to the Bash script
Variables that store the values passed as arguments to the
$1, $2, $3
Bash script
exit Exit from the Bash script, optionally add an error code
Keyboard combination to stop Bash script in the middle of
Ctrl + C
execution
$( ) Execute a command inside of a subshell
Common Separate Values
Common Separate Values
• cut –b
• cut –d “<value> -f1
• cut –d “.” –f1
Print first field for each record in file
• awk '{print $1}'
Copyright ©TAALENTA 2023
READ DIRECTORY
#!/bin/bash
var1=Hello
var2=World
echo $var1 $var2
echo
sampledir=/etc/passwd
cat $sampledir
VARIABLES
• name=anggra"
• echo $name
• echo "$name"
• echo "${name}!"
Copyright ©TAALENTA 2023
VARIABLES
Example:
name=anggra"
echo $name
echo "$name"
echo "${name}!"
SimpleScript:
#!/bin/bash
# contoh variables
clear
var1=Hello
var2=World
echo "variabel 1 dan 2 adalah " $var1 $var2
echo
sampledir=/etc/passwd
cat $sampledir
Copyright ©TAALENTA 2023
SHELL EXECUTION
• echo "I'm in “ $pwd
• echo "I'm in $pwd” # obsolescent # Same
• echo “ my name is :” $name
• echo “ my name is: $name”
• echo ”hostname of this PC is:”`hostname`
Copyright ©TAALENTA 2023
LOOPS
Basic: Ranges Reading lines:
for i in /etc/rc.*; do for i in {1..5}; do while read -r line; do
echo "$i" echo ”Welcome $i" echo “$line”
done done done <file.txt
C-Like for Loop: With step Size
for ((i = 0; i < 100 ; i++)); do for i in {5..50..5}; do
echo "$i"
echo “welcome $1”
done
done
Copyright ©TAALENTA 2023
FUNCTIONS
#!/bin/bash
myfunc() {
echo "hello $1"
}
# Sama seperti dibawah (alternate syntax)
function myfunc() {
echo "hello $1"
}
myfunc ”hello"
Copyright ©TAALENTA 2023
ADDITION
#!/bin/bash
var1=5
var2=10
var3=$(($var1+$var2))
echo "$var3"
#function
add () {
var4=$(($var3 + 20))
echo $var4
Copyright ©TAALENTA 2023
COPY
#!/bin/bash
#copy file1 dari folder awal ke folder lain
echo "contoh cp folder1/file1 folder2/"
folder='/home/pentest/Desktop/Pelatihan/bash_script/simples
cript/folder2'
cp $1 $folder
#kita lihat apakah file sudah terkopi
echo Details for $folder
ls -lh $folder
Copyright ©TAALENTA 2023
READ FILE
#!/bin/bash
read -p "Masukan File : " file
cat $file
Copyright ©TAALENTA 2023
NETWORKING
Copyright ©TAALENTA 2023
My IP Address
#! /bin/bash
clear
echo ”SCRIPT UNTUK MENGETAHUI IP ADDRESS KOMPUTER INI"
IP=$(ifconfig | grep -A 1 'eth0'| grep 'inet'| awk '{print $2}')
echo "IP Address : $IP"
Hasil
Copyright ©TAALENTA 2023
PINGSWEEP
#!/bin/bash
if [ "$1" == "" ]
then
echo "Usage: ./pingsweep.sh [network]"
echo "example: ./pingsweep.sh 172.18.0.0/24"
else
for x in `seq 1 254`; do
ping -c 1 $1.$x | grep "64 bytes" | cut -d" " -f4 | tr -d “:” &
done
fi
Copyright ©TAALENTA 2023
CONCLUSION
• $1, $2, ...The first, second, etc command line arguments to the script.
• variable=valueTo set a value for a variable.
• Remember, no spaces on either side of =
• Quotes " 'Double will do variable substitution, single will not.
• variable=$( command )Save the output of a command into a variable
• export var1Make the variable var1 available to child processes.
Copyright ©TAALENTA 2023
PROBLEM
• Create Machine Information
• Hostname
• Active User
• Operating System
• Kernel
• Architecture
• Processor Name
• System Main IP
Copyright ©TAALENTA 2023
Gathering Machine’s Information
echo -e "Hostname:"`hostname`
echo "Uptime :" `uptime | awk '{print $3,$4}' | sed 's/,//'`
echo "IP Address :" `ifconfig | grep -A 1 'eth0'| grep 'inet'| awk '{print $2}'`
echo “User Active :” `w | awk '{print $1}' | grep -A 1 'USER' | grep -v USER`
echo "Operating System:" `hostnamectl | grep "Operating System" | cut -d ":" -f2`
echo "Kernel:" `hostnamectl | grep "Kernel" | cut -d ":" -f2`
echo "Hypervisor :" `lscpu | grep "Hypervisor vendor" | cut -d ":" -f2`
echo "CPU op-mode(s) :" `lscpu | grep "CPU op-mode" | cut -d ":" -f2`
echo "Processor :" `lscpu | grep "Model name" | cut -d ":" -f2`
echo "main IP-Address :"`hostname -I`
echo "memory usage:" `free | grep -A 1 "Mem" | awk '{print $3/$2*100}'| head -1` "%"
Copyright ©TAALENTA 2023
THANK YOU
Copyright ©TAALENTA 2023