Add DAG repo to CentOS 5

wget; rpm -ivh rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm

This is for 64bit

Postfix active queue status 2

time=10; numprev=0; while true; do for line in active ; do num=`find /var/spool/postfix/$line -type f 2>/dev/null | wc -l`; echo -n "$line - $num ($(($numprev-$num)) sent in ${time}sec) "; [ $(($numprev-$num)) -gt $time ] && echo "[$(($num/(($numprev-$num)/$time)/60))min to finish queue]" || echo "[very long]"; numprev=$num; done; sleep $time; done

Postfix active queue status

time=10; numprev=0; while true; do for line in active ; do num=`find /var/spool/postfix/$line -type f 2>/dev/null | wc -l`; echo -n "$line - $num ($(($num-$numprev))) "; [ $(($numprev-$num)) -gt $time ] && echo "[$(($num/(($numprev-$num)/$time)/60))min]" || echo "[very long]"; numprev=$num; done; sleep $time; done

SSH Autologin

ssh-keygen -t rsa; cat /root/.ssh/ | ssh remote.server "cat >> /root/.ssh/authorized_keys"

Run this from the client

Blocking pesty script kidies

while true; do iptables -t filter -F; (cat /var/log/apache2/access.log /var/log/apache2/access.log.1; zcat /var/log/apache2/access.log.*.gz) | egrep "msgimport||w00tw00t|nonexistenshit|GET http|CONNECT|mantis" | awk '{print $1}' | sort -u | while read line; do iptables -t filter -I INPUT -s $line --log-prefix '*fockers*' -j LOG; iptables -t filter -I INPUT -s $line -j DROP; done; echo -n "`date` --"; iptables -L -n | grep DROP | wc -l; sleep 60; done

This one is nice, long and very dirty:), it will scan the apache logs and block any IP that seems suspicious based on its requests

Cloning identical machines HD over SSH

dd bs=1M if=/dev/cciss/c0d0 | gzip -1 | ssh 'gunzip | dd bs=1M of=/dev/cciss/c0d0'

To see what is going on with the transfer on the same machine on a different console:
while true; do killall -USR1 dd; sleep 10; done (and check the original console)

Link monitor

j=0; d=( '/' '-' '\' '|' ); i=0; while true; do echo -en "${d[j++%4]}^[[D"; ping -W 1 -c 1 &> /dev/null && i=0 || echo -ne "$((++i))" > /dev/null; [ $i == 5 ] && i=0 && echo -e "`date` -- LINK DOWN" && ./internet &> /dev/null; sleep 0.5; done

Rotating cursor

d='-/|\' ; while (( t+=1 )); do echo -ne "\b${d:t%4:1}"; sleep 0.25; done

Remote Backup

ssh "tar -c -f - /var/twiki | gzip -" > /var/backup/twiki.back.`date +%Y%m%d`.tgz

View MBR

dd if=/dev/hda of=mbr.bin bs=512 count=1; od -xa mbr.bin;


dd if=/dev/sda bs=512 count=1 | od -xa

Find all active chroots

ls -d /proc/*/root | xargs -n1 readlink | grep -Fxv / | sort | uniq

ls -l conversion to chmod numeric notation

echo drwxr-x--- | tr "rwx" 1 | tr "-" 0 | sed "s#^.##" | while read -n 3 a; do echo "ibase=2; $a" | bc; done | xargs | sed "s# ##g"

for a simpler solution use stat.

create chown script (backup ownership)

ls -lad `find /var/hobbit/` | awk '{print "chown "$3"."$4" "$9}'


find /var/hobbit/ -printf 'chown %u.%g "%p"\n'

Windows telnet

(sleep 2; echo "username^M"; sleep 2; echo 'pasword^M'; sleep 2; echo "command1^M" sleep 2; echo "command2^M") | telnet

The ^M are enters (‘ctrl-v + enter’ when you type it in shell).

Unattended CLI Commands

(sleep 1; echo "select table_name from user_tables;"; sleep 1; echo "quit";) | sqlplus mozes/******@mozes

Sorted local mounts and memory on remote machine

ssh ptsl656 "df -lh | sort -t a -k 2 -n; free -m"

Set xterm title

PROMPT_COMMAND='echo -ne "\033]0;${HOSTNAME//.*} ${USER} ${PWD} \007"'

Mysql show command to bash vars

for lin in `echo "SHOW MASTER STATUS\G" | mysql 2>/dev/null | sed "1d; s#: #='#; s#^ *##; s# #_#g; s#\\\$#'#;"`; do eval $lin ; done

After running this line all the values are available as vars (try echo $Position)

Mysql dump for all tables, minimal resource usage, gzipped

ionice -c3 nice -n 19 mysqldump -A -R --skip-opt --add-drop-table --add-locks --create-options --disable-keys --extended-insert --single-transaction --quick --set-charset | nice -n 19 gzip -3 > /var/mysql_dump.gz

Requires ionice. On debian: apt-get install schedutils

Mysql dump for a specific table

/usr/local/mysql/bin/mysqldump -p<password> -R --opt -e -B <database> table

Row number for each table in a databse

for i in `echo "show tables" | mysql <database> | sed "1d"`; do c=`echo "select count(*) from $i" | mysql <database> | sed "s#(.*)##; 1d"`; printf "%-16s %s\n" $i $c; done

Mysql dump for all tables

/usr/local/mysql/bin/mysqldump -A -p<password> -R --opt -e

complete dump with all standard options

Timestamp to date — Simple

date -d @{timestamp}

Amazingly this feature is undocumented in the date manual as can be seen by running «PAGER=cat man date | grep ‘@’»

Timestamp to date, another option

date -d "1970-01-01 1184521826 sec GMT"

Converts 1184521826 timestamp to date

Standard date strings for 5 days back (perl for solaris)

/usr/local/bin/perl -e "use Time::ParseDate; print scalar(localtime(parsedate(scalar(localtime($NOW)))-(60*24*60*60)))";

Requires perl with Time::ParseDate

Standard date strings for 5 days back — Simpler (perl for solaris)

/usr/local/bin/perl "print scalar localtime time - 5*86400;"

Timestamp to date

echo $((`date +%s`-35*24*60*60)) | awk '{print strftime("%c",$1)}'

The date 35 days ago

date --date="35 day ago"

numeric only date syntax

date +%G%m%d%H%M%S
output: <year><month><day><hour><minute><second>, 20061206135408

Sed — shrink lines 06 Dec 2006, ShoKo

cat /u01/app/oracle/product/9.2.0/network/admin/tnsnames.ora | sed -n "/^$/{ :a; N; s#10\.0#10\.0#; T a; s#\n# #g; s#^ \([^ ]*\).*\(10.0.0.[0-9]*\).*#\1=\2#; p;}"

input file:

# TNSNAMES.ORA Network Configuration File: /u01/app/oracle/product/9.2.0/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
      (ADDRESS = (PROTOCOL = TCP)(HOST = = 1521))
      (SERVICE_NAME = mozes)
      (ADDRESS = (PROTOCOL = TCP)(HOST = = 1521))
      (SERVICE_NAME = mozes)

Stress the net

dd if=/dev/zero bs=1M | ssh somesite dd of=/dev/null

Send SMS via clickatell

curl '

IPs that made most requests

awk '{ x[$1]++; } END { for(k in x) {print x[k] " " k} }' var/log/httpd/access_log | sort -n | tail -10

All Local IPs

ifconfig -a | grep "inet addr" | sed "s#[^:]*:\([^ ]*\).*#\1#" | grep -v | xargs

Print specific values from CSV

ifs=$IFS; IFS='$'; sed "1d" nodes | while read -a w; do echo ${w[0]} ${w[4]} ${w[20]}; done; 

The csv is with $dollar delimiter

Get CSV var positions numbers by header 06 Dec 2006, ShoKo

head -1 nodes | tr "$" "\n" | cat -n

Tar whole file systems with exclusion over ssh

tar zvf - --one-file-system --preserve --exclude="LogAnalysisInfo" --exclude="" --create / /tmp /var /var/log | ssh "cat > /var/backup.mg1.tgz"

Also good for excluding remote mounts like NFS.

Tar a file list

rm f-sec.tar; rpm -q --filesbypkg f-secure-ssh-server-3.3.2-21.sles9 | awk '$2 !~ /\/usr\/share\/|rc\.d/ {print $2}' | xargs /usr/intel/bin/gtar rvf f-sec.tar

Check existing files from tar

for file in `tar tf f-sec.tar`; do [ -e "/$file" ] && echo "EXIST: /$file"; done

Find Dynamic files

find / -type f -cmin -60 | egrep -v "^/proc/|^/sys/" | sed "s#/[^/]*\$#/#g" | sort -u

Good starting point for finding dynamic files that should be on storage (EBS in EC2 case)

Compare local and remote directories

vimdiff <(find /var/www/ | sort) <(ssh remote "find /var/www/" | sort)&#91;/bash&#93;

Delete older than x days
&#91;bash&#93;find /etc/ -type f -mtime -x -exec rm {} \;&#91;/bash&#93;

Conditional operation if file is older than
&#91;bash&#93;&#91; $((`/bin/date +%s`-`/usr/bin/stat -c "%Y" /file`)) -ge 20 &#93; && ls -la /etc/&#91;/bash&#93;

Track high loads
&#91;bash&#93;&#91; `uptime | sed "s#.*: ##; s#,.*##; s#\..*##;"` -gt 10 &#93; && top -b -n1 &> /var/log/cpu.`date +%G%m%d%H%M%S`.log

Log full top when the load is high (good for cron)

Complete apache memory usage

memstat -w | egrep "`ps awux | grep apache | grep -v grep | awk '{print $2}' | xargs | tr ' ' '|'`" | awk '{gsub("k:",""); sum+= $1}; END {print sum"k"}'

This can be used to many other apache like procs

Cumulative CPU with indented child procs

ps awuxHS | less

Cumulative CPU, sorted by CPU usage

ps awuxS k -%cpu | less

Track a specific command execution information

while true; do echo; /usr/intel/bin/gtime -v ls -laR ~/ >/dev/null; for ((i=5;i--;i>0)); do echo -n "."; sleep 1; done; done


Comments are closed.