Please use a browser that supports javascript

Bogeskov.dk

Some small hacks for Linux

Truncated path in bash-prompt

set -o physical
PROMPT_COMMAND=$'_p="${PWD/#$HOME/~}";_p1="${_p//\134\047/\047\$\047\134\134047\047\047}";eval _p1=("\047${_p1//\134//\047 \047}\047");[ ${#_p1[@]} -gt 6 ]&&_p="${_p1[@]:0:1}/${_p1[@]:1:1}/${_p1[@]:2:1}/**/${_p1[@]:${#_p1[@]}-3:1}/${_p1[@]:${#_p1[@]}-2:1}/${_p1[@]:${#_p1[@]}-1:1}";_u=$USER@;_u=${_u/#bogeskov@/}'
PS1='\u@\h:${_p}\$ '


"export" in tcsh/csh

alias export 'eval `perl -e '\''print join(";", map { s/\047/\047\134\134\047\047/g; s/^(.*?)=(.*)$/setenv $1 \047$2\047/; $_ } @ARGV), "\n"'\'' -- \!*`'


Disable screen blanking on the linux-console

/usr/bin/setterm -term linux -blank 0 >> /etc/issue


A script to run vnc over ssh, with the automatic start and shutdown of vncserver.

#!/usr/bin/expect -f

if {$argc > 0} {
    set host [lindex $argv 0]
} else {
    send_error "usage: $argv0 host \[ display \[ Xvnc options ... \] \]\n\n"
    exit 1
}

if {$argc > 1} {
    set display [lindex $argv 1]
} else {
    set display 1
}

# Sane 16:9 defaults (better than 640x480x8)
if { $argc <= 2 } {
    set argv [split "- :$display -depth 16 -geometry 960x576"]
}

# Escape arguments
set args [ list ]
foreach a [lrange $argv 1 [llength $argv]] {
    set args [concat $args [list "'[regsub -all "'" $a "'\\''"]'"]]
}
set args [join $args " "]

set prompt "(%|#|\\$|>) $"

#
# Connect
#
spawn ssh $host -L[expr 5900 + $display]:127.0.0.1:[expr 5900 + $display]

expect -re $prompt
send "xinit /usr/bin/twm -- /usr/bin/Xvnc $args\n"
expect -re "Listening for VNC connections on TCP port"
system "vncviewer -encodings 'copyrect tight hextile zlib corre rre raw' :$display"
send "\003"
expect -re $prompt
send "exit\n"
send_user "Done!!!\n"


How to fill a device with random data

mkfifo fifo
dd if=/dev/urandom of=fifo bs=$(($RANDOM + 65536)) count=1 &
buffer -i fifo | openssl enc -aes-256-cbc -k "`hexdump -n 128 /dev/urandom  -e '1/1 "%02x"'`" | tee fifo | dd of=$DEVICE bs=64k iflag=fullblock skip=3
  • make a fifo
  • put some random data into the fifo
  • make a feedback-loop
    • read data from the fifo
    • encrypt data with openssl (with a random password)
    • feed tha data back into the fifo (making the feedback-loop)
    • send data into the device (skip the first, to ensure that we're in the feedback-loop)

On a 2.7GHz Core-i7 this yields >300MB/s (using 200% cpu) when send into /dev/null


XPath with document on stdin and output for eval in (ba)sh.

perl -MHTML::TreeBuilder::XPath -e 'my$t=HTML::TreeBuilder::XPath->new()->parse_file(*STDIN);for(@ARGV){die"Failure(arg=$_)\n"unless(m/^(.*?)=(.*?)(?:#(.*))?$/);$re=$3||"^.*\$";print"$1=$_;\n"for(map{m/($re)/ms;$_=$2||$1;s/\047/\047\\\047\047/g;"\047$_\047"}grep{m/$re/ms}$t->findvalues($2))}'

or as an alias:

alias xpath='perl -MHTML::TreeBuilder::XPath -e '\''my$t=HTML::TreeBuilder::XPath->new()->parse_file(*STDIN);for(@ARGV){die"Failure(arg=$_)\n"unless(m/^(.*?)=(.*?)(?:#(.*))?$/);$re=$3||"^.*\$";print"$1=$_;\n"for(map{m/($re)/ms;$_=$2||$1;s/\047/\047\\\047\047/g;"\047$_\047"}grep{m/$re/ms}$t->findvalues($2))}'\'

takes arguments of the form var=xpath or var=xpath#regex