Benchmark of String Comparison Methods

The fastest way of identifying (non)empty strings in Perl is the direct method ($foo eq ''), the slowest is the regular expression ($foo =~ /^$/):


My own results:
             Rate regexp direct length
regexp  4219409/s     --   -76%   -78%
direct 17543860/s   316%     --    -7%
length 18867925/s   347%     8%     --
             Rate regexp length direct
regexp  5128205/s     --   -73%   -74%
length 18867925/s   268%     --    -6%
direct 20000000/s   290%     6%     --

Colors in svn

Install colorsvn for colorizing svn output and "replace" svn with colorsvn, e.g.:
alias svn='colorsvn'
Install colordiff for colorizing svn diff output and configure it to "replace" builtin diff command by editing ~/.colordiffrc, e.g.:
[helpers]
diff-cmd = colordiff

Forward and Reverse DNS Lookups

Hostname to IP:
perl -MSocket -lne 'print "$_: ", eval { inet_ntoa inet_aton $_ }' sites.txt
IP to hostname:
perl -MSocket -lne 'print "$_: ", scalar gethostbyaddr( inet_aton( $_ ), AF_INET )' ips.txt

US International Keyboard

To enable typing of international characters with AltGr key while using US layout:
setxkbmap -rules evdev -model evdev -layout us -variant altgr-intl
To make the changes persistant create /etc/X11/xorg.conf.d/10-keyboard-layout.conf file:
Section "InputClass"
        Identifier "keyboard-layout"
        Driver "evdev"
        MatchIsKeyboard "yes"
        Option "XkbLayout"  "us"
        Option "XkbVariant" "altgr-intl"
EndSection
Some useful key combinations:
  • áAltGr+a
  • äAltGr+q
  • čShift+AltGr+.+c
  • ďShift+AltGr+.+d
  • éAltGr+e
  • íAltGr+i
  • ĺAltGr+'+l
  • ľShift+AltGr+.+l
  • ňShift+AltGr+.+n
  • óAltGr+o
  • ôAltGr+6+o
  • ŕAltGr+'+r
  • šShift+AltGr+.+s
  • ťShift+AltGr+.+t
  • úAltGr+u
  • ýAltGr+'+y
  • žShift+AltGr+.+z
  • £Shift+AltGr+4
  • AltGr+5
  • ¥AltGr+-
  • ©AltGr+c
  • °Shift+AltGr+;
  • AltGr+;

Add New CA Certificate

System wide update of CA certificates (used by for example wget, curl, git):
mkdir -p /usr/local/share/ca-certificates
wget -O /usr/local/share/ca-certificates/ca_cert.pem https://example.com/ca_cert.pem
update-ca-certificates

Transform TAR Archived File Names

Prepend mypackage-0.1 to each archived file and compress the archive using bzip2 in one round:
tar --xform 's,^,mypackage-0.1/,' -cjf mypackage-0.1.tar.bz2 bin/ conf/ doc/ log/ Makefile

Unix Timestamp

Display current or specified date in Unix epoch:
date --utc +%s
date --utc +%s -d '2012-01-01 00:00:00'

Partition Cloning Over Network

Copy disk layout stored in MBR if needed:

Start Netcat receiver on the destination (10.0.0.1) and reread MBR after copying is finished:
nc -l -p 1234 > /dev/sda
partprobe /dev/sda
And send first 512 bytes from the source:
dd if=/dev/sda bs=512 count=1 | nc 10.0.0.1 1234
Copy desired partition:

Start Netcat receiver on the destination again:
nc -l -p 1234 > /dev/sda1
And send partition raw data over the wire:
dd if=/dev/sda1 | nc 10.0.0.1 1234

Reread MBR

Apply changes in disk layout:
partprobe

Copy Whole Disk over Network

rsync -axv -e ssh root@source:/ /mnt/root

Disk Encryption Using LUKS

Load device mapper, encrypt partition, optionally add second passphrase, open encrypted partition and format it using ReiserFS:
moprobe dm-mod
cryptsetup -c aes-xts-plain -y -s 512 luksFormat /dev/sda1
cryptsetup luksAddKey /dev/sda1
cryptsetup luksOpen /dev/sda1 root
mkfs.reiserfs -l ROOT /dev/mapper/root

PostgreSQL Timestamp and Unix Time Conversions

PostgreSQL timestamp to Unix time:
postgres=# select extract(epoch from timestamp with time zone '2011-01-02 03:04:05+06');
 date_part  
------------
 1293915845
(1 row)
Unix time to PostgreSQL timestamp:
postgres=# select to_timestamp(1293915845);
      to_timestamp      
------------------------
 2011-01-01 22:04:05+01
(1 row)

Capture and Replay Network Traffic

Example of capturing and replaying captured syslog traffic.

Capture:
tcpdump -v -s 0 -w syslog.dump -nn udp dst port 514
Rewrite:
tcprewrite --fixcsum \
           --enet-dmac=00:11:22:33:44:55 \
           --dstipmap=0.0.0.0/0:1.2.3.4/32 \
           --infile=syslog.dump \
           --outfile=syslog.redump
Replay:
tcpreplay -i eth0 --topspeed syslog.redump
tcpreplay -i eth0 --mbps=5.0 syslog.redump

POE::Component::Client::DNS

Examples of resolving a "large" number of domains in parallel using asynchronous DNS calls.

Version 3 API

Version 2 API

SVN Relocate

svn: Repository moved temporarily to 'https://svn.example.com/project/trunk'; please relocate
can be solved by
svn switch --relocate http://svn.example.com/project/trunk \
https://svn.example.com/project/trunk

Find out installed EPEL RPM packages

yum list installed | grep @epel

Global Subversion Settings for CA Certificates

How to avoid
Error validating server certificate for 'https://host.example.com:443':
when doing
svn co https://svn.example.com/project/trun
in a system-wide manner:
wget -O /etc/ssl/certs/your_cer.pem https://example.com/your_cert.pem
mkdir /etc/subversion
cat <<EOF > /etc/subversion/servers
[global]
ssl-authority-files = /etc/ssl/certs/your_cer.pem
EOF

Change Oracle User Password

$ su - oracle
$ sqlplus / as sysdba
SQL> alter user USER identified by "PASSWORD";

VirtualBox 4 USB support in Linux

Download VirtualBox Oracle VM VirtualBox Extension Pack and install it by VirtualBox → File → Preferences... → Extensions → Add package.

Then grant vboxusers groups access to the USB device filesystem:
$ grep 108 /etc/group
vboxusers:x:108:
$ vim /etc/fstab
usbfs  /proc/bus/usb  usbfs  auto,busgid=108,busmode=0775,devgid=108,devmode=0664  0  0

Password protected GRUB Legacy

Add password into the main GRUB section to password protect the interactive operations (i.e. editing menu entries and entering the command-line interface). Optionally add lock into a menu entry to prevent it from executing if no valid password is provided by the user.
$ grub-md5-crypt 
Password: 
Retype password: 
$1$K0Kh10$OmQNQOthH8jppDFQ5TYx5/

$ chmod 600 /boot/grub/menu.lst
$ vim /boot/grub/menu.lst
default 0
...
password --md5sum $1$K0Kh10$OmQNQOthH8jppDFQ5TYx5/

title Linux
kernel ...

title OpenBSD
lock
kernel ...

Create a svn tag from trunk

svn mkdir http://svn.example.com/project/tags -m "release directory"
svn copy http://svn.example.com/project/trunk \
         http://svn.example.com/project/tags/1.0 -m "release 1.0"

Nicer Oracle Date Output

Oracle date output including full year, month, day, hours, minutes and seconds:
SQL> alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
SQL> alter session set nls_timestamp_format = 'yyyy-mm-dd hh24:mi:ss';
SQL> select foo_date from bar_table where rownum <= 1;

FOO_DATE
-------------------
2012-01-01 01:02:03

Tunneling VNC connection

Connect to a VNC server listening on localhost on port 5901 (+1 than usual) using tunneling:
vncviewer -via yourusername@remotemachine localhost:1

How to build RPM packages as a regular user

Install the necessary packages as root:
sudo yum install rpmdevtools rpm-build redhat-rpm-config
Initialize the directory hierarchy for your custom build RPM packages as a regular user:
rpmdev-setuptree
Optionally add additional attributes into ~/.rpmmacros, e.g.:
echo '%packager Your Name <your.name@foo.bar>' >> ~/.rpmmacros
Use rpmbuild to build RPM from a source RPM or from a SPEC file:
rpmbuild --rebuild package.src.rpm
rpmbuild -bb package.spec