Posts Tagged hint
Overloading properties in Java
Posted by JohannesTheDeveloper in Happy Hacking on August 11th, 2008
public class TestMain
{
public static void main(String[] args)
{
/* we want "B" with the use of the function from A
* (without copying the function) */
System.out.println(new B().getClassname());
}
}
abstract public class A
{
protected String classname = "A";
String getClassname(){
return classname;
}
}
public class B extends A
{
protected String classname = "B";
}
That doesn’t work.
This will:
public class B extends A
{
{ classname = "B" ; }
}
You can’t make the property final in A then, though.
Maven: OutOfMemory in surefire tests
Posted by JohannesTheDeveloper in fun with Linux on July 29th, 2008
Or how to pass Xmx712M or Xmx1024m parameter to maven2 test runs:
In your pom, add the argLine parameter.
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.3</version>
<configuration>
<argLine>-Xmx712M</argLine>
</configuration>
</plugin>
Join/Merge PDF files together
Posted by JohannesTheDeveloper in fun with Linux on June 16th, 2008
Just found this wonderful command here for ghostscript to produce one pdf out of many:
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=finished.pdf file1.pdf file2.pdf
Bash: Random in bash
Posted by JohannesTheDeveloper in Happy Hacking on May 31st, 2008
Just discovered $RANDOM in bash.
I usually just did something like “grep –text -Eo ‘[0-9]{2,3}’ /dev/urandom” which is fine for most cases.
Bash: Binary parameter search
Posted by JohannesTheDeveloper in Happy Hacking on May 25th, 2008
Suppose you look for a specific numeric parameter n for a program.
It behaves in one way if the parameter is less than n and in another when it is greater than n.
Binary search can be applied.
Examples when you have such a scenario are:
- A web application, and you fill in GET/POST-values with curl or wget
- You use a revision control system and a bug was introduced at some version, and you wrote a test for that bug. (git has a command for that: git-bisect)
Anyway, here a simple binarytest.sh.
It calls the program (third parameter) with a number as parameter.
#!/bin/bash
START=$(($1))
STOP=$(($2))
TEST="$3"
if [ "$START" == "" ] || [ "$STOP" == "" ] || [ "$START" == "$STOP" ] || [ "$TEST" == "" ]; then
echo "SYNAPSIS: $0 <start> <stop> <testprog>"
exit
fi
while [ "$START" != "$STOP" ]; do
N=$(( ($START + $STOP)/2 ))
echo "Testing against $N"
if $TEST $N; then
if [ $(($START-$STOP)) == "-1" ]; then
START="$STOP"
else
START="$N"
fi
echo "selecting upper half: $START:$STOP"
else
if [ $(($START-$STOP)) == "-1" ]; then
STOP="$START"
else
STOP="$N"
fi
echo "selecting lower half: $START:$STOP"
fi
done
SVN up
Posted by JohannesTheDeveloper in fun with Linux on May 3rd, 2008
If you do a svn up, you want to know what happened in between, right?
Log entries, Files changed. You can either go step by step through the revisions or let this bash script do the svn up for you:
#!/bin/bash
oldrev=$(svn info |grep '^Revision: '|sed 's/Revision: //g')
svn up
newrev=$(svn info |grep '^Revision: '|sed 's/Revision: //g')
[ "$oldrev" == "$newrev" ] || {
echo "Made the step from $oldrev to $newrev; Log: "
for i in $(seq $oldrev $newrev); do
svn log -r $i
echo Files:
svn diff --diff-cmd diff -x -q -c $i | grep '^Index:' | sed 's/^Index://g'
echo
done
}
run Firefox as another user
Posted by JohannesTheDeveloper in fun with Linux on April 4th, 2008
Firefox is a big application and thus has security-related issues. NoScript can help a little, but not all bugs are necessarily script related.
Here I let firefox run as another user “kiosk”:
I access the normal user’s Xauthority file to be able to write to the X server. Note that firefox will not be able to write to your users home directory (which is the whole point), but to kiosk’s home, from which you can grab the files.
File /home/user/bin/firefox.sh
#!/bin/bash
cp -f /home/user/.Xauthority /home/kiosk/
chown kiosk:kiosk /home/kiosk/.Xauthority # you can also do this with xhost.
su -c "XAUTHORITY=/home/kiosk/.Xauthority DISPLAY=:0.0 /opt/firefox/firefox" kiosk
Then, as user, call
sudo bash bin/firefox.sh
And firefox should pop up.
There is also a howto for creating a chroot (for various distros).
However, su didn’t compile, so I didn’t use it. Jailkit might also be something to look into.
CVS log analysing for summarising spent hours
Posted by JohannesTheDeveloper in Happy Hacking on April 2nd, 2008
CVS is based on RCS, which is file based. That means, you don’t really have revisions over multiple files. The “cvs log” command reflects that problem. This outputs a more useful summary: date and commit comment per line.
cvs log > cvslog # puts log in this file
cat cvslog |
grep '^date: ' -A3 | # we assume max 3 lines of comments
while read line; do echo -n "$line |"; done | # all in one line, seperated by |
sed 's/date: /\ndate: /g'| # a line per date
sed 's/[-=]\{2,\}/\n/g' | # removing line seperators
sed 's/^[- |]*//g'| # remove useless - and |
grep -v '^revision '| # there exist some more boring lines
sed 's/; author:.*; |/;/g'| # we aren't interested in author and id etc.
sed 's/ |$//g'| # remove ending |
sort -u | # sort (by date)
python guniq.py | # show only uniq lines
cat > cvslog.1
You need guniq.py which does the same as the unix command uniq, except that it removes duplicates found in the whole input.
guniq.py:
#!/usr/bin/python
import sys
a = []
while True:
l = sys.stdin.readline()
if l == '':
break
if not a.__contains__(l):
print l,
a.append(l)
The whole thing got me nearly to boot into Windows, because TortoiseCVS/TortoiseSVN is a really cool awesome thingy!
HOWTO: Samba share with virus scanning
Posted by JohannesTheDeveloper in fun with Linux on March 19th, 2008
This explains how to install a windows share that is scanned when files are uploaded or changed there.
We use:
- clamav
- inotify-tools
Install those.
I presume you have setup already:
- /etc/clamd.conf
- freshclam works.
- The samba share already exists and works.
What the script does is:
- find all files in the share $SHARE
- listen on changes using inotify
- output from those two is given clam(d)scan, which analyses the $FILE
- If the $FILE contains a virus, it is moved to $VIRUSDIR, and $FILE.txt is created containing a report
What to do? How to install?
- Save the script below to something in your $PATH, like /usr/local/bin/scanshares
- (Set the variables according to your setup)
- Make sure $SHARE and $VIRUSDIR is owned by the user “nobody”
- Call it using
sudo -u nobody scanshares >/var/log/scanshares - Watch /var/log/scanshares while you put a test virus file from EICAR into your share. After some seconds, it should be replaced by a text file.
#!/bin/bash
# if you manage to create a file containing a virus,
# which filename signatures a virus, you will send this script
# in a infinite loop ;-)
TMPFILE=/tmp/lastscanresult.txt
SHARE=/srv/samba/
VIRUSDIR=/srv/samba/hasvirus/
VIRUSDB=/var/clamav/
#INOTIFYWAIT=/home/johannes/inotify-tools-3.13/src/inotifywait
INOTIFYWAIT=inotifywait
# if you have no clamd
CLAMSCAN="clamscan --database $VIRUSDB --block-encrypted -i"
# if you have clamd (faster)
CLAMSCAN="clamdscan"
{
# initial check
find $SHARE -type f | while read line; do echo "$line|CLOSE_WRITE"; done;
# await changes
# we need to listen to create, otherwise inotifywait doesn't follow in subdirs :-(
$INOTIFYWAIT -q -m -r -e create -e close_write "$SHARE" --format '%w%f|%e';
} |
grep '|CLOSE_WRITE' --line-buffered | sed -u 's/|.*//g' | # remove create events again
grep -v --line-buffered $VIRUSDIR | # we know THOSE have virus
while read file; do
echo "scanning $file"
$CLAMSCAN --no-summary "$file" > $TMPFILE
if [ "$?" == "1" ]; then
mv "$file" $VIRUSDIR
{
echo 'The file contained a virus and was therefore removed.'
cat $TMPFILE
} > "$file.txt"
fi
echo "scanning $file done."
done
This manual is also available on gentoo-wiki.
Lynx – External Editor
Posted by JohannesTheDeveloper in fun with Linux on February 14th, 2008
Cool feature I just found in the Lynx Manual: In e.g. textareas, where is normally
not possible to add a new line in the middle of the text) you can open the external editor with ^Xe (Ctrl-X e)
You can specify the external editor with the -editor argument, e.g. lynx -editor=pico
Recent Comments