2

The title question says it all. I've built a machine running Ubuntu 9.10 Desktop (AMD 64) and need it to email it's IP address to a list of email addresses whenever it starts up.

Michael Prescott
  • 655
  • 2
  • 9
  • 15
  • Can you elaborate on why you need to do this? Have you considered alternatives like dynamic dns? – Zoredache Feb 24 '10 at 19:54
  • Other machines are part of a domain that the ubuntu machine cannot join. I'd assume that setting up a dynamic dns service would require configuring other machines to use the dynamic dns server as one of their DNS's. I can't modify settings on other machines that will be accessing the ubuntu machine, but I can tell people if/when the IP address has changed. I'd prefer to automate that process. – Michael Prescott Feb 24 '10 at 20:18

1 Answers1

4

When a network interface is started scripts in /etc/network/if-up.d are ran. When the scripts are executed lots of environment variables are provided that include the IP address and other network parameters. It should be very easy to write yourself a if-up script that sends out an email with the information you want.

I think that doing a dynamic dns update would probably be more useful then an email, but perhaps you have some reasons you haven't mentioned.

Here is a old script I had that did something close to what you want.

/etc/default/if_notify

# interface definitions
IF_OUT='eth0'
EMAIL_OUT='user@example.org'
MSG_OUT_UP='outside interface is up. Address is:'

/etc/network/if-up.d/if_notify

#!/bin/bash

[ ! -x /etc/default/if_notify ] || exit 0

. /etc/default/if_notify

NAME=`hostname -f`

if [ "$IFACE" = "$IF_OUT" -a "$MODE" = "start" ] ; then
    if [ "$IF_IN" -a "$MSG_OUT_UP" -a "$EMAIL_OUT" ] ; then
        IFACE="outside"
        MSG="$MSG_OUT_UP $IF_ADDRESS"
        ADDR="$EMAIL_OUT"
    fi
fi

if [ "$IFACE" -a "$MSG" -a "$ADDR" ] ; then
    echo -e "From:root@$NAME\nTo:$ADDR\nSubject:[IFNOTIFY] Interface $IFACE up on $NAME\n$MSG" \
            | sendmail -f root "$ADDR"
fi
Zoredache
  • 130,897
  • 41
  • 276
  • 420
  • Thanks for the tip and code! So, I could drop a bash script into that folder and it would run. I seem to recall seeing script naming convention or perhaps a command for registering scripts to control the order in which they run, I'd assume there is a way to make my script run after all of the other environment variables are created. – Michael Prescott Feb 24 '10 at 20:22
  • 1
    The see `man run-parts` to get a handle on the naming convention and order scripts are ran. See `man interfaces` for details about all the ways you can run things during the network configuration. In the script above I assume the system has a working locally installed mail server. It may be configured to use some other as a smart host, `apt-get install exim4 exim4-daemon-light`. It should be possible to use an external mail server with some other command line tool, but I'll leave that up to you to find, or ask about in a separate question. – Zoredache Feb 24 '10 at 20:37
  • extra points for recommending exim as the MTA :) – BuildTheRobots Feb 25 '10 at 01:20
  • @Michael: The registration command you might be looking for is called `update-rc.d`. – Dennis Williamson Feb 25 '10 at 02:02