martes, 9 de julio de 2013

ruby code after bash pipe

ruby -n -a -e 'puts "first field: #{$F[0]}, whole line: #{$_}"'

viernes, 7 de junio de 2013

grep output only part of matching regex

$ echo 'ETag: "ae02fff4e2395982747582496ada225d"'
ETag: "ae02fff4e2395982747582496ada225d"
 

#I want just the ETag value:
$ echo 'ETag: "ae02fff4e2395982747582496ada225d"' |grep -oP '(?<=ETag: ")\w+(?=")'
ae02fff4e2395982747582496ada225d

Ref: http://unix.stackexchange.com/questions/13466/can-grep-output-only-specified-groupings-that-match

jueves, 9 de mayo de 2013

grep regex

grep -o 'whatever\([a-z_]\+\)whatever'

jueves, 17 de enero de 2013

sort -u like awk for few items and many dups

| awk '{a[$0]=1} END{for (k in a) print k}'

Performance test:
# create a file with few items and many dups
$ for i in {1..1000000}; do printf "a\nb\nc\n" >> few_items_many_dups.txt; done

# sort uniq using awk
$ time cat few_items_many_dups.txt | awk '{a[$0]=1} END{for (k in a) print k}' > few_items_many_dups.txt.awk_sortuniq
real    0m1.291s


# sort uniq using sort -u
$ time cat few_items_many_dups.txt | sort -u > few_items_many_dups.txt.sortuniq
real    0m36.726s

viernes, 25 de mayo de 2012

how-to get IP from hostname using dig

$ dig www.groupon.com +short | tail -n1
184.28.147.170
 
$ dig www.groupon.com +short
www.groupon.com.edgekey.net.
e5352.b.akamaiedge.net.
184.28.147.170

miércoles, 18 de abril de 2012

iptables config script with stateful filtering and logs

#!/bin/bash
#
# iptables configuration script

IPTABLES=/sbin/iptables

#
# Flush all current rules
#
$IPTABLES -F

#
# Allow SSH connections on tcp port 22
# This is essential when working on remote servers
# via SSH to prevent locking yourself out of the system

#
$IPTABLES -A INPUT -p tcp --dport 22 -j ACCEPT

#
# Allow HTTP connections on tcp port 80 from 10.20.30.40
#
$IPTABLES -A INPUT -s 10.20.30.40 -p tcp --dport 80 -j ACCEPT

#
# Set default policies for INPUT, FORWARD and OUTPUT chains
#
$IPTABLES -P INPUT DROP
$IPTABLES -P FORWARD DROP
$IPTABLES -P OUTPUT ACCEPT
#
# Set access for localhost
#
$IPTABLES -A INPUT -i lo -j ACCEPT
#
# Accept packets belonging to established and related connections
#
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT


# Log drops to /var/log/messages
LOGLIMIT="2/s"
$IPTABLES -N LOGDROP
$IPTABLES -A LOGDROP -p tcp -m limit --limit $LOGLIMIT -j LOG --log-prefix "TCP LOGDROP: "
$IPTABLES -A LOGDROP -p udp -m limit --limit $LOGLIMIT -j LOG --log-prefix "UDP LOGDROP: "
$IPTABLES -A LOGDROP -p icmp -m limit --limit $LOGLIMIT -j LOG --log-prefix "ICMP LOGDROP: "
$IPTABLES -A LOGDROP -f -m limit --limit $LOGLIMIT -j LOG --log-prefix "FRAGMENT LOGDROP: "
$IPTABLES -A LOGDROP -j DROP

$IPTABLES -A INPUT -p icmp -i eth0 -j LOGDROP
$IPTABLES -A INPUT -p tcp -i eth0 -j LOGDROP
$IPTABLES -A INPUT -p udp -i eth0 -j LOGDROP




#
# In case you want to save these settings
#
# /sbin/service iptables save
#

#
# List rules
#
$IPTABLES -L -v


ref:
http://www.linuxquestions.org/questions/linux-security-4/iptables-logging-385165/
http://www.centos.org/docs/5/html/5.1/Deployment_Guide/s1-iptables-saving.html

lunes, 9 de abril de 2012

float arithmetic

laptop:~ pablo$ python

CASE 1:

>>> a = 987654321123456.1
>>> a
987654321123456.1
>>> (a*20 - (a-1)*20)/20
1.0
>>> (a*100 - (a-1)*100)/100
0.96
>>> (a*1000 - (a-1)*1000)/1000
1.024
>>> (a*10000 - (a-1)*10000)/10000
1.024

CASE 2:

>>> a = 987654321123456.1
>>> b = 987654321123455.2
>>> a-b
0.875
>>> a
987654321123456.1
>>> b
987654321123455.2