Linux System Administration

Here is a small bash script to test a hard drive transfer speed. It should be take as an approximation. The speed value is taken from Linux dd command output. One way to test your hard drive speed is to use hdparm command:

# hdparm -Tt /dev/sda

OUTPUT:

/dev/sda:
Timing cached reads: 7216 MB in 2.00 seconds = 3615.89 MB/sec
Timing buffered disk reads: 288 MB in 3.00 seconds = 95.87 MB/sec

However, in this case the hdparm command is accessing raw hard drive, disregarding all partitions and file systems. The weakness of the following script is that it does not take source hard drive reading speed into consideration, however it is accurate when measuring transfer speed between two hard drives or speed between two nodes over the network using NFS or samba. Run the script with 3 arguments, source file, destination file and number of runs to make an average:

NOTE: If you do not have a file to copy simply create one by running a following command for couple seconds and interrupt with CTRL+C:

$ cat /dev/zero > myfile.zero

speed_test.sh :

#!/bin/bash

# USAGE:
# ./speed_test.sh /path/to/my/file /path/to/destination number_of_tests

NUM_TESTs=$3
SUM=0
for i in$(seq 1 $NUM_TESTs); do

REC=`ddif=$1 of=$22>some_random_file_ ; catsome_random_file_|cut -d " " -f8 |tail -1`

SUM=`echo$SUM + $REC|bc`

done

RESULT=`echo$SUM / $NUM_TESTs|bc|awk'{ str1=str1 $0 }END{ print str1 }'`

echo$RESULT MB/s

#clean up
rmsome_random_file_
rm$2

./speed_test.sh /mnt/sdb1/ubuntu.iso /mnt/sda1/ubuntu.dd 3

OUTPUT:

57 MB/s
Free BASH scripting guide DOWNLOAD
>PDF DOWNLOAD<
Free BASH scripting guide DOWNLOAD
>PDF DOWNLOAD<
This bash script tutorial assumes no previous knowledge of bash scripting.As you will soon discover in this quick comprehensive bash scripting guide, learning the bash shell scripting is very easy task. However, if you do not find an answer to your questions by reading this bash tutorial or you need extra help, feel free to ask us on our new Linux Forum. We will be more than happy to help you with your bash questions there.

Lets begin this bash scripting tutorial with a simple "Hello World" script. Let's start with Learning the bash Shell: Unix Shell Programming

Bash Scripting Beginners Guide
FREE PDF DOWNLOAD



Bash Scripting Advanced Guide
FREE PDF DOWNLOAD

Hello World Bash Shell Script


First you need to find out where is your bash interpreter located. Enter the following into your command line:



$ which bash

bash interpreter location: /bin/bash

Open up you favorite text editor and a create file called hello_world.sh. Insert the following lines to a file:

NOTE:Every bash shell script in this tutorial starts with shebang:"#!" which is not read as a comment. First line is also a place where you put your interpreter which is in this case: /bin/bash.

Here is our first bash shell script example:

#!/bin/bash
# declare STRING variable
STRING="Hello World"
#print variable on a screen
echo $STRING

Navigate to a directory where your hello_world.sh is located and make the file executable:

$ chmod +x hello_world.sh 

Make bash shell script executable

Now you are ready to execute your first bash script:

./hello_world.sh 

Example of simple bash shell script

Read more...

This article provides few simple scripts to scan and monitor network using combination of bash and ping command. Obviously, these scripts are no match to a full monitoring dedicated software like nagios but they could be useful for a small home brand networks, where implementing sophisticated monitoring system can become an overhead.

Scan network subnet

In this example the bash script will scan network for hosts attached to an IP address 10.1.1.1 - 255. The script will print message Node with IP: IP-address is up if ping command was successful. Feel free to modify the script to scan your hosts range.

#!/bin/bash

is_alive_ping()
{
  ping -c 1 $1 > /dev/null
  [ $? -eq 0 ] && echo Node with IP: $i is up.
}

for i in 10.1.1.{1..255} 
do
is_alive_ping $i & disown
done

Execute:

./bash_ping_scan.sh

OUTPUT:

Node with IP: 10.1.1.1 is up.
Node with IP: 10.1.1.4 is up.
Node with IP: 10.1.1.9 is up.
Read more...

Page 8 of 73

Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top