(Started: August 2006)

I've been using Linux for almost a decade now, so over the years I've collected a number of digital notes on various issues. Some of them could be out of date - they are here mostly as an online reference for myself, but they may also prove useful to others...



Disable LWP and Net::FTP for CPAN

Edit /etc/perl/CPAN/Config.pm and change dontload_hash to... ... 'dontload_hash' => {"Net::FTP" => 1, "LWP" =>1 }, # disable Net::FTP and LWP ...

Talk to an SSL-enabled site with non-SSL clients: stunnel

From window 1: stunnel -d 5000 -c -f -r www.semantix.gr:443 From window 2: non-ssl-wget http://localhost:5000

Make VMWARE images run faster:

Add these lines to the .vmx file: sched.mem.pshare.enable = "FALSE" prefvmx.useRecommendedLockedMemSize = "TRUE" prefvmx.minVmMemPct = "100" mainMem.partialLazySave = "FALSE" mainMem.partialLazyRestore = "FALSE" mainMem.useNamedFile = "FALSE"

Make VMWARE Player use more than two cores:

Edit your .vmx file and change the following: config.version = "8" virtualHW.version = "7" numvcpus = "4" maxvcpus = "4" cpuid.coresPerSocket="2" (remember to give adequate memory, too) memsize = "1024"

Remove a Debian service from startup scripts:

update-rc.d -f nameOfServiceAsExistsUnderEtcInitD remove

Build debian package from its sources: When I want to install a version of a program that only exists in the unstable branch, I don't want to mess up all my Debian installation - I just... apt-get -b source -t unstable pbzip2 ...which creates a nice .deb file, installable with "dpkg -i whatever.deb". If the program has build dependencies, I install them first with... apt-get build-dep pbzip2

Redirecting cerr to a stringstream: // When you try this, don't output to cerr... streambuf * cerr_strbuf(cerr.rdbuf()); ostringstream output; cerr.rdbuf(output.rdbuf()); ... // Restore cerr cerr.rdbuf(cerr_strbuf);

Using Perl as a super-grep: I prefer to use Perl regexps instead of grep's. Here is a supergrep that uses Perl. It supports the '-i', '-n' and '-v' options that grep has (Update: or you can use pcregrep).

Migrate to a RAID1 config: My non-RAID config was simple: I had one (and only one!) Linux partition, which was of course, my root partition: /dev/hda5 . After adding a second (and much bigger) drive, I decided to migrate to a RAID1 configuration. I first used fdisk -u -l /dev/hda , to find out the size (in sectors) of my partition: home root# fdisk -u -l /dev/hda Disk /dev/hda: 40.0 GB, 40020664320 bytes 255 heads, 63 sectors/track, 4865 cylinders, total 78165360 sectors Units = sectors of 1 * 512 = 512 bytes Device Boot Start End Blocks Id System /dev/hda1 * 63 8193149 4096543+ 7 HPFS/NTFS /dev/hda2 8193150 78156224 34981537+ 5 Extended /dev/hda5 8193213 78156224 34981506 83 Linux home root# bc -l bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 78156224-8193213 69963011 <============= The number of sectors ===== I then proceeded to create a partition with exactly the same size, on the new drive (hdb): home root# fdisk -u /dev/hdb The number of cylinders for this disk is set to 38913. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSes (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): n Command action e extended p primary partition (1-4) p --------- The number of sectors ---------, Partition number (1-4): 1 | First sector (63-625142447, default 63): 63 V Last sector or +size or +sizeM or +sizeK (63-625142447, default 625142447): +69963011 Command (m for help): t ...(set the filesystem type for /dev/hdb1 to 0xfd, Linux raid auto-detect) Command (m for help): p ... Command (m for help): w ... Command (m for help): q I then proceeded to create the RAID1 device, md0, setting it up to only have the /dev/hdb1 partition, with the other one set to missing : home root# mdadm --create /dev/md0 --level=1 --raid-disks=2 missing /dev/hdb1 home root# mkreiserfs /dev/md0 home root# mkdir /mnt/tmp home root# mount /dev/md0 /mnt/tmp home root# cp -a /boot /dev /root /.... /... /mnt/tmp/ home root# vi /mnt/tmp/etc/fstab (change the root to be /dev/md0) home root# vi /boot/grub/menu.lst (change the root=/dev/hda5 to root=/dev/md0) At this point, after a reboot, the kernel identified the /dev/md0 automatically, and the boot process went fine... Only one thing remained: to add the /dev/hda5 to the array /dev/md0 , and allow it to automatically sync up: home root# mdadm /dev/md0 -a /dev/hda5 home root# cat /proc/mdstat The last command showed the process of synchronization. When it finished, my RAID1 setup was completed. My fears for faulty drives were finally put to rest...

Man pages with colors (ala OpenBSD console):

Put these in your .bashrc (or whatever your shell initialization script is): export LESS_TERMCAP_mb=$'\E[01;31m' export LESS_TERMCAP_md=$'\E[01;37m' export LESS_TERMCAP_me=$'\E[0m' export LESS_TERMCAP_se=$'\E[0m' export LESS_TERMCAP_so=$'\E[01;44;33m' export LESS_TERMCAP_ue=$'\E[0m' export LESS_TERMCAP_us=$'\E[01;32m' Oh, and make sure 'less' is your PAGER (or MANPAGER).

Setup a RAID1 (mirror) of two loop devices:

bash$ blockdev --getsize /dev/loop0 2048000 bash$ blockdev --getsize /dev/loop1 2048000 (outputs must be the same!) bash$ echo 0 2048000 mirror core 2 16 nosync 2 \ /dev/loop0 0 /dev/loop1 0 | dmsetup create myraid1 bash$ ls -l /dev/mapper/myraid1 bash$ blockdev --getsize /dev/mapper/myraid1 2048000 You can find our more examples (e.g. stripe(RAID0) and linear) in the NTFS documentation that comes with the Linux kernel (Documentation/filesystems/ntfs.txt) - look at the Device Mapper section of the file.





Show package sizes under Debian:

dpkg-query -Wf '${Package}\t${Installed-Size}

'



Create a minimal Debian for chrooted SSH:

root# debootstrap etch /opt/chroot \ http://ftp.x.y/pub/linux/debian/ root# chroot /opt/chroot ....# adduser userlogin ....# logout root# apt-get install libpam-chroot root# echo 'session required pam_chroot.so' >> \ /etc/pam.d/ssh root# echo 'userlogin /opt/chroot' >> \ /etc/security/chroot.conf ...and then, modify the /etc/passwd and /etc/group IDs of the user to mirror those of the chrooted environment.





Use Debian alternatives to set multiple C/C++ compiler versions:

root# update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 10 root# update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 20 root# update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30 root# update-alternatives --set cc /usr/bin/gcc root# update-alternatives --config gcc root# root# update-alternatives --install '/usr/bin/g++' 'g++' '/usr/bin/g++-4.6' 10 root# update-alternatives --install '/usr/bin/g++' 'g++' '/usr/bin/g++-4.7' 20 root# update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30 root# update-alternatives --set c++ /usr/bin/g++ root# update-alternatives --config g++

Allow a user to be sudo-root:

root# visudo ...and add a line for account, say, rootfriend: rootfriend<TAB>ALL=(root) ALL That's it. User rootfriend can do anything root can, as long as he types his own password the first time he "sudo cmd args"...

Backing up with tar:

tar jcplSf /mnt/backup/`date +"%Y%m%d%H%M%S"`.tar.bz2 / j: compress with bzip2

c: create volume

p: preserve permissions

l: stay in this filesystem

S: handle sparse files

f: store in this file



Converting a raw filesystem image to/from AES encryption using aespipe in one pass:

Encrypt: dd if=/dev/XXX bs=512 | aespipe -e aes256 | \ dd of=/dev/XXX bs=512 conv=notrunc Decrypt: dd if=/dev/XXX bs=512 | aespipe -d -e aes256 | \ dd of=/dev/XXX bs=512 conv=notrunc

Mounting a loop-AES partition/DVD using cryptsetup:

sh# losetup /dev/loop0 /path/to/whatever/file/or/volume sh# cryptsetup -c aes-plain -h sha512 create crypted /dev/loop0 Enter passphrase: sh# mount /dev/mapper/crypted /mnt/heaven

Learning about a Samba LAN:

#!/bin/bash for i in `nmap -sP 192.168.0.0/24 | grep 'appears to be up' \ | perl -ne '/(192.168.\d+\.\d+)/ && print $1."

";'` do nmblookup -A $i done

Using afio to back up (with gzip compression) to CD images:

Make it: find / -xdev -print | afio -Z -G 1 -s 650m \ -o /mnt/cdimage.afio (will prompt you before overwriting cdimage.afio with the next image...) Use it: make a symbolic link from Disk to Disk1, afio -Z -G 1 -s 0 -i Dsk and when prompted, change the link to point to Dsk2, Dsk3, etc...

Backing up my NTFS partition at work over the network to the server:

Mount the ntfs partition on /iso1: mount -t ntfs -o iocharset=utf8 /dev/hda1 /iso1 or, depending on kernel version: mount -t ntfs -o nls=utf8 /dev/hda1 /iso1 and in the server, cd into /opt/Backup/Laptop-NTFS/ and run: rsync -avz dell:/iso1/ .

Convert a bitmap to a vector for use from LaTEX:

bash$ autotrace -filter-iteration 9 -output-file fig1.eps \ fig1.pnm bash$ epstopdf fig1.eps and use graphicx in the .tex file: \begin{figure} \begin{center} \includegraphics[width=0.85\textwidth]{../fig1} % fig1.pdf \caption{Blah blah} \label{figure1} \end{center} \end{figure}

Make a two-pages-per-A4 booklet from a PS file:

ps2pdf file.ps acroread file.ps (print to file, Postscript Level 2) psbook -s4 newfile.ps | mpage -2 -bA4 -m0 -o > booklet.ps

Cracking Windows passwords in case of human memory failure:

mkdir /var/tmp/a cd /mnt/ntfs/WINDOWS/system32/ cp -a config/ /var/tmp/a cd /var/tmp/a /usr/local/bin/bkhive ./config/system /tmp/op2.tmp /usr/local/bin/samdump2 ./config/SAM /tmp/op2.tmp > \ /tmp/ophcrack.tmp ophcrack /tmp/ophcrack.tmp 1

Serving files over the Web in a one-liner:

bash$ cd /path/to/datafiles bash$ python -m SimpleHTTPServer

Using multicores via xargs:

find . -type f -iname \*dds | xargs -n 1 -P 4 -I 'crap' convert crap crap.png This command will find all .dds files and execute ImageMagick's "convert" on them... but it will do so by always keeping 4 instances of "convert", and thus keeping 4 cores busy.

Install stuff with CPAN:

bash$ sudo perl -MCPAN -e shell cpan shell -- CPAN exploration and modules installation (v1.9402) Enter 'h' for help. cpan[1]> i /LinkCheck/ ... Module Rubric::CLI::Command::linkcheck (RJBS/Rubric-0.144.tar.gz) Module = W3C::LinkChecker (SCOP/W3C-LinkChecker-4.5.tar.gz) ... 11 items found cpan[2]> install W3C::LinkChecker ... cpan[3]> exit bash$

Python code for sorting XML in preparation for diff:

Download from here, and use like this: bash$ sortXML.py old.xml > old.xml.sorted bash$ sortXML.py new.xml > new.xml.sorted bash$ xxdiff old.xml.sorted new.xml.sorted

Automatically execute code whenever any files under a folder tree change:

#!/bin/bash command="$1" shift fileSpec="$@" sentinel=/tmp/t.$$ touch -t197001010000 $sentinel while : do files=$(find . -newer $sentinel -a '(' $fileSpec ')') if [ $? != 0 ]; then exit 1; fi if [ "$files" != "" ]; then $command touch $sentinel fi sleep 0.1 done

Python code for converting csv to html:

import csv import sys if len ( sys . argv )< 2 : print "Usage:" , sys . argv [ 0 ], "file1 <file2> ..." sys . exit ( 1 ) print "<html><head></head><body><table>" for f in sys . argv [ 1 :]: for line in csv . reader ( open ( f , 'r' )): print "<tr>" for elem in line : print "<td>" , str ( elem ), "</td>" print "</tr>" print "</table></body></html>"

Build histogram with Gnuplot:

Starting from a 'stats.txt' file, containing frames per second measurements, this script will create 'bins' of size 5 (ie. it will accumulate all sample values from 0-5 into bin 1, 5-10 into bin 2, etc) and then plot the bin heights in a histogram. # !/ usr / bin / perl - w # # "stats.txt" has frames per second , e . g . values from 1 to 78 fps # This script will generate "stats.png" via gnuplot . # # For better fonts , before calling this , use : # export GDFONTPATH =/ usr / share / fonts / truetype / msttcorefonts / # export GNUPLOT_DEFAULT_GDFONT = times # use strict ; my $total = 0 ; my open DATA , "stats.txt" or die "No stats

" ; open PLOTDATA , ">histogram.txt" ; while (< DATA >) { # place sample values in bins of width 5 $bins { 2.5 + 5 * int ( $ _ / 5 ) } ++; $total ++; } my @keys = sort { $a <=> $b } keys # print the bin cardinalities foreach my $ k ( @keys ) { print PLOTDATA $k . " " ; print PLOTDATA 100 * $bins { $k } / $total ; print PLOTDATA "

" ; } # Plot the bins as histogram open PIPE , "|gnuplot" || die "Can't initialize gnuplot ...

" ; print PIPE << END # set term postscript eps enhanced color # set output "stats.eps" # set style line 1 lt 1 lw 20 set term png set output "stats.png" set yrange [ 0 : 17 ] set boxwidth 0.8 relative set xlabel "Frames per second" set ylabel "Percentage with this speed" plot 'histogram.txt' using 1 : 2 with boxes fs solid 0.7 title 'Reported speeds, Oct 2010 ' END

Build animated .gif with Gnuplot:

Just use this: print PIPE "set term gif animate 300 optimize

"; print PIPE "set output \"graph.gif\"

"; ...and then plot as usual.

Increase number of loop devices (more than 8 squashfs):

Edit/create this file in Debian... bash$ cat /etc/modprobe.d/local-loop options loop max_loop=24

Squid simple authentication:

Edit squid.conf (in IPCOP, located under /etc/squid): Add these lines on top: auth_param basic program /usr/lib/squid/ncsa_auth /etc/squid/passwds.proxy acl auth_users proxy_auth REQUIRED http_access allow auth_users And maintain the passws.proxy via htpasswd...

SSH productivity tips:

bash$ cat ~/.ssh/config ControlMaster auto ControlPath /tmp/ssh_mux_%h_%p_%r ControlPersist 4h More SSH tips are here.

CentOS RPM build from source:

bash$ cd /usr/src/redhat/SOURCES/ bash$ wget http://mirror.mcs.anl.gov/openssh/portable/openssh-5.9p1.tar.gz bash$ tar zxvf openssh-5.9p1.tar.gz bash$ cp openssh-5.9p1/contrib/redhat/openssh.spec /usr/src/redhat/SPECS/ bash$ rm -rf openssh-5.9p1/ bash$ cd /usr/src/redhat/SPECS/ bash$ rpmbuild -bb openssh.spec bash$ cd /usr/src/redhat/RPMS/i386 bash$ rpm -Uvh ...

Sed and Tr wizardry:

bash$ cat nice.sh tr -cs A-Za-z '

' | tr A-Z a-z | sort | uniq -c | sort -rn | sed ${1}q bash$ cat README | nice.sh 5 3 the 2 of 2 a 1 with 1 up Explanation: - Make one-word lines by transliterating the complement (-c) of the alphabet into newlines (note the quoted newline), and squeezing out (-s) multiple newlines. - Transliterate upper case to lower case. - Sort to bring identical words together. - Replace each run of duplicate words with a single representative and include a count (-c). - Sort in reverse (-r) numeric (-n) order. - Pass through the stream editor; quit (q) after printing the number of lines designated by the script's first argument.

Perfect tmux config:

bash$ cat ~/.tmux.conf set -g prefix C-a unbind % bind \ split-window -h bind - split-window -v bind-key k select-pane -U bind-key j select-pane -D bind-key h select-pane -L bind-key l select-pane -R bash$ tmux Ctrl-a \ (vertical split) Ctrl-a - (horiz split) Ctrl-a cursors (or h/j/k/l): move amongst windows Ctrl-a d (detach) bash$ tmux list-sessions ... bash$ tmux attach -t 0

HTML entities inside VIM:

$ cat /usr/local/bin/htmlEntities.pl #!/usr/bin/perl -w use HTML::Entities; while(<>) { print encode_entities($_); } $ vi whatever.html ... Then select using visual mode and... '!/usr/local/bin/htmlEntities.pl or using classic ranges: (go to start and mark with marker 'a') ma (go to end and mark with marker 'b') mb 'a,'b!/usr/local/bin/htmlEntities.pl

Use VI mode in bash but keep ESC-.

Inside your .bashrc, add these: set -o vi # Read my comment in this SO post for explanation: # http://stackoverflow.com/questions/6839273/ bind -m vi-command ".":insert-last-argument # Restore Ctrl-L, Ctrl-A, Ctrl-E, Ctrl-W bind -m vi-insert "\C-l.":clear-screen bind -m vi-insert "\C-a.":beginning-of-line bind -m vi-insert "\C-e.":end-of-line bind -m vi-insert "\C-w.":backward-kill-word

My .Xresources:

$ cat .Xresources *VT100*foreground: wheat *VT100*background: black *VT100*saveLines: 2500 *VT100*scrollBar: true *VT100*visualBell: true *XTerm*font: -b&h-lucida console-medium-r -semi condensed--0-0-0-0-m-0-iso8859-7 *XTerm*faceName: -b&h-lucida console-medium-r -semi condensed--0-0-0-0-m-0-iso8859-7 *XTerm*faceSize: 12 *XTerm*renderFont: true ! To write in Greek, set this in your .bashrc ! XTERM_LOCALE=el_GR.iso88597 ! and uncomment this: !*XTerm*locale: true $ grep .Xresources .bashrc xrdb -merge $HOME/.Xresources

My bash PS1 setting:

$ grep PS1 .bashrc export PS1='

\[\e[32;1m\]\u@\h\[\e[37;1m\] \w\]

\[\e[0m\]\$ '

Create a "drive" for QEMU out of a partition image:

bash$ dd if=/dev/zero of=/tmp/zero count=63 bash$ losetup -fv /tmp/zero bash$ losetup -fv sda1 bash$ echo -e \ "unit: sectors



/dev/sda1 : start= 63, size= $( blockdev --getsz sda1 ), Id=7" | \ sfdisk --force /dev/loop0 bash$ echo -e \ "0 63 linear /dev/loop0 0

63 $( blockdev --getsz sda1 ) linear /dev/loop1 0" | \ dmsetup -v create virtdisk bash$ fdisk -lu /dev/mapper/virtdisk There; A new "drive" to pass to QEMU.

Password protect an Apache folder:

bash$ htpasswd -c -m /etc/mypass myaccount (create password for user 'myaccount') bash$ cd /var/www/wherever/you/want bash$ cat > .htaccess AuthUserFile /etc/mypass AuthName "Please Log In" AuthType Basic require valid-user And remember to use https, not just http (to avoid sniffers)

At bootup, spawn a screen session via cron:

Inside your crontab, use.. $ crontab -e ... (Add this line) @reboot /usr/bin/screen -d -m -S autoLoginScreen /home/ttsiod/bin.local/autoLogin.sh ... (And this is what exists in the spawned script - automatically logging-in into an obscure crappy thingie that loses its wits all the time): $ cat /home/ttsiod/bin.local/autoLogin.sh #!/bin/bash while true ; do date /home/ttsiod/bin.local/autoLogin.py sleep 60 done This is better than spawning autoLogin.py from within cron, for various reasons: You can attach to the screen (screen -r ...) and see what's happening - and also, some scripts (e.g. using Python pexpect) may not run properly under cron, even after setting the env to the same as the normal one (most likely, missing tty-stuff).

Downgrade a hybrid Debian into "pure" stable or testing:

( echo -n "sudo apt-get install -y --allow-downgrade " ; apt-cache madison \ $(apt-show-versions | grep -v jessie | grep 'newer than version' | nth 0 ) | grep ftp.de | awk '{printf("%s=%s

", $1, $3);}' ) > doit.sh sudo ./doit.sh

CLang,NERDTree,EasyMotion for VIM:

Install NERDtree and EasyMotion, by downloading from http://vim.org and unzip-ing inside your .vim/ folder. For CLang autocompletion it's the same process, with the added caveat that under Arch, I had to use the "libclang" based mode of operation (spawning didn't work for some reason). After that, in my .vimrc: let g:clang_library_path='/usr/lib/llvm' (necessary for using libclang) autocmd CursorMovedI * if pumvisible() == 0|pclose|endif autocmd InsertLeave * if pumvisible() == 0|pclose|endif (auto-closes preview window after you select what to auto-complete with) nmap <silent> <c-n> :NERDTreeToggle<CR> (maps NERDTree to Ctrl-N) let g:NERDTreeDirArrows=0 (tells NERDTree to use standard characters)

Python administration with pip and virtualenv:

After installing easy_install from distro... easy_install pip pip install virtualenv pip install virtualenvwrapper mkdir $HOME/.virtualenvs Edit .bashrc, and change python2 below to your Python... export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2 source /usr/bin/virtualenvwrapper.sh Re-login. You can now... mkvirtualenv whatever workon (shows list) workon whatever (prompt changes, you are in whatever now...) pip freeze (shows list of packages, both system-wide and local) pip freeze -l (shows list of local packages) pip search packagename pip install packagename pip uninstall packagename deactivate (back to normal) If you want a "fresh" environment, that doesn't use the globally installed versions of packages, then instead of... mkvirtualenv whatever ...do this: mkvirtualenv --no-site-packages whatever

Import sources in a local CVS repository:

mkdir /opt/cvsroot export CVSROOT=/opt/cvsroot cd /var/tmp dtar mysources.tgz cd NewProggy cvs import ttsiodras/NewProggy NewProggy start cd .. rm -rf NewProggy cvs checkout ttsiodras/NewProggy cd ttsiodras/NewProggy vi ... cvs commit ...

Git-svn generated .cvsignore files into .git/info/exclude :

From directory containing .git, execute... find . -type f -name .cvsignore | \ while read LO ; do \ PREF="`dirname $LO | cut -c3-`" ; \ cat "$LO" | \ while read WOW ; \ do echo "$PREF/$WOW" ; \ done ; \ done >> .git/info/exclude

Git status unescaped UTF8 output:

Place quotepath = false in .git/config in the [core] section, or just: $ git config core.quotepath false

Create PNGs from a pdf presentation:

gs -dSAFER -dBATCH -dNOPAUSE -dTextAlphaBits=4 \ -dGraphicsAlphaBits=4 \ -r85 -q -sDEVICE=png16m -sOutputFile=icfp-pg%02d.png \ PhDPresentation.pdf

Indent C or C++ code:

Use indent -kr *c for C, and astyle --style=ansi -l *h *cc for C++.

Show default defines of GCC:

echo | gcc -dM -E -

Execute queries on MySQL and have html output viewed via w3m:

#!/bin/bash echo What is the wikidb wikiuser passwd? read PASSWD while true do echo SQL cmd to execute: \(\"quit\" to abort\) read ANS if [ "$ANS" == "quit" ] then break fi ( echo $ANS | mysql -u wikiuser wikidb -p$PASSWD \ -H -q | w3m -o confirm_qq=0 -T text/html ) done

Change MySQL tables to UTF8:

bash$ mysql -p ... mysql> alter table some_table convert to character set utf8 collate utf8_unicode_ci;

Use octave to solve polynomials:

To use octave for solving 4 3 2 x + 60x - 9261x - 54000x - 810000 = 0 I typed: a = [ 1 60 -9261 -54000 -810000] roots(a) and I got: ans = -128.9492 + 0.0000i 75.0000 + 0.0000i -3.0254 + 8.6372i -3.0254 - 8.6372i

Use oprofile for detailed stats on runtime:

opcontrol --no-vmlinux opcontrol --start run_your_program opcontrol --shutdown opreport -l your_program_binary

Use a PAP-based modem account:

Edit /etc/ppp/pap-secrets and /etc/ppp/options to fill-in the loginname/password info. In /etc/ppp/pap-secrets add a line: * * password and in /etc/ppp/options, add a line: lock name loginname usepeerdns

Access Internet from within QEMU:

Once you get connected with wvdial or whatever you use... echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE and execute qemu...

The simplest of software RAID1, two partitions:

mdadm --create /dev/md0 --level=1 --raid-disks=2 \ /dev/sda1 /dev/sda2 mdadm -A /dev/md0 /dev/sdb1 /dev/sdb2

Read a damaged CD/DVD valid parts and get the rest with rsync:

As is often the case, when I bring some burned CD/DVD from work, I find out that its bad at some offset. I came up with this Perl script: --------------------------------------- #!/usr/bin/perl -w use strict; my $i=0; select((select(STDOUT), $| = 1)[0]); unlink("data"); system("dd if=/dev/zero of=zero bs=2K count=1"); my $step = 1; print "Sector: "; while(1) { system("dd if=/cdrom/BadSector of=sector bs=2K skip=$i". "count=1 >/dev/null 2>&1"); if ($? == 0) { print sprintf("\b\b\b\b\b\b\b\b%08d", $i); system("cat sector >> data"); $step = 1; $i += $step; } else { system("cat zero >> data"); $step += $step; $i += $step; print "

Jumped over $step

Sector: "; } } ----------------------------- With the CD/DVD mounted on /cdrom/, it will slowly but effectively copy sector by sector of the file mentioned in the 'dd' line into the file called 'data'. Reading sector-sector proves to be enough to correct a multitude of read-errors on my DVD reader, but even if that isn't enough, it will quickly jump over the problem areas in the DVD, writing blank 'sectors' to mark the jumps. After that, rsync can save the day: rsync -vvv -B 131072 -e ssh \ login@xxx.yyy.zzz.kkk:/path/todata data

Connect to the BSD console over a null cable:

slattach -p slip -s 19200 /dev/ttyS0 & ifconfig sl0 192.168......

Password-less logins through SSH:

To allow no-password logins to a target account, (say, root :‑), you must add the public key of the account you will login from, into the target account's .ssh/authorized_keys file. To create the public/secret key pair on the source account, use ssh-keygen -t dsa -b 1024 To avoid break-ins if someone steals your private key, use a passphrase when creating the key pair, and use ssh-agent to only type the passphrase once per boot.

Convert a static lib (.a) into a dynamic one (.so):

gcc -shared -o libXxf86vm.so.1.0 \ -Wl,-soname,libXxf86vm.so.1 \ -Wl,--whole-archive,libXxf86vm.a,--no-whole-archive

Link with a shared library and store the path (avoid LD_LIBRARY_PATH):

gcc -o exename -L/path/to/dynamiclib/ -lnameofLib \ -Wl,-R/path/to/dynamiclib/ sourceCode1.c ... (This assumes that /path/to/dynamiclib/ contains libnameofLib.so)

Undelete from a ReiserFS partition:

First, take a backup of all files in that partition, as the undeletion process WILL mess up all existing files! Then, reiserfsck --rebuild-tree -S \ -l /var/tmp/recovery.log /dev/hda2 where /dev/hda2 is of course your own partition...

Create live presentations from your X11:

======================= To generate an XVID AVI ======================= First, run the server: vncserver -geometry 640x480 Then connect to it, and produce the recording file: vncrec -record samples.vncrec (e.g. when asked, connect to localhost:1) Hit F8 to bring up the menu to Stop. Then, check it with: vncrec -play samples.vncrec and encode it (at 64kbps) with: transcode -i samples.vncrec -x vnc -z -y xvid \ -g 1024x768 -o mitsos.avi -w 64 View it with: mplayer -vo x11 -fs mitsos.avi ======================================= To create an SWF that works for the Web ======================================= vncserver -geometry 640x480 vnc2swf video.swf localhost:1 F9 to start recording F8 to bring up menu and quit cp ../track.mp3 . edit_vnc2swf.py -o final.swf -a track.mp3 video.swf

wget fails, needs referrer:

wget -nc -r -l inf -k -p -T 10 -o log \ --referer=http://doom9.org/software2.htm \ http://doom9.org/doc-overview.htm

Convert text between different encodings:

iconv -f WINDOWS-1253 -t ISO-8859-7 Subtitle.GR.srt

Programmable debugging with GDB:

While trying to figure out what was going wrong with decoding my mp3PRO files, I used GDB's command lists: break init_plugins_from_dir run -ao pcm:file=o.wav -demuxer xmms a.mp3 display/i $pc set disassembly-flavor intel cont tbreak *(decode_file+0x360) break *(decode_file+0x432) commands 3 silent echo Reached point where sample rate is final

x /1x $my q end c ni set $my = (*($eax+4)+32) set $my2 = (*($eax+4)+0x10f78) echo Initial value of sample rate

x /1x $my x /1x $my2 break *(decode_file+0x39f) commands 4 silent echo Before call at decode_file+0x39f

x /1x $my x /1x $my2 cont end break *(decode_file+0x3a4) commands 5 silent echo After call at decode_file+0x39f

x /1x $my x /1x $my2 echo

cont end set $cnt = 0 break *mp3decDecode commands 6 silent set $cnt = $cnt + 1 if $cnt == 3 echo Entered mp3decDecode

set $breakDecodeFrame = 1 end cont end set $breakDecodeFrame = 0 break *(DecodeFrame__14CMp3PRODecoderPviPibPUcT3+0x1a3) commands 7 if $breakDecodeFrame == 1 echo This is where the sample rate is changed from

x /1x $my2 set $my3 = *($my2)+0x98 x /1x $my3 else cont end end c

Create syntax-highlighted HTML from code:

webcpp file.c file.html

Turn the clock back for an application:

Use this code to create a shared library called time-warp.so : gcc -D_GNU_SOURCE -shared -o time-warp.so time-warp.c -ldl ...and then, simply execute the desired binary with... LD_PRELOAD=/path/to/time-warp.so /path/to/binary [args] You can change how much back the clock will be set by changing OFFSET_IN_SECONDS in the code.

Dump hex and ASCII data:

bash$ echo Check the MBR data bash$ dd if=/dev/sdc bs=512 count=1 | hexdump -C 00000000 eb 48 90 d0 bc 00 7c fb 50 07 50 1f fc be 1b 7c |.H....|.P.P....|| 00000010 bf 1b 06 50 57 b9 e5 01 f3 a4 cb bd be 07 b1 04 |...PW...........| 00000020 38 6e 00 7c 09 75 13 83 c5 10 e2 f4 cd 18 8b f5 |8n.|.u..........| 00000030 83 c6 10 49 74 19 38 2c 74 f6 a0 b5 07 b4 03 02 |...It.8,t.......| 00000040 ff 00 00 20 01 00 00 00 00 02 fa 90 90 f6 c2 80 |... ............| ...

Unbuffered pipes (driven to grep, etc):

Install the "expect" package, and use unbuffer : bash$ unbuffer tail -f /var/log/apache/access.log | grep --line-buffered bozzo | ...

Python debugging:

If you find pdb a bit lacking, install ipdb and put something like this in your code: if specialConditionAtRuntime() or specialCmdLineArgPassed: import ipdb ipdb.set_trace() You can then have the nice tab-completion and colored syntax output of IPython during your debugging.

Compile-time C assert:

#define assert_static(e) \ do { \ enum { assert_static__ = 1/(e) }; \ } while (0) ... // This will fail at compile time in 64bit compilers long x; assert_static(sizeof(x) == 4);

Creating a MinGW import library from a .dll:

Method 1: pexports RASDLG.DLL | sed 's/^_//' > rasdlg.def dlltool --input-def rasdlg.def --dllname RASDLG.DLL --output-lib librasdlg.a -k cp librasdlg.a /Mingw/lib ranlib /Mingw/lib/librasdlg.a Method 2: pexports /c/WINDOWS/system32/python25.dll >py25.def dlltool -D python25.dll -d python25.def -l libpython25.a

Simple readline example:

#include <stdio.h> #include <stdlib.h> #include <readline/readline.h> #include <readline/history.h> int main ( void ) { while ( 1 ) { char * msg = readline ( "hello> " ); if ( msg ) { printf ( "echo: '%s'

" , msg ); add_history ( msg ); free ( msg ); } else exit ( 1 ); } return 0 ; }

Simple FFT example:

import math import scipy import sys if len ( sys . argv )> 1 : N = int ( sys . argv [ 1 ]) else : N = 16 Nlast = N / 2 angle = [ x * 360 ./ N for x in range ( 0 , N )] print "Angles:" , angle angleInRadians = [ x * 2 .* math . pi / 360 . for x in angle ] print "Broadcast frequencies of 1, 2, 3 and 8Hz" data = [ 3.5 * math . cos ( 1 .* x )+ 1.0 * math . cos ( 2 .* x )+ 2.5 * math . cos ( 3 .* x )+ 0.5 * math . cos ( 8 .* x ) for x in angleInRadians ] print "Performing FFT..." freq = scipy . fft ( data )[ 0 : Nlast + 1 ] freq /= N / 2 . freq [ 0 ] /= 2 . freq [- 1 ] /= 2 . print "Magnitudes:" , print "," . join ( "%4.2f" % x for x in abs ( freq )) def near ( f , g ): return abs ( f - g )< 1e-6 print "Verifying coefficients..." assert ( near ( freq [ 1 ], 3.5 )) assert ( near ( freq [ 2 ], 1.0 )) assert ( near ( freq [ 3 ], 2.5 )) assert ( near ( freq [ 8 ], 0.5 )) print "Coefficients OK."

Catch undefined behaviour with clang:

Just compile with: -fcatch-undefined-behavior