Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Thursday, May 17, 2007

Jugando con IPv6 (apto para todo IPv4-público ;)

Dale ... festejemos el día de Internet usando lo que es y será su pegamento futuro: IPv6.
Ingredientes: tan sólo 1 IP pública
Resultado: estemmm ... exactamente 1208925819614629174706176 (2^80 ;) direcciones IPv6, las cuales han estado ahí desde hace tiempo esperándote :-P
Para saborear más aún: end-to-end para las máquinas que quieras ... como en los buenos viejos tiempos, por ej. podrás hacer desde "afuera":
ssh -6 flamanteIPv6_de_esa_PC_que_no_tiene_IP_publica


El mecanismo se llama tunneling 6to4, está descripto por doquier, en particular yo escribí un mini artículo para el seminario de IPv6 que dimos en el 2005 en la UM.

Podés probar usando mi script: ipv6-setup6to4.sh , el cual sólo muestra los comandos necesarios (es decir: inofensivo :-), por ejemplo con una dir. IPv4= 65.1.2.3 da como salida:

bash$ ./ipv6-setup6to4.sh
IP4_ADDR=65.1.2.3
IP6TO4_PREF=2002:4101:203
#check you allow ipv6 encap: iptables -I INPUT -p 41 -d 65.1.2.3
ip tunnel add tun6to4 mode sit remote any local 65.1.2.3 ttl 64
ip addr flush dev tun6to4 2>/dev/null
ip link set dev tun6to4 up
ip addr add 2002:4101:203::1/16 dev tun6to4
ip route add ::/96 dev tun6to4
ip route add 2000::/3 via ::192.88.99.1 dev tun6to4 metric 1
#you may do something like: ip -6 addr add 2002:4101:203:0001::1/64 dev eth0

#NOTHING done, use me as: ./ipv6/ipv6-setup6to4.sh |sudo sh -x


Que lo disfrutes!

Sunday, March 04, 2007

[en] powerful arrays in bash

Doing some experiments with advanced bash array features. I created a bash implementation of insertion sort algorithm.

UPDATE: This example is already part of the great Advanced Shell Scripting Guide :)


Enjoy :-D




#!/bin/bash
# insertion-sort.bash.sh: Insertion sort implementation in bash
# Heavy use of bash array features: slicing, merging, etc
# URL: http://www.lugmen.org.ar/~jjo/jjotip/insertion-sort.bash.d/insertion-sort.bash.sh
#
# Author: JuanJo Ciarlante jjo \O/ irrigacion gov ar
# License: GPLv2
#
# Test with: ./insertion-sort.bash.sh -t
#

: ${DEBUG:=1} # debug, override with: DEBUG=1 ./scriptname ..

# Global array: "list"
typeset -a list
# Load whitespace separated numbers from just stdin 1st line
if [ "$1" = "-t" ];then
read -a list < <(od -An -w32 -t u2 /dev/urandom )
else
read -a list
fi
numelem=${#list[*]}

# Shows the list, marking the element whose index es $1 by surrounding it with
# the two chars passed as $2; whole line prefixed with $3
showlist() { echo "$3"${list[@]:0:$1} ${2:0:1}${list[$1]}${2:1:1} ${list[@]:$1+1}; }

# loop "pivot" from 2nd elem, to end of list
for((i=1;i<numelem;i++))do
((DEBUG))&&showlist i "[]" " "
# From current "pivot", back to 1st elem
for((j=i;j;j--))do
# search for the 1st elem less than current "pivot" ...
[[ "${list[j-1]}" -le "${list[i]}" ]] && break
done
((i==j)) && continue ## no insertion was needed for this element
# ... move list[i] (pivot) to the left of list[j]:
list=(${list[@]:0:j} ${list[i]} ${list[j]} ${list[@]:j+1:i-(j+1)} ${list[@]:i+1})
# {0,j-1} {i} {j} {j+1,i-1} {i+1,last}
((DEBUG))&amp;amp;amp;&showlist j "<>" "*"
done
echo $'Result:\n'${list[@]}