Bash is a Unix Shell and Command Language which runs in the form of a file (myscript.sh) and issues commands to the machine(s) it’s running on or connected to. Bash Scripts can save countless hours of time editing files or performing operations, so it’s quite versatile and useful.
A simple Bash Script might look something similar to this:
#!/bin/bash
# Monitor Network Connections - K0NxT3D 2021
clear
# All Connections to File
# sudo netstat -atupen >> output.txt
# Established Connections To File
# sudo netstat -atnp | grep ESTA >> output.txt
# Live Monitoring No File
sudo watch -d -n3 "netstat -atnp | grep ESTA"
The use of the pound sign at the beginning and following text is called the “Shebang” and is critical to your script environment, while also being used alone to Comment lines out in the rest of the script.
The Shebang is only used at the beginning of the script.
Scripts can be run on Linux based operating systems as well as Windows, my examples here are exclusive to Linux.
Sometimes you need a very “Personal” script to edit particular data from one or more files and might end up with something more like this processing script which gives me an output of the ip or website currently connected to my machine.
#!/bin/bash
#netstat -a -c -tp (Run As Sudo For More Details)
clear
date=$(date +"%m-%d-%Y_%H-%M")
file="output.txt"
ipo='^[0-9]+([.][0-9]+)?$'
#file="$date-netstat.output"
touch $file
netstat -tp|awk '{print $5}' >> $file
sleep 10
sed -i 's/:https//' $file
sed -i 's/:http//' $file
sed -i 's/servers)//' $file
sed -i 's/Address//' $file
sed -i 's/edge-msgr-latest-//' $file
sed -i 's/edge-star-shv-01-//' $file
sed -i 's/localhost:netbios-ssn//' $file
sed -i '/^\s*$/d' $file
sleep 10
while IFS= read -r ip
do
ping -c 1 $ip &> /dev/null && echo $ip >> IPS.txt || echo Could Not Connect.
done < $file
rm $file
This script simply removes lines of information I don’t need from the output file it generates and cleans up my data and the mess.
Sometimes, I just wanna mess with my dogs.
FOREVER!
#!/bin/bash
i=0
until [ $i -gt 3 ]
do
random=$(cat /dev/urandom | tr -dc '0-9' | fold -w 4 | head -n 1)
ffplay -f lavfi -i "sine=frequency=$random:duration=1" -autoexit -nodisp
((i=i-1))
done
You can always read more information here on Wikipedia.