Perl : Convert time from Epoch to local time

watch

This little Perl one-liner can get handy when you need to translate “time in seconds since the Epoch” (for example in logs) to local time :
% perl -e 'print scalar(localtime(1202484725)), "\n";'
Fri Feb 8 16:32:05 2008
%

This was pretty useful today when browsing through Nagios event logs, where times are given in seconds from the Epoch.

By the way, the Epoch is defined as 00:00 UTC on January, 1st, 1970.

Perl : Optimizing pattern searches with Regexp::Assemble

When you perform a pattern matching with multiple “or” (|) clauses, such as /pattern1|pattern2|pattern3/, Perl regexp engine will try to match each of them one after the other in sequence, resulting in poor performance if you have a long list of “or” clauses.

In order to optimize such a pattern matching, you can use the Regexp::Assemble module.

Continue reading Perl : Optimizing pattern searches with Regexp::Assemble

Linux : Taking control of Virtual Terminals (VT) from command line

When you use Linux in text mode (as opposed to with an X server), you readily have access to multiple Virtual Terminals (aka VT for short) by hitting one of your <Alt-Fn> keys (if you are running a X server, you’ll need to hit <Ctrl-Alt-Fn> simultaneously).

This lets you access one of the VTs which are initialized at boot time, but won’t let you create new ones even if your kernel configuration would allow more VTs. Furthermore, what if you want to deal with VTs from a script ?

This post covers the 3 commands which will let you control your VTs from the command line or from a script.

Continue reading Linux : Taking control of Virtual Terminals (VT) from command line

Unix : the “script” command

The script command is a must for any unix sysadmin.

Once invoked, it will faithfully write anything you typed as well as any output generated in your terminal into a file of your choice (defaults to “typescript”).

This is great when you want to document everything you did on a specific server, for example.
spaghetti:~$ script
Script started, file is typescript
spaghetti:~$

When launched, you don’t see anything, but everything displayed goes to a file as well as the terminal.

As usual see the man page for details (like appending instead of creating a new file, launching another command instead of your default shell, and so on)

sed : replacing a text in a file

To replace a text in a file, you can invoke sed as in the following example :

% cat file.txt | sed -e 's/text/replacement/g' > result.txt

This will change all the occurences of “text” to “replacement” in “file.txt” and output the result in “result.txt”

Note : As suggested by Matthias from adminlife in the comments, if you wanted to do “in place” text replacement (that is modify the file without a temporary file in between), you can do the following :

sed -i ’s/text/replacement/g’ file.txt

For more complicated text manipulations you might consider moving to Perl, but sometimes you don’t need the sledge-hammer 🙂

Solaris 10: On which CD is that XYZ package ?

If you want to know on which CD is a package, without :

  1. Mounting CD
  2. Searching
  3. Unmounting
  4. Swear and
  5. Go back to 1

Then you can :

  1. Mount CD #1 (mount -F hsfs /dev/dsk/<CDROM DEVICE> /mnt or, if you have automount cd /cdrom/cdrom0 or something like that)
  2. Go in the Solaris_10/Product directory of the CD
  3. Do grep -l <PACKAGE NAME> .virtual_packagetoc_*
  4. which will output the .virtual_packagetoc_N where N is the number of the CD holding that package.

Exemple :

# pwd
/mnt/Solaris_10/Product
# grep -l SUNWzsh .virtual_packagetoc_*
.virtual_packagetoc_5
#

So SUNWzsh, the package for ZSH shell, is on CD #5 of Solaris 10 distribution (damn, I don’t have it !)

This tip is courtesy of BlaF (thanks dude !)

CFEngine – Installing on Debian GNU/Linux

In this post we’ll install CFEngine on a Debian system. Debian make is really simple to install any packages, so let’s follow the “standard” package installation procedure (I’ll assume that apt is correctly setup on your system ! If you have troubles with it, let me know, I’ll write a post on this topic).
Continue reading CFEngine – Installing on Debian GNU/Linux

Keeping track of changes with cfengine and SubVersioN

Cfengine is a tool which purpose is to describe what is a healthy system and how to bring it back to normal when something fails.

I won’t go into an explanation about how cfengine works, because the project webpage already has a neat tutorial and complete reference. Instead of that, I’ll explain how I used cfengine to build a fool proof Linux firewall.

Continue reading Keeping track of changes with cfengine and SubVersioN

OpenBSD : Give money !!

not to me !! 😀

The OpenBSD project needs money to hold its events (such as hackaton, where developpers gather to implement features, or usual running costs).

If you think you don’t use OpenBSD, think again ! The project OpenSSH, which implements a free, and secure SSH implementation, comes from OpenBSD. It is widely used in many OSes and appliances …

We need OpenBSD !! Don’t hesitate to make a donation !

The original post
The OpenBSD project

The OpenSSH sister project