Archief

Archief voor de ‘Linux Tips & Trics’ Categorie

SMS to Twitter

31 maart 2010 Reacties uit

I’ve wrote this shell script that uses an SMS to post to Twitter. It is ment to be used as an Eventhander for SMS Server Tools, but it’s simple enough for stand alone usage with simple text files.

If used as an Eventhandler the arguments are “RECEIVED” and the path to the SMS file, add “eventhandler=/path/to/sms2twitter.sh” to your smsd.conf. Without SMS Server Tools you can also use a simple file for posting to twitter:
Create a file starting like this:

  1. Phone: 0634567890
  2.  
  3. Your tweet in max. 140 characters

Then call this script with the path to your file:

  1. sms2twitter.sh /path/to/file.txt

You will need to create a sqlite3 DB with this schema:

  1. sqlite3 sms2twitter.db \
  2.     "CREATE TABLE account (phone, username, password, PRIMARY KEY(phone))"

For every Phone that you allow to post Tweets thru SMS you need to run:

  1. sqlite3 sms2twitter.db \
  2.     "INSERT INTO account VALUES (‘phonenumber’, ‘username’, ‘password’)"

Lees meer…

Categorieën:Linux Tips & Trics Tags:, , ,

Bash script to read XMail boxes with Mutt

1 oktober 2007 Reacties uit

For Postmasters: easy way to read a mailbox from your XMail spool. If you have a CDB file (see this post), it is also capable of looking up aliases.

  1.  
  2. readmail real.user@domain.org
  3. #or:
  4. readmail an.alias@domain.org
  5. #or if your RootDomain is domain.org:
  6. readmail an.alias

 

  1.  
  2. #!/bin/bash
  3. XMAILROOT="/var/lib/xmail"
  4. CDB="/var/lib/qpsmtpd/rcptto/validrcptto.cdb"
  5.  
  6. RootDomain=$(grep RootDomain $XMAILROOT/server.tab | awk ‘{print $2}’|sed ‘s/"//g’)
  7. aliaslookup=1
  8. [ -f "$CDB" ] || {
  9.   echo "warning: CDB file not found, alias lookup disabled"
  10.   aliaslookup=0
  11. }
  12. spool="$XMAILROOT/domains/"
  13. if [ -z "$1" ]; then echo usage: $0 user@domain; exit; fi
  14. user=$(echo $1|cut -d@ -f1);
  15. domain=$(echo $1|cut -d@ -f2)
  16. [ "$domain" == "$user" ] && domain=$RootDomain
  17.  
  18. if [ ! -d "$spool/$domain" ]; then
  19.   echo "$domain: no such domain in $spool"
  20.   exit 8
  21. fi
  22.  
  23. if [ ! -d "$spool/$domain/$user" ]; then
  24.   if [ $aliaslookup -eq 1 ]; then
  25.     echo -n "$user@$domain not found, looking for alias: "
  26.     real=$(cdb -qm $CDB $user@$domain)
  27.     if [ $real == "" ];then
  28.       echo "not found"
  29.       exit 8
  30.     else
  31.       echo "using $real"
  32.       user=$(echo $real|cut -d@ -f1)
  33.     fi
  34.   else
  35.     echo "$user: no such user in domain $domain"
  36.     exit 9
  37.   fi
  38. fi
  39.  
  40. mutt -f $spool/$domain/$user/Maildir
Categorieën:Linux Tips & Trics Tags:,

XMail to CDB-database for use with qpsmtpd

1 oktober 2007 Reacties uit

This scripts creates a Constant DataBase file. We use this to interact with the qpsmtpd plugin check_validrcptto_cdb.

  1.  
  2. #!/bin/bash
  3.  
  4. #this script creates a cdb file from xmail users and their aliases
  5.  
  6. OUTPUTFILE=/var/lib/qpsmtpd/rcptto/validrcptto.cdb
  7. XMAILROOT=/etc/xmail
  8.  
  9. #no more settings beyond this point
  10.  
  11. TMPFILE=$(mktemp /tmp/email2cdb.XXXXXXXXXX)
  12.  
  13. [ -d "$(dirname $OUTPUTFILE)" ] || {
  14.     echo "directory for OUTPUTFILE $OUTPUTFILE does not exists"
  15.     exit 3
  16. }
  17.  
  18. [ -f "$TMPFILE" ] && rm $TMPFILE
  19. touch $TMPFILE || {
  20.     echo "can not create $TMPFILE"
  21.     exit 9
  22. }
  23.  
  24. #paranoia strikes again:
  25. #chmod og-r $TMPFILE
  26.  
  27. #list all email boxes:
  28. cat $XMAILROOT/mailusers.tab \
  29.     | sed ‘s/"//g’ \
  30.     | awk ‘{print $2 "@" $1 "\t" $2 "@" $1}’ \
  31.     | sed ‘s/^*//’ \
  32.     | sed ‘s/*//’ >> $TMPFILE
  33.  
  34. #list aliases
  35. cat $XMAILROOT/aliases.tab \
  36.     | sed ‘s/"//g’ \
  37.     | awk ‘{print $2 "@" $1 "\t" $3 "@" $1}’ \
  38.     | sed ‘s/^*//’ \
  39.     | sed ‘s/*//’ >> $TMPFILE
  40.  
  41. #create cdb file
  42. cdb -cm $CDBOPTIONS  $OUTPUTFILE $TMPFILE || {
  43.     echo "can not create cdb file $OUTPUTFILE FROM $TMPFILE"
  44.     echo "WARNING: I did not remove $TMPFILE for debug purposes"
  45.     exit 9
  46. }
  47.  
  48. #cleanup:
  49. rm $TMPFILE
  50. exit 0
Categorieën:Linux Tips & Trics Tags:

KDE dialog for crypysetup

1 oktober 2007 Reacties uit

Here is a bash script that uses kdialog to present a password prompt and tries to mount a crypt device. It interacts with the (k)ubuntu “cryptsetup” package.

  1.  
  2. #!/bin/bash
  3. if [ -r /lib/cryptsetup/cryptdisks.functions ]; then
  4.   . /lib/cryptsetup/cryptdisks.functions
  5. else
  6.   kdialog –title $(basename $0) –error "/lib/cryptsetup/cryptdisks.functions not found"
  7.   exit 1
  8. fi
  9.  
  10. crypt_create (){
  11.   DLG_TITLE="Cryptsetup Dialog"
  12.   mountpoint=$(grep $MAPPER/$1 /etc/fstab | awk ‘{FS=" "}{print $2}’)
  13.   sudo cryptsetup status $1 > /dev/null
  14.   if [ $? -ne 0 ]; then
  15.     pwd=$(kdialog –title "$DLG_TITLE" –password "mapping $1 to device <$2>
  16.  
  17. Enter passphrase:")
  18.     if [ $? -ne 0 ]; then return 3; fi
  19.     if [ "$pwd" == "" ]; then
  20.       kdialog –title "$DLG_TITLE" –warningyesno "empty password, try again?" && {
  21.         crypt_create $1 $2
  22.         return 2
  23.       }
  24.     fi
  25.     echo "$pwd" | sudo cryptsetup create $1 $2
  26.     mount $mountpoint 2&>/dev/null|| {
  27.       sudo cryptsetup remove $1
  28.       kdialog –title "$DLG_TITLE" –warningyesno "cryptsetup create <$1> <$2> failed, try again?" && {
  29.         crypt_create $1 $2
  30.         return 2
  31.       }
  32.       return 0
  33.     }
  34.   else
  35.     if [ -z $3 ]; then
  36.       kdialog –yesno "$MAPPER/$CRYPT_NAME is active, do you want to deactivate?" && {
  37.         umount $mountpoint
  38.         sudo cryptsetup remove $1
  39.       }
  40.     fi
  41.   fi
  42.   return 1 #cryptdevice already setup
  43. }
  44.  
  45. if [ -r $TABFILE ]; then
  46.   grep -Ev "^#" $TABFILE | while read line; do
  47.     cryptname=$(echo $line | awk ‘{print $1}’)
  48.     cryptdevice=$(echo $line | awk ‘{print $2}’)
  49.     crypt_create $cryptname $cryptdevice $1
  50.   done
  51.   exit 0
  52. else
  53.   kdialog –title $(basename $0) –error "$TABFILE not readable"
  54.   exit 2
  55. fi
  56.  

PostgreSQL 8.2 with tsearch2 and Dutch Snowball stemmer

13 juni 2007 Reacties uit

quick walk-through for compiling PostgreSQL with tsearch2 full text extension and Dutch Snowball stemmer on Debian Etch:

  1. sudo su -
  2. cd /usr/src
  3. apt-get build-deps postgresql-8.1
  4. wget ftp://ftp4.nl.postgresql.org/postgresql.zeelandnet.nl/latest/postgresql-8.2.4.tar.bz2
  5. tar jxvf postgresql-8.2.4.tar.bz2
  6. cd postgresql-8.2.4
  7. wget http://www.sai.msu.su/~megera/postgres/gist/tsearch/V2/tsearch_snowball_82-20070504.gz
  8. gunzip tsearch_snowball_82-20070504.gz
  9. patch -b -p0 < tsearch_snowball_82-20070504
  10. ./config
  11. make
  12. make install
  13. cd contrib
  14. make
  15. make install
  16. cd tsearch2/gendict
  17. wget http://snowball.tartarus.org/algorithms/dutch/stem.c
  18. wget http://snowball.tartarus.org/algorithms/dutch/stem.h
  19. ./config.sh -n nl -s -p dutch_ISO_8859_1 -v -C‘Dutch Stemmer. Snowball’
  20. cd ../../dict_nl
  21. make
  22. make install
  23. wget http://snowball.tartarus.org/algorithms/dutch/stop.txt -O /usr/local/pgsql/share/contrib/dutch.stop
  24. adduser postgres
  25. mkdir /usr/local/pgsql/data
  26. chown postgres /usr/local/pgsql/data
  27. su – postgres
  28. /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
  29. /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >/tmp/logfile 2>&1 &
  30. /usr/local/pgsql/bin/createdb tsearch2
  31. /usr/local/pgsql/bin/psql tsearch2 < /usr/local/pgsql/share/contrib/tsearch2.sql
  32. /usr/local/pgsql/bin/psql tsearch2 < /usr/local/pgsql/share/contrib/dict_nl.sql
  33. /usr/local/pgsql/bin/psql tsearch2

If everything went well you are now in a PostgreSQL prompt.

We will now update ans insert some stuff:

  1. UPDATE pg_ts_dict SET dict_initoption=‘contrib/dutch.stop’ WHERE dict_name=‘nl’;
  2. INSERT INTO pg_ts_cfg (ts_name, prs_name, locale) VALUES (‘dutch’, ‘default’, ‘nl_NL’);
  3. INSERT INTO pg_ts_cfgmap (SELECT ‘dutch’, tok_alias, dict_name FROM pg_ts_cfgmap WHERE ts_name=‘default’);
  4. UPDATE pg_ts_cfgmap SET dict_name=‘{nl}’ WHERE ts_name=‘dutch’ AND dict_name=‘{en_stem}’;
select to_tsvector('dutch', 'ik ga naar school');    to_tsvector
------------------
'ga':2 'schol':4
(1 row)
Categorieën:Linux Tips & Trics Tags:

Quanta Documentation

11 februari 2007 Reacties uit

I’ve created php5 and mysql5 documentation packages for Quanta. They include an installer and come in several languages. I’m working on PHP:PEAR Documentation. Download here …

Categorieën:Linux Tips & Trics Tags:

Mysql character set

26 januari 2007 Reacties uit

Today I had this problem with a MySQL/php application:

Illegal mix of collations (latin1_bin,IMPLICIT) and (latin1_swedish_ci,IMPLICIT)

I’ve created this loop to solve the problem, because the server contains several database with lots of tables:

  1. passwd="yeah right!"
  2. mysql="mysql -u root -p"$passwd" -ss -B -e"
  3. $mysql "show databases"|while read db; do
  4. $mysql "SHOW TABLES FROM $db"|while read table; do
  5. if [ "$($mysql "show create table $db.$table"|grep -c latin1_bin)" != "0" ]; then
  6. $mysql "ALTER TABLE $db.$table CONVERT TO CHARACTER SET  latin1"
  7. fi
  8. done
  9. done
Categorieën:Linux Tips & Trics Tags:

WAN IP address to clipboard (KDE only!)

30 oktober 2006 Reacties uit

Often I want to copy/paste my WAN IP address, here is a shellscript for it (warning: kde only, requires wget and sed, no error detecting whatsoever!)

  1. #!/bin/bash
  2. ip=$(wget -q -O – http://checkip.dyndns.org/ | sed ‘s/.+ (([[:digit:]]{1,3}.){3}[[:digit:]]{1,3}).+/1/’)
  3. dcop klipper klipper setClipboardContents $ip
  4. echo $ip
Categorieën:Linux Tips & Trics Tags:

Mailsplitter

28 oktober 2006 Reacties uit

We use Spamassassin as anti spam tool with Bayes learning. If I want to feed spam massages to the sa-learn tool, I have to get individual messages from my Thunderbird Mailfolder which stores all mails together in a text file.

First I move alle spam massages to the Thunderbird Junk mail. Thunderbird has a build in learning tool to fight spam, so most spam messages are moved to the junk folder automagically. Make sure there are no messages in this folder that are already marked as spam by Spamassassin!
Split a Thundebird mailfolder into individual Mails:

  1. targetfolder=/tmp/spam
  2. if [ ! -d "$targetfolder" ]; then
  3. echo "targetfolder $targetfolder does not exists";
  4. exit 1;
  5. fi
  6. m=0
  7. find ~/.mozilla-thunderbird -type f -name Junk|while read junkfile; do
  8. i=0
  9. for line in `grep -n "From – " "$junkfile" | cut -d: -f1`; do
  10. if [ $i -gt 0 ]; then
  11. head "$junkfile" -n $(echo $line-1|bc) | tail -n $(echo $line-$cl|bc) > $targetfolder/msg$(printf "%09d" $m).eml
  12. fi
  13. let i=$i+1
  14. let m=$m+1
  15. cl=$line
  16. done
  17. done
Categorieën:Linux Tips & Trics Tags:

CD/DVD layout maker

5 juli 2006 Reacties uit

Sometimes you have to write lots of CD’s/DVD’s of even more files. This scripts loops through files in a target directory and creates new “virtual disks” and places symbolic links in it to the found file. If a “virtual disk” is full it will create another one. You can define your own pattern for the disknames.

#!/bin/bash

# sizes in Megabytes
CDSIZE=750
DVDSIZE=4380

if [ "$(basename $0)" == "make-dvd" ]; then
let "DISKSIZE=$DVDSIZE*1024*1024"
else
let "DISKSIZE=$CDSIZE*1024*1024"
fi

VERBOSE=1
usage ()
{
echo "usage: $0 [OPTIONS] source target, where options can be:"
echo "  -s START      number of 1st disk"
echo "  -v            print usefull messages"
echo "  -V            print usefull and less usefull messages"
echo "  -d BYTES      disksize of the writable media (default $DISKSIZE)"
echo "  -r            puts all symlinks in corresponding directory"
echo "                without this option all symbolic links are placed directly under disk"
echo "  -p            pattern to use for diskname (default DISK_%03d), see man printf"
echo "                example pattern: \"MY_CD_%03d\" for MY_CD_001, MY_CD_002, etc ..."
if [ "$1" != "" ]; then
echo "ERROR: $1"
exit 9
fi
exit 8
}

myprint ()
{
if [ "$VERBOSE" == "$1" ]; then
echo $2;
fi
}

integertest ()
{
stringTest=$(echo $1 | sed 's/[0-9]+//g');
if [ ${#stringTest} -gt 0 ]; then
usage "argument for $2 must be an integer"
fi

}

if [ $# -lt 2 ]; then usage; fi

START=1
VERBOSE=0
INTREE=0
PATTERN="DISK_%03d"

while getopts ":s:t:p:rvVd:" Option; do
OPTARG=$(echo $OPTARG | sed 's/^W+//')
case $Option in
s ) START=$OPTARG;;
v ) VERBOSE=1;;
V ) VERBOSE=2;;
r ) INTREE=1;;
d ) DISKSIZE=$OPTARG;;
p ) PATTERN=$OPTARG;;
* ) usage "unkown option";;
esac
done
shift $(($OPTIND - 1))

SOURCE=$(cd "$1" && pwd)
TARGET=$(cd "$2" && pwd)
integertest $START "-s"
integertest $DISKSIZE "-d"

DISKNAME=$(printf $PATTERN $START)
if [ $? -gt 0 ]; then usage "wrong argument for pattern"; fi

if [ ! -d "$SOURCE" ]; then usage "source \"$SOURCE\" is not a directory"; fi
if [ ! -d "$TARGET" ]; then usage "target \"$TARGET\" is not a directory"; fi
if [ ! -w "$TARGET" ]; then usage "target directory \"$TARGET\" is not writable"; fi

OLDIFS=$IFS
IFS=:
CURRENTSIZE=0

FIRSTRUN=1
echo -n "fetching filelist from $SOURCE ... "
for file in $(find -L $SOURCE -type f -printf "%P$IFS"); do
if [ "$FIRSTRUN" == "1" ]; then
echo "done"
myprint 1 "making 1st disk $DISKNAME"
if [ -d "$TARGET/$DISKNAME" ]; then usage "can not make disk $TARGET/$DISKNAME"; fi
mkdir -p $TARGET/$DISKNAME
FIRSTRUN=0
fi

filesize=$(du -b "$SOURCE/$file" 2> /dev/null | sed 's/W+.*//')
myprint 2 $(printf "%s [%10d - %10d] %s" $DISKNAME $CURRENTSIZE $filesize $file 2>/dev/null)
if [ "$filesize" == "" ]; then echo "can not get filesize of file $SOURCE/$file, skipping it"; continue; fi
let "newsize=$CURRENTSIZE+$filesize"
if [ $newsize -gt $DISKSIZE ]; then
let "START=START+1"
DISKNAME=$(printf $PATTERN $START)
myprint 1 "disk full, making new one: $DISKNAME"
CURRENTSIZE=$filesize
if [ -d "$TARGET/$DISKNAME" ]; then usage "can not make disk $TARGET/$DISKNAME"; fi
mkdir -p $TARGET/$DISKNAME
else
let "CURRENTSIZE=$CURRENTSIZE+$filesize"
fi
DIRNAME=$(dirname "$file")
FILENAME=$(basename "$file")
if [ "$INTREE" == "1" ]; then
if [ ! -d "$TARGET/$DISKNAME/$DIRNAME" ]; then mkdir -p "$TARGET/$DISKNAME/$DIRNAME"; fi
LINKTARGET="$TARGET/$DISKNAME/$DIRNAME/$FILENAME"
else
LINKTARGET="$TARGET/$DISKNAME/$FILENAME"
fi
ln -s "$SOURCE/$file" "$LINKTARGET"
if [ $? -gt 0 ]; then
let "CURRENTSIZE=$CURRENTSIZE-$filesize"
fi
done
IFS=$OLDIFS
Categorieën:Linux Tips & Trics Tags: