Subject: Re: IP-aliasing

From: Craig Sanders <[email-masked]>
Date: Sat, 3 Aug 1996 21:30:23 +1000 (EST)

On Fri, 26 Jul 1996, Behan Webster wrote:

> I've also written the following simple script for one of the machines
> I manage that is used as a multi-homed web site (it has about 6 ip
> addresses right now.) Just give it a list of ip addresses on the
> command line. It's to be used once at boot time.
>
> >#!/usr/bin/perl
> > [...script deleted...]

> I hope this is helpful.

it's certainly a great idea.  Thanks, I can use something like that in my
wetware collection of useful tricks!

what i will never understand, though, is why people use perl for jobs that
sh is much better suited to.  

perl is great if you want awk/sed/grep/kitchen sink all rolled into one
scripting language. but there's a price - perl's slower and bigger and
takes longer to load.

here's the same thing in sh

---cut here---
#! /bin/sh

device=eth0             # aliased device
netmask=255.255.255.0   # your netmask

interface=0
for i in $@ ; do
    broadcast=`echo $i | sed -e 's/[^.]$/255/'`
    ifconfig $device:$interface $i netmask $netmask broadcast $broadcast
    route add -host $i dev $device:$interface
    interface=$[interface + 1]
done
---cut here---

(NB: this script should work BUT is untested)

actually, i can see problems in this (and in your perl implementation).

The netmask & broadcast address could be wrong in some (not uncommon)
circumstances...it should be possible, however, to make the script
generic by calculating the correct values for netmask and broadcast from
an arg like '203.16.167.50/24'.

it might also be worthwhile calculating the correct network address and
doing 'route add -net $netaddr $device:$interface'...would help when the
machine has ip alias addresses in multiple networks or subnets.

(of course, this extra stuff probably make it worth the overhead of
implementing it in perl :-)

Craig

---

From: Rob Browning <[email-masked]>
Date: 03 Aug 1996 11:16:51 -0500

> scripting language. but there's a price - perl's slower and bigger and

What I will never understand is why people assume that perl is so much
slower.  For most things it's much faster since it doesn't have to
launch a separate program for most operations.

Seriously, perl is usually faster.  Just to give you some idea, here's
a comparison (standard disclaimers apply -- this only applies for the
given programs etc., but then that's what we were talking about, a
perl and sh version of the same program).

In terms of load time, I ran this ten times and got the same result
each time.

(555) time sh -c "echo hello"
hello
0.03user 0.01system 0:00.04elapsed 93%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (147major+29minor)pagefaults 0swaps

(568) time perl -e 'print "hello\n"'
hello
0.02user 0.01system 0:00.02elapsed 107%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (159major+29minor)pagefaults 0swaps

So perl is at least as fast in that department. Now take essentially
your sh script and time it:

#!/bin/sh

interface=0
for i in $@ ; do

	# Let's assume these take the same time in either language:
  # ifconfig $device:$interface $i netmask $netmask broadcast $broadcast
  # route add -host $i dev $device:$interface

(517) time speed-test-bash foo bar baz bax
0.08user 0.07system 0:00.14elapsed 102%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (464major+532minor)pagefaults 0swaps

(531) time speed-test-bash foo bar baz bax
0.04user 0.11system 0:00.14elapsed 102%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (464major+532minor)pagefaults 0swaps

Now let's do the same thing in perl.

#!/usr/bin/perl
 
$device=eth0;             # aliased device
$netmask=255.255.255.0;   # your netmask

$interface=0;
foreach $arg (@ARGV) {

  $broadcast = $arg;
	$broadcast =~ s/[^.]$/255/;
	
	# Let's assume these take the same time in either language:
  # ifconfig $device:$interface $i netmask $netmask broadcast $broadcast
  # route add -host $i dev $device:$interface
	
  $interface++;
}

(534) time speed-test-perl foo bar baz bax
0.02user 0.01system 0:00.02elapsed 120%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (169major+30minor)pagefaults 0swaps

(535) time speed-test-perl foo bar baz bax
0.02user 0.01system 0:00.02elapsed 120%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (169major+30minor)pagefaults 0swaps
 
As you can see, in real time sh is about seven times slower.  That's
no doubt because you have to fork all those copies of sed, and the
subshells to handle the piping.

Just FYI.
--
Rob

---

From: Craig Sanders <[email-masked]>
Date: Sun, 4 Aug 1996 12:33:23 +1000 (EST)

On 3 Aug 1996, Rob Browning wrote:

> > what i will never understand, though, is why people use perl for
> > jobs that sh is much better suited to.
> >
> > perl is great if you want awk/sed/grep/kitchen sink all rolled into
> > one scripting language. but there's a price - perl's slower and
> > bigger and takes longer to load.
>

sorry, my language was unclear - i implied that perl ran slower when all
i really meant was that it takes longer to load and is overkill for many
small tasks.

perl RUNS faster, that's a fact.

perl LOADS slower, that's also a fact.

perl also requires much more memory, which on a 4mb or 8mb system will
slow things down even further due to extra swapping.

therefore tiny scripts are better done in sh, which is a) smaller and
therefore loads faster, and b) almost always in memory/cache anyway.

Of course, as you say, if you need to fork sed (or whatever) many times
then perl is clearly a winner.

my basic rule of thumb is:

    if the script is mostly a program launcher then use sh.  otherwise
    use awk or perl or whatever language best suits the application.

one big advantage to sh for me is that if i find that i am entering
similar or repetitive commands at the prompt, i can easily cut and paste
the command line into a text file. Add "#! /bin/sh", some extra line
breaks to make it more readable, some comments to explain what it does
and what arguments it takes, and i have a new script.

I'd like to see a bourne-like shell with perl-like regexp stuff (mainly
sed & grep) built in - i'd switch to that in a flash. As a built-in it
would be extremely fast, and i'd still be able to test/debug/prototype
my scripts interactively on the command line :-)

Craig

---

From: [email-masked] (Emilio Lopes)
Date: Tue, 6 Aug 96 19:49 EST

>>>>> "CS" == Craig Sanders <[email-masked]> wrote:

CS> I'd like to see a bourne-like shell with perl-like regexp stuff
CS> (mainly sed & grep) built in - i'd switch to that in a flash.

People may say it's called ksh93. IMHO, it may seem somewhat
interesting, but I really don't like it. Look likes tcl... :-)

Please no flame war!

Emilio.

-- 
                 Emilio C. Lopes <mailto:[email-masked]>
               Instituto de Fisica da Universidade de Sao Paulo
             Caixa Postal 66318, 05389-970 Sao Paulo - SP, BRAZIL
              Phone: (+55) (11) 818-6724 (Voice) / 818-6715 (Fax)

---

From: Craig Sanders <[email-masked]>
Date: Mon, 12 Aug 1996 10:58:49 +1000 (EST)

> interesting, but I really don't like it. Look likes tcl... :-)

zsh seems to have some of these features.

see http://www.mal.com/zsh/ for full details, but this looks useful:

        and a substitution modifier: 

        % echo $name:s/foo/bar/
        bar.c
        % ls
        foo.c    foo.h    foo.o    foo.pro
        % for i in foo.*; mv $i $i:s/foo/bar/
        % ls
        bar.c    bar.h    bar.o    bar.pro

it's only simple substitution, though.  no regexp.

        (siva-cas) ~$ zsh
        % fred=xyz.abc.zzz
        % echo $fred
        xyz.abc.zzz

lets try a regexp subsitution ala sed:
        % echo $fred:s/\(...\).\(...\).\(...\)/\3\2\1/
        xyz.abc.zzz
ok.  that obviously doesn't work.  lets try a different format:
        % echo $fred:s/(...).(...).(...)/\3\2\1/
        xyz.abc.zzz
nope. 

you can string together multiple substitions, though:
        % echo $fred:s/abc/111/                       
        xyz.111.zzz
        % echo $fred:s/abc/111/:s/xyz/222/
        222.111.zzz
        % echo $fred:s/abc/111/:s/xyz/222/:s/zzz/333/
        222.111.333

zsh looks like it's got nice stuff out of sh/bash, ksh, and csh.

looks useful, but it doesn't get eliminate the need to fork sed and
grep.

hmmm...the substitution facility could be useful for standardising
responses in a case statement:

read yesno
case $yesno:l:s/yes/y/:s/no/n/ in
  y) do_yes ;; 
  n) do_no ;;
  *) do_error ;;
esac

it's probably too non-standard to use for debian.{pre,post}{rm,inst} scripts
but i might switch to it for my personal use.