create a ssh alias

Add the lines below in .ssh/config file to create a new alias

vi .ssh/config

Host 7
  HostName 192.168.0.7
  User root

Connect using the alias

ssh 7

Generates configuration file entries for all IPs of a network

nmap -sP 192.168.0.0/24 | egrep -o '([0-9]{1,3}\.){3}[0-9]{1,3}' | sort -n -t . -k 4 | awk '{print "Host "substr( $1,13,3 )"\n  Hostname " $1"\n"}'
Advertisements

regex and pattern matching with awk

Filter rows that start with ‘r’ or ‘b’

awk '/^[rb]/' /etc/passwd

Filter rows that contain ‘G’ in the third column

df -h | awk '$3 ~ /G/'

Sum rows that contain ‘G’ in the third column

df -h | awk '$3 ~ /G/ { n+=$3 } END { print n }'

Filter rows containing ‘G’ in the third column and the first column start with ‘/dev/sd’ followed by the range of ‘a-z’

df -h | awk '$3 ~ /G/ { if( $1 ~ "/dev/sd[a-z]") print }'

Filter rows that have ‘a’ value in the first column and that contain ‘1’ in the second column

echo -e "a 1x\nb 2y" | awk '{ if ( $1 == "a" && $2 ~ /1/ ) print }'

Filter rows that have ‘httpd’ value in the second column and that contain value greater than 0 in columns 3 and 4

ps -A --sort -rss -o pid,comm,pcpu,pmem | awk '$2 ~ /httpd/ && ( ($3>0) || ($4>0) )'

replace field on certain line with awk

Replace second field in line that start with root string

awk -F':' '/^root/{sub($2, "abc")}1' /etc/passwd
awk -F':' '/^root/{sub($2, "abc"); print}' /etc/passwd
awk -F':' '/^root/{sub($2, "abc")}1' /etc/passwd > newFile
awk -F':' '/^root/{sub($2, "abc")}/^root/' /etc/passwd >> newFile

Replace second field in line that have root string in first field

awk -F':' '$1~"root$"{sub($2, "abc")}1' /etc/passwd
awk -F':' '$1~"root$"{sub($2, "abc"); print}' /etc/passwd

CPU, Memory Usage by Apache

APACHE PROCESSES THAT CPU OR MEMORY IS LARGER THAN 0

ps -A --sort -rss -o pid,comm,pcpu,pmem,lstart | grep httpd | awk '($3>0) || ($4>0)'

TOTAL CPU USED BY THE APACHE PROCESS

ps -A --sort -rss -o pid,comm,pcpu,pmem | grep httpd | awk '{n+=$3} END {print $2" total cpu: "n"%"}'

TOTAL MEMORY USED BY THE APACHE PROCESS

ps -A --sort -rss -o pid,comm,pcpu,pmem | grep httpd | awk '{n+=$4} END {print $2" total mem: "n"%"}'

TOTAL CPU AND MEMORY USED BY THE APACHE PROCESS

ps -A --sort -rss -o pid,comm,pcpu,pmem | grep httpd | awk '{x+=$3} {y+=$4} END {print $2" total cpu: "x"% mem: "y"%"}'

You can use server-status module to monitoring apache

lynx http://localhost/server-status?refresh=5