fredag 26 november 2010

kontrollsiffra personnummer oneliner in python

def pnr(nr,d=2,c=0,i=0): return (i==9) and 10-(c%10) or pnr(str(nr),3-d,c+(d*int(nr[i]))%10+(d*int(nr[i]))/10,i+1)



def pnr(p):
    a,p=[0,2,4,6,8,1,3,5,7,9],[int(i) for i in p]
    return 10-(a[p[0]]+p[1]+a[p[2]]+p[3]+a[p[4]]+p[5]+a[p[6]]+p[7]+a[p[8]])%10



print "sista siffran = %d" % pnr("6408233234"[:-1])

torsdag 25 november 2010

Setup IPSec between two hosts

# based on http://www.ipsec-howto.org/x304.html


key() ( bits=$1; dd if=/dev/random count=$((bits/8)) bs=1| xxd -ps )

host1=2001:16e8:ff4e::1
host2=2001:16e7:cd3c::223:77ff:fe18:1421

cat > setkey.conf <
#!/usr/sbin/setkey -f

# Configuration for $host1

# Flush the SAD and SPD
flush;
spdflush;

# set the authentication header
# AH SAs using 128 bit long keys
add $host2 $host1 ah 0x200 -A hmac-md5  0x$(key 128);
add $host1 $host2 ah 0x300 -A hmac-md5  0x$(key 128);

# ESP SAs using 192 bit long keys (168 + 24 parity)
add $host2 $host1 esp 0x201 -E 3des-cbc  0x$(key 192);
add $host1 $host2 esp 0x301 -E 3des-cbc  0x$(key 192);

# Security policies
spdadd $host2 $host1 any -P out ipsec
           esp/transport//require
           ah/transport//require;

spdadd $host1 $host2 any -P in ipsec
           esp/transport//require
           ah/transport//require;

EOF

sudo setkey -f setkey.conf

# copy setkey.conf to the other computer (host2),
# and on the other computer swap the '-P in' and '-P out'
# in the spdadd and run the sudo setkey -f setkey.conf
# WARNING! You will not be able to contact the other
# computer if only one of them has enabled the IPSec rules
# with setkey.

måndag 1 november 2010

multiple users on one screen session

I got this info from here.

Suppose larsr and bill have accounts on a machine stella.
Thanks to 'screen' they can easily share a terminal window.

both log in to stella (for instace with ssh).

larsr starts screen

  • screen

and enables multiuser mode

  • CTRL-a :multiuser on

and permit bill to connect

  • CTRL-a :acladd bill


Now bill can connect to the screen by typing

  • screen -x larsr/

Note the '/'. If larsr has many screens, the screen id is entered after the '/'.