Friday, 30 May 2008

Port forwarding with ssh

Let's just say that I've a box with an sshd listening on port 22, but I want it to accept as well connections on port, for example, 443. There might be many many ways to do it, but one way is to open an ssh tunnel, by running this as root on the said box:

ssh -g -N -f -L 443:localhost:22 user@localhost

-f forks, so the tunnel stays open in the background as long as the box is up. -g allows connections from the outside world. -N is there to avoid executing a command (just forward the port). And -L gives the tunnel specification.

Tuesday, 27 May 2008

Process substitution in bash

I like the process substitution syntax in bash. Since I don't expect everyone to already use it at lengths, a small note on it might be welcomed.

Process substitution connects the input or the output of a process list to a pipe whose other end is connected to the rest of your command line. For example, this will list your files in reverse order:

sort --reverse <(ls)

You'd said, I could have used a good old pipe, as in ls | sort --reverse. Fair enough, but sometimes you want more than one input, or output. That's where process substitution comes to the rescue.

I find this particularly useful with diff(1). I like diff. Don't you like diff? (GNU diff, I mean, the one with -u for unified diffs. It don't mean a thing if it ain't got that switch.) I read unified diffs every morning at breakfast. (I swear it's true.)

So let's see, I want to compare the output of two commands. Let's say I want to compare two perl optrees. (A perl optree is the intermediate form in which perl compiles your program before executing them.) To get a good, but concise, overview of a perl optree, dumped on the standard error stream, my favourite command is:
perl -MO=Concise

(I think I'll make myself an alias for that one day.)

Let's say my almost similar programs are in files and To get a diff of their optrees, the command will then be:
diff -u <(perl -MO=Concise 2>&1) <(perl -MO=Concise 2>&1)

(Note that I redirected stderr to stdout.) Et voilà.

And if that's too much typing, there's always history substitution:
perl -MO=Concise 2>&1
diff -u <(!!) <(!!:s/foo/bar/)

Monday, 26 May 2008

On Naples and its garbage

The situation of garbage disposal in Naples and the monopoly that Camorra (the Napolitan Mafia) acquired upon it is an application of the formula:

mafia = capitalism - state

What happened there? A couple of decades ago, the Camorra began to invest in garbage disposal companies. Garbage disposal and its treatment are quite expensive, especially when the laws require not to dump dangerous chemicals anywhere in the fields or the rivers. But if you don't care about polluting an entire country, garbage disposal, at the market prices, is very lucrative, because it allows for enormous margins. Using intimidation and corruption to wipe out local competition, and selling its services to industrials to cheaply dispose of their waste, (thus gaining approval from the powerful industry bosses), the Camorra has now transformed the Campania in a wasteland where people are a lot more likely to develop cancers than in other parts of Italy.

Fair competition is part of capitalism, but without an effective judicial power to prevent violence against competitors, the game is biased. Moreover, without a government to edict and guarantee safeguards, protecting its citizens from whatever consequences unrestrained capitalism might have on the environment or on the society, ecological and social catastrophes are bound to happen -- at the very profit of the bad ones.

My formula of course applies as well to the Sicilian Mafia, born at the end of the XIXth century around Palermo; at that time, it took advantage of a very good economical situation in Sicily and of a default of government, Italy being a young kingdom, and Sicily just out of feudalism.

Thus, every time I read on the internets anarcho-capitalism, I mentally translate it into Mafia rule waiting to happen. I'm sure the Honoured Society would be happy to see a libertarian elected in the White House, even if this libertarian is the most honest man on earth. (The Mafia doesn't have political opinions -- it helps whoever will help it.)

(As an aside, Tony Soprano comes from a Napolitan family, and officially works in garbage disposal. The writers were documented...)

Friday, 9 May 2008

Why People Are Passionate About Perl

brian d foy asks why people are passionate about Perl. So, brian, here are a pumpking's answers to your questions.

The person who introduced me to Perl showed me that I could throw away all those fragile sed and awk scripts and get the job done much quickier. So that gets us to the second question:

I first starting using Perl to process very large data files. Or at least they were considered very large at the time: less than 2 Go, I think. But I quickly grasped the thing and began to wrote small scripts to automate various things on my computer (and others').

I kept using Perl because it was fun. And it still is.

I can't stop thinking about Perl because... they won't let me! If I stop thinking about Perl, I know they're out there to get me, lurking in the dark corners of the intarweb, with their bug reports, their suggestions and their patches.

I'm still using Perl because, well, basically, because I'm paid for it. But on the other hand, I chose a job where I was getting paid to use Perl. I could have chosen a Java, a C, or (my goodness) a PHP job. But with a proper Perl job you have the CPAN at hand, ready to be used. So you can concentrate with the problem solving and the business side of your applications, because almost all the libraries you'll need are already there.

I get other people to use Perl by hitting them on the head with a -- no wait. Actually that's the most interesting question. I get other people to use Perl by trying to make Perl a less annoying language, in my own scale.

I also program in C, but mostly when I have to patch perl itself -- the only place where you can't use Perl.