Bash hacks by example
From Ye Ole Stash
Prologue
Most of this is very old and was moved here for me to look back at all the wonderfully bad bash hacks I have done over the years. It also serves to show ways to quickly do things even if they are not always right ;)
Proceed with caution !
Simple one-liners
Find out a list of users and connection time
ac -p | sort -k 2 -gr
Quick way to find what processes are using something
find /proc -ls 2>/dev/null | grep $PATH_YOU_WANT_TO_FIND example find /proc -ls 2>/dev/null | grep "/home/vpopmail/domains"
Quick IPTABLES Firewall connections hack. Count | protocol | DESTIP:PORT | RemoteIP
/usr/sbin/iptstate -s | awk '{print $3,$2,$1}'| cut -d: -f1-2 | uniq -c | sort -g
Used this to run a script and then I did some BC math on the numbers to make sure MRTG was correct.
for i in `seq 1 2`; do /usr/sbin/disklatency2; sleep 60;done
Used this to move a large number of tar.gz files that were the same size
du -s *| grep .tar.gz | while read SIZE NAME; do if [ $SIZE == 4 ]; then mv $NAME backups/; fi; done
Used to quickly see which domains in /var/qmail/control/virtualdomains were a alias
sed s/:/\ \/g < /var/qmail/control/virtualdomains | while read line1 line2; do if [ "$line1" != "$line2" ]; then echo $line1 $line2;fi;done
Wow this is messy. Used this to find domains larger than 25 email accounts.
for i in `ls /usr/local/sites/`;do for d in `find /usr/local/sites/$i/.qmail* | wc -l`;do if [ $d -gt 25 ]; then echo $i >> /root/list4john; fi; done;done
Used this in a SNMP setup for a FS Inode check...messy and non-Posix I believe but it works in RHEL ;)
df -iP | grep / | while read A B C D E F;do echo -e "$A\nINODES\n$B\nIUsed\n$C\nIFree\n$D\nIUse\n$E"; done
Variables in conditionals are easy !
- This shows a negate of all $WHATEVER devices and finds all physical ethernet controllers (not ports!)
lspcioutput=$(lspci 2>/dev/null) if [ $(echo "$lspcioutput" | grep -v $WHATEVER| egrep "^[0-9]+\:[0-9]+\.0 Ethernet" | wc -l) -gt 1 ]; then echo "cool stuff";fi
or you can use awk if you dont like the "dual Grep action" above.
lspci | awk '{ if ( $0 ~ /^[0-9]+\:[0-9]+\.0 Ethernet/ && $4 !~ /$WHATEVER/) count++ } END {print count}'
Backgrounding in a for loop
for i in {1..6}; do /bin/ping -q -s1024 -f web01& done
or
for i in {1..6}; do { /bin/ping -q -s1024 -f web01&}; done
Slightly Longer Hacks
General Sysadmin fluff
- Script to create some email load Mailer_load.sh
- Script to read and format a list of websites in a directory WebsiteList.sh
- MRTG Graph creation using lynx and a for loop graphcreate.sh
- Check disk space and report with a email for each mounted filesystem over set amount diskspace.sh
- courier-authdaemon convert usernames of $domain-$number from .qmail files to MySQL DB (STILL IN DEVELOPMENT) courierauth_convert.sh
MySQL
- Backup all MySQL DBs using gzip mysqlbackup_gzip.sh
DNS (bind)
The "in theory this might be useful" scripts
- Interesting use for the $RANDOM function in bash pop3random.sh
- Listing all the deps for installed RPMs.
for r in $(rpm -qa); do yum -q deplist $r >> /tmp/deplist ; done
Even Longer Hacks
- A hack using incrond and a bash script to scan files automagicly when put in a folder with the SELinux on. autovirus_walkthrough or the bash script autovirus.sh