I want to script a change to /etc/hosts

I’m now not at home, using a USB dongle to connect to an UMTS network for internet access.
Interestingly, network manager works with this thing. What it doesn’t do is adjust the hosts file to
the new IP it gets on each reconnect.

I can hook a script in /etc/ppp/ip-up.local, that part I know how to do it. What I don’t know is how
to edit the line in the host file. The line is like this:

10.9.1.1 minas-tirith.valinor minas-tirith

I probably would have to locate that line, delete it, then add a new one with the proper IP (the
script gets the IP as part of the data, no problem there). Sed, perhaps? Do you know a sed manual
for dummies?


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” (Minas Tirith))

Am 02.09.2012 13:05, schrieb Carlos E. R.:
> Sed, perhaps? Do you know a sed manual
> for dummies?
This is what I look at when I use sed (being myself not a sed wizard but
a dummy)
http://www.grymoire.com/Unix/Sed.html


PC: oS 12.2 x86_64 | i7-2600@3.40GHz | 16GB | KDE 4.8.4 | GeForce GT 420
ThinkPad E320: oS 12.1 x86_64 | i3@2.30GHz | 8GB | KDE 4.8.5 | HD 3000
eCAFE 800: oS 12.1 i586 | AMD Geode LX 800@500MHz | 512MB | KDE 3.5.10

Something similar to that maybe (for testing I used a variable NEWIP with the new ip address)


sed "s/.*minas-tirith.valinor minas-tirith/$NEWIP minas-tirith.valinor minas-tirith/" test

yo probably want an inplace change with sed -i (you can add a suffix to the -i to get automatic backup of the changed file)

I’m not sure why you even need that line. My laptop works fine without their LAN ip in the hosts file.

I shall assume that the hostname “minas-tirith” is fixed, and that the IP address is in shell variable “$IP”.

Try:


cp /etc/hosts /etc/hosts.old
sed -e '/minas-tirith/s=^[0-9\.]*='"$IP"'=' /etc/hosts.old > /etc/hosts

On 2012-09-02 13:23, Martin Helm wrote:
> Am 02.09.2012 13:05, schrieb Carlos E. R.:
>> Sed, perhaps? Do you know a sed manual
>> for dummies?
> This is what I look at when I use sed (being myself not a sed wizard but
> a dummy)
> http://www.grymoire.com/Unix/Sed.html

Thanks! I have some reading to do.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” (Minas Tirith))

On 2012-09-02 15:26, nrickert wrote:
>
> I’m not sure why you even need that line. My laptop works fine without
> their LAN ip in the hosts file.

One item does not: postfix. And I use postfix to do the mail sending, instead of directly from
thunderbird, because sometimes I use Pine instead.

> I shall assume that the hostname “minas-tirith” is fixed, and that the
> IP address is in shell variable “$IP”.
>
> Try:
>
> Code:
> --------------------
>
> cp /etc/hosts /etc/hosts.old
> sed -e ‘/minas-tirith/s=^[0-9.]*=’"$IP"’=’ /etc/hosts.old > /etc/hosts
>
> --------------------

Yes, that appears to work, I tried on a temporary file :slight_smile:

Thanks! :slight_smile:

Now my next problem is that ip-up is not executing ip-up.local :-/
Ah, it is, but silently. I added logger commands to see my way.

This is the ip-up.local script. Do you see a problem there?
I started by copying the system ip-up script and removed almost all.


#!/bin/bash

unset POSIXLY_CORRECT ; set +o posix # we're using non-posix bash features

BASENAME=${0##*/}
INTERFACE=$1
DEVICE=$2
SPEED=$3
LOCALIP=$4
REMOTEIP=$5
IPPARAM=$6

# send all output to syslog
#exec > >(logger -p security.notice -t "$BASENAME") 2>&1

#TERM=raw
#export TERM

logger -p security.notice "inside .local $INTERFACE $DEVICE"


case "$BASENAME" in
*-up)
logger -p security.notice "inside up.local $INTERFACE $DEVICE"
if  $INTERFACE = "ppp0" ]; then

# All conectar con Yoigo la IP local varía, y postfix no funciona bien.
cp /etc/hosts /etc/hosts.old
sed -e '/minas-tirith/s=^[0-9\.]*='"$IP"'=' /etc/hosts.old > /etc/hosts
rcpostfix reload
fi

;;
esac


The log entry says:


Sep  2 19:46:53 minas-tirith logger: inside .local ip-up.local ppp0 /dev/ttyUSB1

Thus basename is not matching, I don’t see the second entry. Ah, of course, the “local” part. Ok,
now the script works!

It is now:


case "$BASENAME" in
*-up.local)
logger -p security.notice "inside up.local $INTERFACE $DEVICE"
if  $INTERFACE = "ppp0" ]; then

# All conectar con Yoigo la IP local varía, y postfix no funciona bien.
cp /etc/hosts /etc/hosts.old
sed -e '/minas-tirith/s=^[0-9\.]*='"$LOCALIP"'=' /etc/hosts.old > /etc/hosts
/sbin/rcpostfix reload
fi

;;
esac

Interesting, I see the errors just after I copy them here to send :wink:


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” (Minas Tirith))