Powered By Blogger

Thursday, November 13, 2014

Shell Script

Every Unix engineer need to know shell scripting as this is first and evermost tool for making life easier by automating system admin task.

In today's world nobody want to do manual things, everything should be automated e.g switching on water pump of overhead water tank is dependant upon it's water level. i.e when overhead tank consumes 60% water  , it will automatically trigger water pump so that it will be always full , irrespective of time and you will enjoy water all the time.

Same is unix task , you can not stay in office till midnight to take backup of your server , you will need to automate backup job and cron it .

To understand Unix shell scripting , you will need to understand below basic commands and how they work with each other .


Command
Switches
Head / tail
-5 , +5 , -f
Cut
-d , -f (all)
Sort
-f (ignore case) -t (field separator)  -r (reverse order)  -n (numerical sort)
Tr
-c complement the set of characters in string i.e tr -C ab will all cahra except a and b
-d delet chara from string from input
-s squeeze multiple occurance
Grep
-i , -c , -v , -w , -n
Find
-name , -size , -mtime , +mtime, -atime , -type oring or noring
.


Shell Script or command line tool to create user and their first login password
 
for i in `more userlist.txt ;do echo $i,adduser $i ; done
for i in `more userlist.txt ; do echo $i, echo $i"123" |passwd --stdin $i ,echo $username's password is changed"




Reguler Expression
Time in format of HH:MM:SS

^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)

IP Address \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b


How to read Nagios data file: Nagios data file is binary file contain all the data related to your monitoring.
Livestatus (https://mathias-kettner.de/checkmk_livestatus.html)  has utility to read Nagios banary file

e.g /opt/nagios/bin/unixcat  /opt/nagios/var/rw/live




Reg-ex

^.*?\s+(\d+\.\d+\.\d+\.\d+)\s+-\s+-\s+\[([^\]]+)\]\s+"(.*?)"\s+(\d+)\s+(\d+)\s+(\".?\")\s+"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"

Translationservice-3741631407.eu-de1.plex.vodafone.com 127.0.0.1 - - [20/Mar/2015:09:12:18 +0100] "GET / HTTP/1.1" 200 787 "-" "curl/7.19.7 (x86_64-suse-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8j zlib/1.2.3 libidn/1.10" "-" "JSESSIONID=5Z-ZyUUdf8GJZtYrkL6rdeIn.undefined; Path=/, JSESSIONID=ga91iOwiss+cqpjxm5kaea2n.undefined; Path=/" "172.26.69.15, 127.0.0.1" "-" "-" "-" "20318" "global" "live"

MATCH 1
1.[55-64]`127.0.0.1`
2.[70-96]`20/Mar/2015:09:12:18 +0100`
3.[99-113]`GET / HTTP/1.1`
4.[115-118]`200`
5.[119-122]`787`
6.[123-126]`"-"`
7.[128-216]`curl/7.19.7 (x86_64-suse-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8j zlib/1.2.3 libidn/1.10`
8.[219-220]`-`
9.[223-331]`JSESSIONID=5Z-ZyUUdf8GJZtYrkL6rdeIn.undefined; Path=/, JSESSIONID=ga91iOwiss+cqpjxm5kaea2n.undefined; Path=/`
10.[334-357]`172.26.69.15, 127.0.0.1`
11.[360-361]`-`
12.[364-365]`-`
13.[368-369]`-`
14.[372-377]`20318`
15.[380-386]`global`
16.[389-393]`live`



No comments:

Post a Comment