/ Markus Amersdorfer:home / university / about:me /
\ Say NO to Software-Patents! \

Useful Scripts and Similar Stuff


Lists IP-address(es) of the given or else all available network devices.

  /sbin/ifconfig $1 | grep inet | awk '{print $2}' | sed 's/^addr://g'

Mozilla Startup

At least since Mozilla RC3, starting mozilla again if an instance of it is already running, will result in the Profile Manager's window asking you to choose a profile or create a new one. Just trying to use the current one will not work.
This is especially a quite boring behaviour if the browser is started from another application (for example by double-clicking on a link within my mail client Sylpheed).

I wrote the following script which uses the -remote option previously known from Netscape 4.x in order to get around this problem. Just always use this script to start mozilla ... and everything's fine. :)


  # /opt/mozilla_start-this_max
  # assuming we get a URL as parameter $1, we either start mozilla and open the URL,
  # or we just open a new window using an already running mozilla-process.
  # for further details check out
  #   /opt/mozilla/mozilla --help
  # and
  #   http://www.mozilla.org/unix/remote.html

  if /bin/ps ax | /bin/grep mozilla-bin | /bin/grep -v grep > /dev/null; then
    # an instance of mozilla is already running, just open a new window
    /opt/mozilla/mozilla -remote "openurl($1, new-window)"
    # no instance of mozilla present, start totally new.
    /opt/mozilla/mozilla $1

A cooler startup-script by August Mayer:

if mozilla -remote "ping()"; then
   mozilla -remote "xfeDoCommand(openBrowser)";
   if [ "x$*" != "x" ]; then
      mozilla -remote "openURL($*)"
   mozilla -splash $* &

POP3 over SSH-tunnel

I use fetchmail to get the mails from all of my POP3-accounts. It runs as root (but can be run as $USER too) and is started via a cron-job. A simple max ~$ touch nomails will result in no mails being fetched until the file is removed again.
  # as one line:
  poll localhost  port 10110  protocol pop3  username pop-server-username
  is local-username  password "xxxxxxx"  fetchall 
  preconnect "/usr/bin/ssh -f -C -x -L 10110:pop.server.net:110 ssh-server-username@ssh-server /bin/sleep 10"

  */10 * * * * root /usr/bin/test -f /home/max/nomails || /usr/bin/fetchmail -s -f /root/.fetchmailrc

SSH tricks

SSH tricks.



As I continuously forget how to build for-loops using the BASH, here are two ways to do so...

  for i in `/usr/bin/seq 1 10`
    /bin/echo $i

  for (( i=11; i<=20; i++ ))
    /bin/echo $i


As can be found in the BASH Programming - Introduction HOWTO, here are the redirection rules:

Redirect stdout to file:
  command > stdout.txt

Redirect stderr to file:
  command 2> stderr.txt

Redirect both stdout and stderr to file:
  command &> stdout-and-stderr.txt

Redirect stdout to stderr:
  command 1>&2

Redirect stderr to stdout:
  command 2>&1

Thus, you can easily grep on the stderr by redirecting it to stdout and performing a normal grep, something like: command 2>&1 | grep special-errors.


If you are placed behind an HTTP-proxy (with or without user-authentication), you can not simply use ssh to connect to an outside server. Everything else but HTTP is locked.
If for some reason you desperately need to get out, do the following:

server (outside the proxy):
  apt-get install httptunnel
  hts -F localhost:22 1234

client (inside the proxy):
  apt-get install httptunnel
  htc -F 5678 -A proxyuser:proxypwd -P proxy-server remote-host:1234
  ssh -p 5678 localhost

With this, the local port "5678" is tunnelled to "remote-host's" port "22" via an HTTP-protocol-based "connection" between the HTTP-Tunnel-Client and the HTTP-Tunnel-Server (port "1234").
SSH connects to localhost:5678 and reaches remote-host:22 (in this configuration).

Check for old but still used Libraries

From time to time, security-updates are necessary. Once installed, every program started afterwards uses the new binaries.
If the security-update covers libraries, it is necessary to restart all programs which use these libraries. Just doing an "apt-get update && apt-get upgrade -u" might not suffice in these cases!
Especially with servers (which tend to be running 24/7, or at least SHOULD do so :) ...), it is important to know if and which programs/services are still using such old (and already replaced) libraries. And that's exactly what check-old-libraries.sh does: find these programs.

# Library-Security-Updates via "apt-get update && apt-get upgrade" 
# replace the old libraries with the new ones.
# Programs which continue running (e.g. services) still use the
# old ones. They can be tracked down by this script, a mail is
# sent to admin@subnet.at to have him realize the situation.
#   Sample output:
#     Restart "sshd", it uses the old library "/usr/lib/i586/libcrypto.so.0.9.6.dpkg-new".
#  *) Save this script as /root/bin/check-old-libraries.sh
#  *) Create a cron-job to run it once a day
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   See the GNU General Public License for more details:
#     http://www.fsf.org/licensing/licenses/gpl.txt
#   If you cannot access the GNU General Public License via the link
#   above, write to the Free Software Foundation, Inc., 59 Temple Place,
#   Suite 330, Boston, MA 02111-1307 USA 
# Copyright 2003 Markus Amersdorfer

lsof +L1 | grep dpkg-new$ | awk '{print $1 " " $9}' | sort | uniq | awk '{print "Restart \"" $1 "\", it uses the old library \"" $2 "\"."}'

Create a diff file

I've recently patched action.c from Sylpheed-0.9.0:

Creating the diff file:
  diff --unified --recursive --new-file sylpheed-0.9.0_ORIGINAL/ sylpheed-0.9.0 > actions-patch_sylpheed-0.9.0

Patching the original code (with "./sylpheed-0.9.0/" being the original code now):
  patch -p0 < actions-patch_sylpheed-0.9.0

vi stuff

search and replace:
Once in current line only: s/pattern/newpattern/
All occurences in current line: s/pattern/newpattern/g
Only first occurence in every line in the whole file: %s/pattern/newpattern/
All occurences in the whole file: %s/pattern/newpattern/g
Add-on: % equals 1,$ which is the range "line 1 to end of file"

Compiling your own Linux Kernel

Once you got the source-code (e.g. from one of the mirrors of kernel.org), unpack it to /usr/src/ and create a symlink /usr/src/linux -> /usr/src/linux-2.4.20 (or similar). If you run Debian GNU/Linux, best use kernel-package. Otherwise, run:

  cd /usr/src/linux
  make xconfig
  make dep clean bzImage modules modules_install
  cp System.map /boot/System.map /boot/System.map-2.4.20
  cp arch/i386/boot/bzImage /boot/vmlinuz-2.4.20
  cd /boot/
  ln -ivs System.map-2.4.20 System.map
  ln -ivs vmlinuz-2.4.20 vmlinuz

Edit your /etc/lilo.conf accordingly and run lilo (or edit /boot/grub/menu.lst if you use GRUB).

tcpdump printing ACSII

In order to get a human-readable ASCII-printout of the network traffic to a certain machine, use the call "tcpdump -n -X -s 1500 host".
(-X enables ASCII, -s 1500 sets the maximum length of a packet's data to be shown. With the default of 68 you won't see the packet's content/payload.)

Print a file's content, beginning with line "n"

To print a file's content starting with a specific line, e.g. the 200th, run:

  awk '(FNR >= 16) { print }' $file

Use a list of files

Suppose you have a file "my-files.txt" holding a list of files, each of them in a single line. To execute a command for each of them (e.g. to untar all those .gz files):

Using list of .tar.gz-files in "my-files.txt":
  cat my-files.txt | xargs -n 1 tar xzvf

Directly in the directory (with "my-files.txt"):
  echo *.tar.gz | xargs -n 1 tar xzvf

If you want to use that list of files as an indicator for "tar" to tell it which files to extract or archive, use tar's option "-T":

  tar cvzf my-tarball.tar.gz -T my-files.txt

Rename all *.xxx to *.yyy

In order to rename all *.xxx files to *.yyy, you may either run "rename s/\.xxx$/\.yyy/ *", or you may experiment a little with sed and pipes: "ls *.xxx | sed 's/\(.*\)\.xxx$/mv & \1.yyy/' | sh". (Run "man sed" and "/\\1" to search for the explanation of "& \1" in the last command...)

Valid HTML 4.01! Valid CSS! Created with Vim [Blue Ribbon Campaign icon]
© Markus Amersdorfer
last modified: Tuesday, 23-Feb-2010 15:42:11 UTC