Posts Tagged hint

Python – Load Module from File (by Filename)

You know where a file is and want to load it as a module.

This loads from the file test1 from the directory in which this file is located too.


import os
import imp
def myimp(name):
thisdir = os.path.dirname( myimp.func_code.co_filename )
filename = os.path.join(thisdir, name + ".py")
fp = file(filename, "r")
description = (".py", "r", imp.PY_SOURCE)
return imp.load_module(name, fp, thisdir, description)
test1 = myimp("test1")

No Comments

Good bash.org comments

This grays out lowrated bash.org entries from the random site. Open the file random2 with a browser, e.g. Epiphany (as it automatically reloads the file on changes)

wget bash.org/?random1 -q -O - |
sed 's/\(<p class="quote">.*<\/a>([0-4]\?[0-9]\{1,2\})<a .*<p class="qt"\)/\1 style="color: lightgray !important;"/g' > random2

No Comments

Spamassassin config

Update for a previous story of mine where I posted my user_pref file for spamassassin.

Some notes:

  • Spamassassin usually uses RBL (Realtime Blackhole Lists), you can enable/disable with skip_rbl_checks. If you use it, you might want to use a dnscacher like dnsmasq.
  • The bayesian filter works great here
  • check out Auto-Whitelisting
  • The SpamAssassin-Wiki is really good
  • if you have more than one user, spamd is probably worth checking out.
  • sa-compile for faster performance
  • language/locale checking sounds cool. Found the option at a config generator You will find other options explained there too, like report_safe.
use_auto_whitelist 0
score DATE_IN_FUTURE_12_24      5
score HTML_MESSAGE              3.3
score MIME_HTML_ONLY            5
score MULTIPART_ALT_NON_TEXT    3
score BAYES_00 -4
score BAYES_05 -2
score BAYES_95 6
score BAYES_99 9
score GAPPY_SUBJECT             5
score FROM_EXCESS_BASE64        3
score FROM_HAS_ULINE_NUMS       3
score UPPERCASE_25_50           3
score UPPERCASE_50_75           4
score SUBJ_ALL_CAPS             2
body EVIL_WORDS         /(software|petroleum|casino|kurs|profit|symbol)/i
score EVIL_WORDS                2
describe EVIL_WORDS     evil_words
body EVIL_WORDS2        /(price|oil|bonus|prognose|borse|preis)/i
score EVIL_WORDS2               2
describe EVIL_WORDS2    evil_words2
body EVIL_WORDS3        /(casino|lottery|lotto|euro|free|cheap|health|pharmacy)/i
score EVIL_WORDS3               3
describe EVIL_WORDS3    evil_words3
body DOLLAR_SIGN        /[\$]/
score DOLLAR_SIGN       3
body PERCENT_SIGN       /[\%]/
score PERCENT_SIGN      3
rewrite_header subject         ***SPAM***
report_safe             0
#skip_rbl_checks         1
ok_languages            en de
# Mail using locales used in these country codes will not be marked
# as being possibly spam in a foreign language.
ok_locales              en de

No Comments

Spamassassin and Sylpheed

Sylpheed is a really cool mail program. Less icons than thunderbird, but very fast and gets the job done well.

Junk mail filtering is not integrated, which makes sense since there are great products out there that we should reuse.

Sylpheed provides external program calls, and a preset option fills them out for bsfilter and bogofilter. However, my favorite, spamassassin is not a preset.

Learning command for Junk: sa-learn --spam
Learning command for Not Junk: sa-learn --ham
Classifying command: (see following text)

As a classifying command, sylpheed expects (like bogofilter):
0 for spam; 1 for non-spam; 2 for unsure
However, spamassassin returns:
0 for non-spam, 5 for spam

So, use save the following script in /home/user/.spamassassin/spamcheck.sh:

#!/bin/bash
# return
#       0 for spam; 1 for non-spam; 2 for unsure ; 3 for I/O or other errors.
# spamassessin returns
#       0 for non-spam, 5 for spam
spamassassin -e "$1"
RETURN="$?"
[[ "$RETURN" == "0" ]] && exit 1
[[ "$RETURN" == "5" ]] && exit 0
exit 2

Set it in sylpheed as classifying command:
bash /home/user/.spamassassin/spamcheck.sh

If you want to save 0.1 seconds per call, compile the attached C file spamcheck (c, 2 KB) with

gcc -Wall -Wextra -Werror -ansi -pedantic -o spamcheck spamcheck.c

and use the outcoming spamcheck executable instead.

No Comments

Rip audio files from Flash videos (youtube etc.)

This describes how you can rip the audio from FLV video files as found on youtube&Co and save them as ogg or mp3.
You need:

  • youtube-dl Python script for fetching the FLV-file from youtube. (optionally if you have the flv file already)
  • mplayer for extracting the audio data from the video
  • oggenc for encoding the raw audio to a ogg audio file or
  • lame for encoding the raw audio to a mp3 audio file

The commands:

python youtube-dl.py -t 'http://www.youtube.com/watch?v=<videohash>'
mplayer -ao pcm -vo null '<yourvideo>.flv'
oggenc audiodump.wav -o '<yourtargetoggfile>.ogg'
lame audiodump.wav -o '<yourtargetmp3file>.mp3'
rm audiodump.wav

No Comments

NTFS resize on DSL (Damn Small Linux)

open a terminal
optionally set proxy


export http_proxy=http://proxy.somedomain:8080/
export http_proxy=http://ntlmuser:ntlmpasswd@proxy.somedomain:8080/

open the MyDSL Panel


mydslPanel.dsl

From “System”, install gcc1-with-libs, gnu-utils and linux-kernel-headers (probably only need the first)

Point your Browser to http://linux-ntfs.org/ and download the source for ntfsprogs to /tmp. the usual compiling:


cp /tmp/
tar -xjvf ntfsprogs-*.tar.bz2
cd ntfsprogs-*/
./configure && make

in the subfolder are the utils, most interesting at the moment, ntfsresize.


cd ntfsprogs
./ntfsresize --help
./ntfsresize -i /dev/hda1
./ntfsresize -i /dev/hda5
./ntfsresize -n -s 6500M /dev/hda5
# before, check that your battery won't run out and your data is backed up.
./ntfsresize -s 6500M /dev/hda5

Note: all the *.dsl in /tmp that are downloaded by mydslPanel can be reused with mysdl-load.
Just copy them to a stick, samba- or ssh-server and copy them back after a restart.

No Comments

Encrypted Gnucash data and other programs

Hi! It’s been some time, which means you hadn’t to read trashy blog entries to fill the gaps.

I just updated http://gentoo-wiki.com/TIP_Encrypted_GNUCash.

Its a great way to protect your data files, it is a solution fit for other programs as well, and should work on any unix/linux system. It uses GnuPG/PGP.
Check it out!

No Comments

Sniffing & Firewall rules

Just found this on http://jeffsoh.blogspot.com/2007/06/wireshark-and-firewall-rules.html I want to share:

Click on any captured packet, and go to Analyze, Firewall ACL Rules. A dialog box will pop up showing you the syntax to write a rule denying or accepting the packet by IP, MAC, port or combination thereof depending on what device you choose. Supported is Cisco IOS, iptables, ipfirewall, Windows firewall, and pf (BSD’s Packet Filter firewall).

No Comments

Fighting spam mails

I use POP, and have already 7000 messages on the server (cause I always tell my clients to leave them, so I can read mails from different places).

Until now, under the above were also lots of spam mails, which I wanted to get rid of (when I fetch them on a new client, I takes ages).

Thunderbird supports something cool: If you have your spammail in one folder (Junk), you can create a message filter:
“If mail is not flagged: Delete from POP server, mark as flagged. Run in Junk folder.”

This is great, but you still have to find out which mails are spams. The following can help: We try to get our address book filled based on number of mails we got from each address. Thus we will (almost) have our friends and people we expect mail from, in the address book, and they won’t be marked as spam.

$ cd "~/.thunderbird/s2ardbyl.default/Mail/Local Folders"
$ cat Inbox |
grep "^From: " |
grep -Eo "[^< ]*@[^ >]*"  |
sort |
uniq -c |
sort -n |
grep -v " 1 " |
sed 's/^ *\([0-9]*\) /\1\t/g' > ~/emails.tab

Okay, now we got in ~/emails.tab the number of mails, and the address, tab separated. You can import that using the address book of thunderbird (choose for example Work Phone and Primary email address; then sort by the first). Look over it, and delete stuff you don’t know. Either change the setting in Junk controls or move the people to your “Personal Addressbook”.

Now you can make a saved search looking for emails from your address book. You can mark them all as “Not Spam”.
Results of running Junk control over the mail folder should improve by that.

1 Comment

Live Ping graph

The first snippet makes ping queries and saves the response time to pingstat.txt.


ping www.google.com |
grep --line-buffered -Eo "time=[0-9\.]+ ms" |
sed -u 's/time=\([0-9\.]*\) ms/\1/g' |
while read line; do echo $(date +%s)" $line"; done >> pingstat.txt

The second takes the last 200 from them and displays them in a graph (gnuplot).


while true; do
tail -n200 <pingstat.txt >pingstat1.txt;
echo 'plot [] [0:1000] "pingstat1.txt" with lines notitle';
sleep 1;
done } |
gnuplot

No Comments