Working with pon/poff in Ubuntu

I've set up the following script to connect to the office VPN on a Ubuntu laptop:

--------8<--------
#!/bin/sh

case "$1" in
    on)
        sudo pon office
        # Need to wait for connection to establish
        sleep 5
        # Now set up routes
        sudo route add -net 192.168.1.0/24 ppp0
        sudo route add -net 192.168.96.0/20 ppp0
        ;;
    off)
        sudo route del -net 192.168.96.0/20 ppp0
        sudo route del -net 192.168.1.0/24 ppp0
        sudo pon office
        ;;
    *)
        echo "Usage: $0 {on|off}"
esac
--------8<--------

It works (i.e. I've got pppd working etc), which I'm feeling very pleased about, but something tells me that using sleep 5 in my script is a bit naff, and I've not done anything about connecting to the office domain name servers.

Here are my questions:

(1) Should I be setting up and taking down those routes in scripts in /etc/ppp/ip-up.d and /etc/ppp/ip-down.d, and if so how to I script it to be applied for "office" only?

(2) I notice what looks like a clever looking script for setting up DNS from the peer /etc/ppp/ip-up.d/0000usepeerdns, which uses resolvconf (which apt-get doesn't seem to know about in my set-up). Is this what I should be using to get my resolver to use the office DNS, and if so, what's the sensible way to get the right package for this?
LVL 17
rstaveleyAsked:
Who is Participating?
 
nociConnect With a Mentor Software EngineerCommented:
IMHO, in the off case I thing "sudo poff office" would work as intended

Ad 1)
From "man pppd":

      ipparam string
              Provides  an  extra  parameter to the ip-up and ip-down scripts.  If this option is given, the string supplied is given as the 6th
              parameter to those scripts.

And yes the up & down scripts are a better place to specify routeing ...
the ipparam should be in the options file for your office settings.

Ad 2)
Do you mean the file resolve.conf? it normaly resides in /etc.

What the code in 000usepeerdns does, is looking where the resolv.conf is, and save the old one & replace it with one that ppp created
in ip-down it undoes this rename.


0
 
rstaveleyAuthor Commented:
I see that resolvconf isn't part of the Ubuntu distribution, because there are some bugs associated with it. Is there an alternative that's known to work?
0
 
nociSoftware EngineerCommented:
btw, i meant /etc/resolv.conf in the previous remark, not /etc/resolve.conf

0
 
rstaveleyAuthor Commented:
Thanks for the comments noci. I'll do as you suggest for 1.

Ref 2: The script is indeed resolvconf, which sets up /etc/resolv.conf with the DNS reported by the connection. Here's the Debian package: http://packages.debian.org/unstable/net/resolvconf. I guess I need to set up my Ubuntu repositories to get apt-get to get it. I'm explicitly overwriting resolv.conf with hard-coded values for the time being, which works for as long as the office VPN isn't changed. I'm still finding my way with Ubuntu.
0
 
nociSoftware EngineerCommented:
Here is the code rewritten from gentoo:

in ip-up:
---8<---
if [ "$USEPEERDNS" ]; then

        # add the server supplied DNS entries to /etc/resolv.conf
        # (taken from debian's 0000usepeerdns)

        # follow any symlink to find the real file
        REALRESOLVCONF=$(readlink --canonicalize /etc/resolv.conf)

        if [ "$REALRESOLVCONF" != "/etc/ppp/resolv.conf" ]; then

                # merge the new nameservers with the other options from the old configuration
                {
                        grep --invert-match '^nameserver[[:space:]]' $REALRESOLVCONF
                        cat /etc/ppp/resolv.conf
                } > $REALRESOLVCONF.tmp

                # backup the old configuration and install the new one
                cp -dpP $REALRESOLVCONF $REALRESOLVCONF.pppd-backup
                mv $REALRESOLVCONF.tmp $REALRESOLVCONF

                # correct permissions
                chmod 0644 /etc/resolv.conf
                chown root:root /etc/resolv.conf

        fi

fi
---8<---


end part of ip-down:
---8<---
if [ "$USEPEERDNS" ]; then

        # taken from debian's 0000usepeerdns
        # follow any symlink to find the real file
        REALRESOLVCONF=$(readlink --canonicalize /etc/resolv.conf)

        if [ "$REALRESOLVCONF" != "/etc/ppp/resolv.conf" ]; then

                # if an old resolv.conf file exists, restore it
                if [ -e $REALRESOLVCONF.pppd-backup ]; then
                        mv $REALRESOLVCONF.pppd-backup $REALRESOLVCONF
                fi

        fi

fi
---8<---

Ther's more surounding it like resetting default gateway etc.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.