Codesnippets

Some useful hacks and snippets



www.net-tex.de dumpsh dump.sh download source
a small shell-script that dumps a filesystem determining the level by the
current date (it uses level 0 in the 1.st week of month, 1 else), encrypts
the file, wipes out the unencrypted dump and 'scp's it to a remote host. sslenc dir-enc.sh download source
This skript en-/decrypts the files placed in another directory with OpenSSL
stefan@AragornOfArathorn {56} ls test/
dmes       dmm.1.txt  l          ll         uname      upt
stefan@AragornOfArathorn {57} ./dir-enc.sh e test/ passwd
files encrypted and originals removed
stefan@AragornOfArathorn {58} ls test/
dmes.enc      dmm.1.txt.enc l.enc         ll.enc        uname.enc     upt.enc
stefan@AragornOfArathorn {59} ./dir-enc.sh d test/ passwd
files decrypted
stefan@AragornOfArathorn {60} ls test/
dmes       dmm.1.txt  l          ll         uname      upt
stefan@AragornOfArathorn {61} 
lilo lilo.pl download source
a PERL-script that reads in a file and prints it to stdout linewise
with a given pause between each line

I use it on my VAX on roadshows or with XScreensavers phosphor-module.
stefan@AragornOfArathorn {633} ./lilo.pl -h
lilo.pl -v        : show version
lilo.pl -h        : show this   
lilo.pl foo.bar   : reads foo.bar in and prints it linewise out 
                    with a standard pause of 1 sec between each line
lilo.pl foo.bar 3 : reads foo.bar in and prints it linewise out 
                    with a pause of 3 sec
signpic picsic.pl download source
Sometimes I decide to publish fotos I've taken by digicam on my HP, but I'm
to lazy to add a kind of sign to each pic, so I created a small skript
solving the task for me.

First I created a transparent GIF containing my sign the following
way with GIMP: [FILE] -> [NEW] -> [FILL TYPE] -> [TRANSPARENT]
Saved it and now I use picsig.pl to add that GIF to the lower right corner
of each JPG located in the current dir.
Requires 'identify' and 'composite' of ImageMagick package (/usr/pkgsrc/graphics/ImageMagick/)
stefan@AragornOfArathorn {222} ./picsig.pl -h
picsig.pl -v            : show version
picsig.pl -h            : show this   
picsig.pl foobar.gif    : add foobar.gif to each *.jpg at the lower right corner
                          writing the result to signed-*
stefan@AragornOfArathorn {223} 
alb album.pl download source
uses ImageMagick 'convert' to create thumbnails of given size and creates a
HTML gallery as table with the thumbnail, file size and filename as link.
An example gallery can be found here
pkgdb pkg_db.pl download source
creates 3 HTML files, 1 containing a list of all installed pkgs
linked to one with the detailed description of the pkg and one
with a list of installed files
dblrem dblrem.pl download source
NAME
        dblrem.pl - remove duplicate files using a MD5 hash
SYNOPSIS
        dblrem.pl

DESCRIPTION

dblrem.pl creates cryptographic MD5 hashes over 
filecontents and deletes duplicate files
Example:
 
stefan@AragornOfArathorn {136} ls -la | wc -l
   17315
stefan@AragornOfArathorn {137} time ./dblrem.pl 

real    2m1.055s
user    0m52.437s
sys     1m15.175s
stefan@AragornOfArathorn {138} ls -la | wc -l
      12
stefan@AragornOfArathorn {139} 
gpgrecv gpgrecv.pl download source
Imports all PGP Keys that are used as signature for another key,
but not found at the keyring.
Requires a Keyserver to be set in ~/.gnupg/options xferlog xferlog.pl download source
A *very* dirty hack to analyze the 'xferlog' logfile of a NetBSD FTP Server.
stefan@AragornOfArathorn {27} ./xferlog.pl 
anonymous logins             1375 
downloaded files             3287   
downloaded bytes         7452354548  
downloaded Kbytes        7277689.98828125
downloaded Mbytes        7107.11912918091
downloaded Gbytes        6.94054602459073
ent entropy.sh a way to get some random data:
 while true ; do dd if=/dev/urandom bs=1200 count=1 2>/dev/null | sha1 ; done
per substition of text in multiple files substitutes "new" (case insensitive) in each html-File with "old"
find daten/www/ -newer index.html -name "*.html" -printx | xargs perl -i -pe 's/new/old/gi'
con convert images in a directory resize each JPG in this directory to 800*600 with ImageMagick convert
for i in *.jpg
do
	convert -size 800x600 -resize 800x600 $i $i.2
	mv  $i.2 `basename $i.2 .2`
done	
load load-runner.sh Sometimes I have to check if a machine's Hardware (esp. CPU and/or RAM) is OK. I use prime95 (it generates primes and can do some torture testing) and additionally the following shellskript.
It writes 50MB of data from /dev/urandom to a file, generates a SHA1 hash over the file, encrypts and decrypts it with OpenSSL and writes a 2nd SHA1 hash over the decrypted file. So you'll get two lists of hashs which should be equal if your machine is OK, FUBARed hardware usually results in problems with en-/decrypting files. I suggest to generate a load of at least 4, so on more sophisticated machines I do some dd if=/dev/urandom of=/dev/null &
#!/bin/ksh
typeset -i cnt=1
while [[ $cnt -le 5 ]] 
do
	dd if=/dev/urandom of=temp.wipe bs=1M count=50 2>/dev/null
	sha1 temp.wipe >> temp.sha1.1
	openssl des3 -in temp.wipe -out temp.wipe.enc -e -salt -k passwd 
	rm -Prf temp.wipe
	openssl des3 -d -in temp.wipe.enc -out temp.wipe -k passwd
	rm -Prf temp.wipe.nc
	sha1 temp.wipe >> temp.sha1.2
	cnt=cnt+1
done
diff temp.sha1.1 temp.sha1.2
[ $? -eq 0 ] && { echo "No Error"; } || echo "Errors found!";
rm temp.wipe.enc
rm temp.wipe
silcd starting a silc-daemon bound to a dynamic IP There is no way to bind silcd to a dyn IP (eg. a DSL-PPP connection), so I configured and copied the conf /usr/pkg/etc/silcd/silcd.conf to /usr/pkg/etc/silcd/silcd.conf.dynip and manipulated "Secondary" ("Primary" is my local 192.168.2.1) that way:
Secondary { ip = "dynip"; port = 706; };
now my skript pipes silcd.conf.dynip through sed, which substitutes "dynip" to my IP address of ppp0 using the backticks calling ifconfig and using awk to reveal the IP. Maybe you have to tweak it a little bit for other OS.
cat /usr/pkg/etc/silcd/silcd.conf.dynip | sed s/dynip/`ifconfig ppp0 | 
awk '$0 ~ /inet / {print  $2}'`/ > /usr/pkg/etc/silcd/silcd.conf
silcd
www logfile analyzer a small shellskript to analyze logfiles of my university www-account:
for i in `awk '{print $5}' *|sed s/'\/steschum\/'// | sort|uniq`; 
do cat * | grep $i| awk '{summe+=$1;groesse+=$3} END {printf summe"        "groesse"       "}';echo $i ;
done|sort -n

net-tex.de, Index
$Id: index.html,v 1.45 2014/08/21 09:24:33 stefan Exp $ 
Autor: Stefan Schumacher, stefan@net-tex.de, PGP-Key 0xB3FBAE33
für net-tex.de/cryptomancer.de

Bitte beachten Sie, das die Seite inhaltlich seit Ende 2007 nicht mehr gepflegt wird!
Aktuellere Informationen erhalten Sie auf Kaishakunin.com