Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

Tuesday, June 19, 2012

Kill annoying processes that match a pattern

So, there was a bunch of annoying processes named wit a pattern and I wanted to kill all of them

Here is the script (I know this can be done in a much cleaner way in awk, but I like this way)


#!/bin/sh
P="master(-worker)?"

PS=/bin/ps
SED=/bin/sed
KILL=/bin/kill
GREP=/bin/grep
CUT=/usr/bin/cut

$KILL `$PS ax | $GREP -E $P | $GREP -v grep | $SED -e 's/^\ \+//g' | $CUT -d ' ' -f 1 | $GREP -E '^[[:digit:]]+'`

--
  = ^ . ^ =

Thursday, March 22, 2012

Delete temp files and directories

% while sleep 0.1 ; do if [ $(($RANDOM % 2)) -eq 0 ] ; then rmdir -v `mktemp -d` ; else rm -v `mktemp` ; fi ; done ; 


--
  = ^ . ^ =

Wednesday, March 14, 2012

show my ip address


#!/bin/sh

IP=/bin/ip
SED=/bin/sed
CUT=/usr/bin/cut

IF=en0

if [ ! -z ${1} ]
then
  IF=${1}
fi

$IP addr show dev $IF | $SED -n 3p | $SED -e 's/\ \+/\ /g' -e 's/\/.*$//g' | $CUT -d ' ' -f 3

--
  = ^ . ^ =

Thursday, December 1, 2011

Monitoring my network traffic

Today I wrote this simple script to monitor my network traffic. I release this script under GPLv3

Enjoy

#!/bin/sh
# monitor-traffic.sh - Monitor network traffic excluding common requests
# Andres Hernandez - Tonejito

TCPDUMP=/usr/sbin/tcpdump
IP=/sbin/ip
DEV=en1
ADDR=`$IP addr show dev $DEV | grep 'inet ' | cut -d ' ' -f 6 | cut -d '/' -f 1`

$TCPDUMP -ni $DEV "host $ADDR and port not (67 or 68 or 80 or 443 or 1863 or 5222 or 587 or 993 or 995)"

--
= ^ . ^ =

Tuesday, November 15, 2011

zerofree - Zero out space on a virtual machine

Today, I had to shrink a virtual machine size for deployment.

I like the zerofree utility that clears the unallocated blocks in a ext2/ext3/ext4 filesystem.

Take a look at the code snipplet

First go to single user

# init 1

Then do the magic

# for i in `mount | grep sda | grep ext | cut -b 9` ; do mount -o remount,ro /dev/sda$i && zerofree -v /dev/sda$i && mount -o remount,rw /dev/sda$i ; done ;

And here is a sample on a VirtualBox VM



--
= ^ . ^ =