Leveling up your mind

Notifications
Clear all

MISC Scripts  

 
Sohaib
(@sohaib-ops)
Member Admin

When you write a script just remember everything in that script run in sequence or in order from the top to bottom.

$ cat basic-task 
#!/bin/bash

# Purpose: Basic task 

echo
echo This script will run a few basic tasks
echo

pwd
ls
whoami
date
cal
touch test{1..3}

bashsmash@bashsmash:~/bin$ basic-task 

This script will run a few basic tasks

/home/bashsmash/bin
backup	basic-task  first_script  hungry  varscript
bashsmash
Thu Nov 14 19:40:00 CST 2019
   November 2019      
Su Mo Tu We Th Fr Sa  
                1  2  
 3  4  5  6  7  8  9  
10 11 12 13 14 15 16  
17 18 19 20 21 22 23  
24 25 26 27 28 29 30  
                      
This topic was modified 4 months ago by Sohaib
Quote
Posted : 15/11/2019 1:56 am
Sohaib
(@sohaib-ops)
Member Admin

Pretty cool script!

1.I created a file with three ips that i want to ping. In that file only three ips are stored nothing else. The variable can be a path to a file like in the example below.The path is to the file that is stored with three ips

hosts=/home/bashsmash/bin/iplist

2. I used the "for loop" statement. Notice the "ip" becomes the variable

for next action $(cat $hosts)

3. Ping the ips stored in the file. Notice the "ip" is for next set of action " $(cat $hosts)" which is to cat the IP stored file

do
ping -c2 $ip &> /dev/null

$ cat hostIPs 
#!/bin/bash

hosts=/home/bashsmash/bin/iplist

for ip in $(cat  $hosts)
do
ping -c2 $ip &> /dev/null
	if [ $? -eq 0 ]
then 
	echo $ip This  checks OK
else
	echo $ip Something is wrong with this server!
fi
done
This post was modified 9 months ago by Sohaib
ReplyQuote
Posted : 19/11/2019 2:31 am
Sohaib
(@sohaib-ops)
Member Admin

Creating multiple files

1.file is the variable and numbered from 1-10
for file in {1..10}

2.what 'do' we want? create testfile from 1-10. I also gave it a path where i want the files to be created

do
touch ~/bash/files/testfile.$file

$ cat creatingfile 
#!/bin/bash

for file in {1..10}
do
	touch ~/bash/files/testfile.$file
done
bashsmash@bashsmash:~/bin$ ls ../bash/files/
folder_jam  testfile.10  testfile.3  testfile.5  testfile.7  testfile.9
testfile.1  testfile.2   testfile.4  testfile.6  testfile.8
ReplyQuote
Posted : 24/11/2019 2:25 am
Sohaib
(@sohaib-ops)
Member Admin

Creating user input file

$ cat user-input 
#!/bin/bash

echo How many file would like to create?
read t
echo
echo Please enter the name of the file you would like to create.
read n

for num in $(seq 1 $t)
do
	touch ~/bash/files/$n.$num
done
bashsmash@bashsmash:~/bin$ user-input 
How many file would like to create?
6

Please enter the name of the file you would like to create.
inputfile
bashsmash@bashsmash:~/bin$ ls ../bash/files/
folder_jam   inputfile.2  inputfile.4  inputfile.6
inputfile.1  inputfile.3  inputfile.5
ReplyQuote
Posted : 24/11/2019 2:39 am
Sohaib
(@sohaib-ops)
Member Admin

Assigning user permissions

*BEFORE*

$ ls -ltr ../bash/files/
total 4
drwxrwxr-x 3 bashsmash bashsmash 4096 Nov  3 17:43 folder_jam
-rw-rw-r-- 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.3
-rw-rw-r-- 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.2
-rw-rw-r-- 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.1
-rw-rw-r-- 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.6
-rw-rw-r-- 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.5
-rw-rw-r-- 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.4

*AFTER*

$ cat assign-permissions 
#!/bin/bash

for f in inputfile.*
do
	echo Assigning write permissions to $f
	chmod +x ~/bash/files/$f
	sleep 1
done
bashsmash@bashsmash:~/bin$ ls -ltr ../bash/files/
total 4
drwxrwxr-x 3 bashsmash bashsmash 4096 Nov  3 17:43 folder_jam
-rwxrwxr-x 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.3
-rwxrwxr-x 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.2
-rwxrwxr-x 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.1
-rwxrwxr-x 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.6
-rwxrwxr-x 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.5
-rwxrwxr-x 1 bashsmash bashsmash    0 Nov 23 20:38 inputfile.4
ReplyQuote
Posted : 24/11/2019 2:53 am
Sohaib
(@sohaib-ops)
Member Admin

To check if certain file exists

$ cat file-check 
#!/bin/bash

FILES="/etc/passwd
/etc/group/
/etc/shadow/
/etc/nsswitch.conf
/etc/sshd_ssh_config
/etc/fake"

echo
for file in $FILES
do 
	if [ ! -e $file ]
	then
	echo $file does not exist
	echo
	fi
done
ReplyQuote
Posted : 24/11/2019 11:44 pm
Sohaib
(@sohaib-ops)
Member Admin

Inventory system script: basically adds system to a database file to keep up with. The database file can be stored anywhere just make sure to define the path in the script.

$ cat bin/sysinventory 
#!/bin/bash

echo What is your hostname?
read host
echo

	grep -q $host /home/bashsmash/backup/database
	if [ $? -eq 0 ]
then
	echo Error -- Hostname $host already exist
	echo
	exit 0
fi

echo Please enter IP address.
read IP
echo 
	grep -q $IP /home/bashsmash/backup/database
	if [ $? -eq 0 ]
then 
	echo Error -- IP $IP already exist
	echo
	exit 0 
fi

echo Please enter description.
read description
echo

echo $host $IP $description >> /home/bashsmash/backup/database
echo The provided record has been added
echo

In Action:

$ sysinventory 
What is your hostname?
hostPC

Please enter IP address.
192.xxx.xx.x

Please enter description.
Sample PC!!

The provided record has been added

In Storage:

$ cat backup/database 
hostPC 192.xxx.xx.x Sample PC!!
ReplyQuote
Posted : 02/12/2019 12:45 am
Sohaib
(@sohaib-ops)
Member Admin

A simple for loop script

$ cat 1script-testing 
#!/bin/bash

colors="red black white"

for col in $colors
do 
	echo $col
done

$ 1script-testing 
red
black
white
ReplyQuote
Posted : 06/12/2019 1:16 am
Sohaib
(@sohaib-ops)
Member Admin

small script to make the first letter lowercase of a capitalized WORD

$ cat 1script-testing 
#!/bin/bash

fir=FIRSTLETTERWILLBELOWERCASE

echo ${fir,}

bashsmash@bashsmash:~/bin$ 1script-testing 
fIRSTLETTERWILLBELOWERCASE
ReplyQuote
Posted : 06/12/2019 1:34 am
Sohaib
(@sohaib-ops)
Member Admin

To make all the letters lowercase in a word is by adding two commas when echoing the variables.

$ cat 1script-testing 
#!/bin/bash

fir=FIRSTLETTERWILLBELOWERCASE

echo ${fir,,}

bashsmash@bashsmash:~/bin$ 1script-testing 
firstletterwillbelowercase
ReplyQuote
Posted : 06/12/2019 1:36 am
Sohaib
(@sohaib-ops)
Member Admin

If you want to print a statement or a word in quote. This can be done by including the "back-slash=escape key \"

$ cat 1script-testing 
#!/bin/bash

echo "\"Bash Scripting is Awesome"\"
bashsmash@bashsmash:~/bin$ 1script-testing 
"Bash Scripting is Awesome"
ReplyQuote
Posted : 06/12/2019 1:43 am
Sohaib
(@sohaib-ops)
Member Admin

To make all the letters uppercase in a word is by adding two ^^ (carat key) when echoing the variables.

$ cat 1script-testing 
#!/bin/bash

UPPERCASE=firstletterwillbeuppercase

echo ${UPPERCASE^^}

bashsmash@bashsmash:~/bin$ 1script-testing 
FIRSTLETTERWILLBEUPPERCASE
ReplyQuote
Posted : 15/12/2019 2:13 am
Sohaib
(@sohaib-ops)
Member Admin

This is an example script of time limit

#!/bin/bash

LIMIT_TIME=18
TIME_INTERVAL=1

echo "This script will run for $LIMIT_TIME seconds."
echo "Press Ctrl-c to exit before the time limit."

while [ "$SECONDS" -le "$LIMIT_TIME" ]
do
	echo "This script has been running for $SECONDS seconds"
	sleep $TIME_INTERVAL
done

In Action:

$ 1script-testing 
This script will run for 18 seconds.
Press Ctrl-c to exit before the time limit.
This script has been running for 0 seconds
This script has been running for 1 seconds
This script has been running for 2 seconds
^C
ReplyQuote
Posted : 14/01/2020 12:34 am
Sohaib
(@sohaib-ops)
Member Admin

This script simply asks the user to press the directional key on the keyboard (left,right,up,down)

Script:

#!/bin/bash

up=$'\x1b[A'
down=$'\x1b[B'
left=$'\x1b[D'
right=$'\x1b[C'

read -s -n3 -p "Pres an arrow key: " arrow

case "$arrow" in
	
	$up) echo "You pressed up";;
	$down) echo "You pressed down";;
	$left) echo "You pressed left";;
	$right) echo "You pressed right";;

esac

Result:

$ 1script-testing 
Pres an arrow key: You pressed left
ReplyQuote
Posted : 20/01/2020 11:30 pm
Sohaib
(@sohaib-ops)
Member Admin

This script shows open network ports on a system. 

 

 

#!/bin/bash
#
#
#  This script shows the open network port on a system.
# Use -4 as an argument to limit to tcpv4 ports.

netstat -nutl $1 | grep ':' | awk '{print $4}' | awk -F ':' '{print $NF}'

 

 

RESULT: 

 

$ ./open_network_ports 
111
53
22
631
42153
111
22
631
5353
323
57256
53
67
68
111
5353
323
52068
111
ReplyQuote
Posted : 29/04/2020 9:38 pm