email with new ipadress when pppoe ip changes. CentOS 5.1

shaunwingin
shaunwingin used Ask the Experts™
on
Say, I'm using rppppoe on CentOS machine and need the current email sent to me. How can this be done? I don't want to use Dyndns but rather receive ip as an email.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
Currently there is not any home made linux product to do this

you will have to create your own script , and have to run that script example every 1 hour, as soon as script will see there is an ip changes it will sent you an email

you can create a new question in sh, bash section on this


Julian ParkerSenior Systems Administrator

Commented:
> you can create a new question in sh, bash section on this

or just get the moderators to modify the zones so there isnt a duplicate Q :-)

you could probably just modify /etc/sysconfig/network-scripts/ifup to add an email bit;
   ifconfig -a | mail -s "NEW IP" me@home.com

Author

Commented:
Tx. This is  a cat of ifup. Where do I add the line you suggest?
[root@localhost network-scripts]# cat ifup
#!/bin/bash
# Network Interface Configuration System
# Copyright (c) 1996-2001 Red Hat, Inc. all rights reserved.
#
# This software may be freely redistributed under the terms of the GNU
# public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

unset WINDOW # defined by screen, conflicts with our usage

. /etc/init.d/functions

cd /etc/sysconfig/network-scripts
. ./network-functions

[ -f ../network ] && . ../network

CONFIG=${1}

[ -z "${CONFIG}" ] && {
    echo $"Usage: ifup <device name>" >&2
    exit 1
}

need_config ${CONFIG}

[ -f "${CONFIG}" ] || {
    echo $"$0: configuration for ${1} not found." >&2
    echo $"Usage: ifup <device name>" >&2
    exit 1
}

if [ ${UID} != 0 ]; then
    if [ -x /usr/sbin/usernetctl ]; then
        source_config
        if /usr/sbin/usernetctl ${CONFIG} report ; then
            exec /usr/sbin/usernetctl ${CONFIG} up
        fi
    fi
    echo $"Users cannot control this device." >&2
    exit 1
fi

source_config

if [ "foo$2" = "fooboot" ] && [ "${ONBOOT}" = "no" -o "${ONBOOT}" = "NO" ]
then
    exit 0
fi
if [ -n "$IN_HOTPLUG" ] && [ "${HOTPLUG}" = "no" -o "${HOTPLUG}" = "NO" ]
then
    exit 0
fi
if [ -n "$IN_HOTPLUG" -a "${TYPE}" = "Bridge" ];
then
    exit 0
fi

# Ethernet 802.1Q VLAN support
if [ -x /sbin/vconfig -a "${VLAN}" = "yes" -a "$ISALIAS" = "no" ]; then
    VID=""
    MATCH='^(eth|hsi|bond)[0-9]+\.[0-9]{1,4}$'
    if [[ "${DEVICE}" =~ $MATCH ]]; then
        VID=$(echo "${DEVICE}" | LC_ALL=C sed 's/^[a-z0-9]*\.0*//')
        PHYSDEV=${DEVICE%.*}
    fi
    if [[ "${DEVICE}" =~ '^vlan[0-9]{1,4}?' ]]; then
        VID=$(echo "${DEVICE}" | LC_ALL=C sed 's/^vlan0*//')
        # PHYSDEV should be set in ifcfg-vlan* file
        if test -z "$PHYSDEV"; then
                echo $"PHYSDEV should be set for device ${DEVICE}"
                exit 1
        fi
    fi
    if [ -n "$VID" ]; then
        if [ ! -d /proc/net/vlan ]; then
            if ! modprobe 8021q >/dev/null 2>&1 ; then
                echo $"No 802.1Q VLAN support available in kernel for device ${DEVICE}"
                exit 1
            fi
        fi

        test -z "$VLAN_NAME_TYPE" && VLAN_NAME_TYPE=DEV_PLUS_VID_NO_PAD
        /sbin/vconfig set_name_type "$VLAN_NAME_TYPE" >/dev/null 2>&1 || {
                echo $"Could not set 802.1Q VLAN parameters."
        }

        is_available ${PHYSDEV} || {
            if [ "$?" = "1" ] ; then
                echo $"$alias device ${DEVICE} does not seem to be present, delaying initialization."
                exit 1
            else
                exit 0
            fi
        }

        # Link on Physical device needs to be up but no ip required
        check_device_down ${PHYSDEV} && {
            ip -o link set dev ${PHYSDEV} up
        }

        if [ ! -f /proc/net/vlan/${DEVICE} ]; then
            /sbin/vconfig add ${PHYSDEV} ${VID} || {
                (/usr/bin/logger -p daemon.info -t ifup \
                    $"ERROR: could not add vlan ${VID} as ${DEVICE} on dev ${PHYSDEV}" &)&
                echo $"ERROR: could not add vlan ${VID} as ${DEVICE} on dev ${PHYSDEV}"
                exit 1
            }
        fi

        if [ -f /proc/net/vlan/${DEVICE} ]; then
            case "$REORDER_HDR" in
                yes|1)
                    /sbin/vconfig set_flag ${DEVICE} 1 1 || {
                        (/usr/bin/logger -p daemon.info -t ifup \
                            "WARNING: vconfig not able to enable REORDER_HDR on ${DEVICE}" &)&
                    }
                    ;;
                no|0)
                    /sbin/vconfig set_flag ${DEVICE} 1 0 || {
                        (/usr/bin/logger -p daemon.info -t ifup \
                            $"WARNING: vconfig not able to disable REORDER_HDR on ${DEVICE}" &)&
                    }
                    ;;
            esac
        fi
    fi
fi

# Old BOOTP variable
if [ "${BOOTP}" = "yes" ]; then
    BOOTPROTO=bootp
fi

if [ "${BOOTPROTO}" = "bootp" -o "${BOOTPROTO}" = "dhcp" ]; then
    DYNCONFIG=true
fi

if [ -x /sbin/ifup-pre-local ]; then
    /sbin/ifup-pre-local ${CONFIG} $2
fi

OTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-${DEVICETYPE}"

if [ ! -x ${OTHERSCRIPT} ]; then
    OTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-eth"
fi

exec ${OTHERSCRIPT} ${CONFIG} $2

Open in new window

Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Julian ParkerSenior Systems Administrator
Commented:
Although I'm not famialiar with rpppoe I'm guessing it still uses the same startup scripts (perhaps you could supply more information).

Just add the following line in /etc/sysconfig/network-scripts/ifup-post BEFORE the `exit 0`

   /sbin/ifconfig -a | mail -s "NETWORK" me@mydomain.com

Note that you do get a mail error because the interface is not available but it does work.

Author

Commented:
Say, how can I check if the mail is going out - as don't receive any mail?
Julian ParkerSenior Systems Administrator

Commented:
mailq is a good start.

Can you email from the server normally?? I was kinda hoping the email was already working...

Author

Commented:
I don't believe I can mail normally.... but hope to play around to sort out.

Author

Commented:
This is ifup-ppp. Where do I add the script. There is no exit 0

#! /bin/bash

. /etc/init.d/functions

cd /etc/sysconfig/network-scripts
. ./network-functions

# ifup-post for PPP is handled through /etc/ppp/ip-up
if [ "${1}" = daemon ] ; then
  # we've been called from ppp-watch, so don't invoke it for persistence
  shift
else
  # just in case a full path to the configuration file is passed in
  CONFIG=${1##*/} # CONFIG=$(basename $1)
  [ -f "${CONFIG}" ] || CONFIG=ifcfg-${1}
  source_config
  # don't start ppp-watch by xDSL
  if [ "${DEMAND}" != yes -a "$TYPE" != "xDSL" ] ; then
    # let ppp-watch do the right thing
    exec /sbin/ppp-watch "${CONFIG##ifcfg-}" "$2"
  fi
fi

CONFIG=$1
[ -f "${CONFIG}" ] || CONFIG=ifcfg-${1}
source_config

if [ -z "${DISCONNECTTIMEOUT}" ]; then
  DISCONNECTTIMEOUT=2
fi

if [ -z "${RETRYTIMEOUT}" ]; then
  RETRYTIMEOUT=30
fi

if [ -z "${IDLETIMEOUT}" ]; then
  IDLETIMEOUT=600
fi

if [ "${2}" = "boot" -a "${ONBOOT}" = "no" ]; then
  exit
fi

[ -x /sbin/pppd -o -x /usr/sbin/pppd ] || {
  echo $"pppd does not exist or is not executable"
  echo $"ifup-ppp for ${DEVICE} exiting"
  /usr/bin/logger -p daemon.info -t ifup-ppp \
    $"pppd does not exist or is not executable for ${DEVICE}"
  exit 1
}

# check that xDSL connection
if [ "$TYPE" = "xDSL" ] ; then
    if [ -x /sbin/adsl-start -o -x /usr/sbin/adsl-start ] ; then
        adsl-start /etc/sysconfig/network-scripts/$CONFIG
        exit $?

 else
        /usr/bin/logger -p daemon.info -t ifup-ppp \
            $"adsl-start does not exist or is not executable for ${DEVICE}"
        exit 1
    fi
fi

PEERCONF=/etc/ppp/peers/${DEVNAME}

if [ "${DEBUG}" = "yes" ]; then
  CHATDBG="-v"
fi

if [ ! -f ${PEERCONF} ]; then
  if [ -z "${WVDIALSECT}" ] ; then
    CHATSCRIPT=/etc/sysconfig/network-scripts/chat-${DEVNAME}
    [ -f ${CHATSCRIPT} ] || {
      CHATSCRIPT=/etc/sysconfig/network-scripts/chat-${PARENTDEVNAME}
    }
    [ -f ${CHATSCRIPT} ] || {
     echo $"/etc/sysconfig/network-scripts/chat-${DEVNAME} does not exist"
     echo $"ifup-ppp for ${DEVNAME} exiting"
     /usr/bin/logger -p daemon.info -t ifup-ppp \
       $"/etc/sysconfig/network-scripts/chat-${DEVNAME} does not exist for ${DEVICE}"
     exit 1
    }
  fi
  /usr/bin/logger -s -p daemon.notice -t ifup-ppp \
    $"Setting up a new ${PEERCONF} config file"
  if [ -f /etc/ppp/peers/${DEVICE} ]; then
    cp -f /etc/ppp/peers/${DEVICE} ${PEERCONF}
  else
    touch ${PEERCONF}
  fi
  if [ "${WVDIALSECT}" ]; then
    echo "connect \"/usr/bin/wvdial --remotename ${DEVNAME} --chat '${WVDIALSECT}'\"" >> ${PEERCONF}
  else
    echo "connect \"/usr/sbin/chat ${CHATDBG} -f ${CHATSCRIPT}\"" >> ${PEERCONF}
  fi
fi

opts="lock"
if [ "${HARDFLOWCTL}" != no ] ; then
  opts="$opts modem crtscts"
fi
if [ "${ESCAPECHARS}" != yes ] ; then
  opts="$opts asyncmap 00000000"
fi
if [ "${DEFROUTE}" != no ] ; then
  # pppd will no longer delete an existing default route
  # so we have to help it out a little here.
  DEFRT=$(ip route list match 0/0)
  [ -n "${DEFRT}" ] && echo "$DEFRT" > /etc/default-routes
  echo "$DEFRT" | while read spec; do

  [ -n "${DEFRT}" ] && echo "$DEFRT" > /etc/default-routes
  echo "$DEFRT" | while read spec; do
      ip route del $spec;
  done
  opts="$opts defaultroute"
fi
if [ "${PEERDNS}" != no ] ; then
  cp -f /etc/resolv.conf /etc/resolv.conf.save
  opts="$opts usepeerdns"
fi
if [ -n "${MRU}" ] ; then
  opts="$opts mru ${MRU}"
fi
if [ -n "${MTU}" ] ; then
  opts="$opts mtu ${MTU}"
fi
if [ -n "${IPADDR}${REMIP}" ] ; then
  # if either IP address is set, the following will work.
  opts="$opts ${IPADDR}:${REMIP}"
fi
if [ -n "${PAPNAME}" ] ; then
  opts="$opts user ${PAPNAME} remotename ${DEVNAME}"
fi
if [ "${DEBUG}" = yes ] ; then
  opts="$opts debug"
fi

if [ ${DEMAND} = yes ] ; then
  opts="$opts demand ktune idle ${IDLETIMEOUT} holdoff ${RETRYTIMEOUT}"
  exec=
else
  opts="$opts nodetach"
  exec=exec
fi

(/usr/bin/logger -p daemon.info -t ifup-ppp \
  $"pppd started for ${DEVNAME} on ${MODEMPORT} at ${LINESPEED}" &)&

$exec pppd $opts ${MODEMPORT} ${LINESPEED} \
    ipparam ${DEVNAME} linkname ${DEVNAME} call ${DEVNAME}\
    noauth \
    ${PPPOPTIONS} || exit

if [ "${DEMAND}" = "yes" ] ; then
  # pppd is a tad slow to write the pid-file.
  sleep 2
  if [ -f /var/run/ppp-${DEVNAME}.pid ] ; then
    REALDEVICE=`tail -1 /var/run/ppp-${DEVNAME}.pid`
    /etc/sysconfig/network-scripts/ifup-routes ${REALDEVICE} ${DEVNAME}
  fi
fi

Author

Commented:
Hi Jools, It works thank you.
Is it possible to only the following line as the rest is not relevant: "inet addr:xxx.xxx.xxx.xxx"
The entire output looks like this:
.....
RX bytes:2858767351 (2.6 GiB) TX bytes:2858767351 (2.6 GiB)

ppp0 Link encap:Point-to-Point Protocol
inet addr:99.185.166.69 P-t-P:99.185.75.1 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1492 Metric:1
RX packets:3 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:54 (54.0 b) TX bytes:54 (54.0 b)

sit0 Link encap:IPv6-in-IPv4
.....

Author

Commented:
What file must I change for this server as ifup-post not active here...

-bash-3.2# tail -fn300 /var/log/messages
Aug 22 04:02:04 elastix syslogd 1.4.1: restart.
Aug 22 21:20:03 elastix pppd[7267]: LCP terminated by peer
Aug 22 21:20:03 elastix pppd[7267]: Connect time 17600.2 minutes.
Aug 22 21:20:03 elastix pppd[7267]: Sent 16211532 bytes, received 1888436 bytes.
Aug 22 21:20:03 elastix pppoe[7268]: Session 13235 terminated -- received PADT from peer
Aug 22 21:20:03 elastix pppoe[7268]: Sent PADT
Aug 22 21:20:03 elastix pppd[7267]: Modem hangup
Aug 22 21:20:03 elastix pppd[7267]: Connection terminated.
Aug 22 21:20:03 elastix pppd[7267]: Exit.
Aug 22 21:20:03 elastix pppoe-connect: PPPoE connection lost; attempting re-connection.
Aug 22 21:20:08 elastix pppd[24333]: pppd 2.4.4 started by root, uid 0
Aug 22 21:20:08 elastix pppd[24333]: Using interface ppp0
Aug 22 21:20:08 elastix pppd[24333]: Connect: ppp0 <--> /dev/pts/2
Aug 22 21:20:23 elastix pppoe[24334]: PPP session is 12319 (0x301f)
Aug 22 21:20:23 elastix pppd[24333]: PAP authentication succeeded
Aug 22 21:20:23 elastix pppd[24333]: local  IP address 41.185.1.1
Aug 22 21:20:23 elastix pppd[24333]: remote IP address 41.185.7.1
Aug 22 21:25:19 elastix pppd[24333]: LCP terminated by peer
Aug 22 21:25:19 elastix pppd[24333]: Connect time 5.0 minutes.
Aug 22 21:25:19 elastix pppd[24333]: Sent 134013 bytes, received 44899 bytes.
Aug 22 21:25:19 elastix pppoe[24334]: Session 12319 terminated -- received PADT from peer
Aug 22 21:25:19 elastix pppoe[24334]: Sent PADT
Aug 22 21:25:19 elastix pppd[24333]: Modem hangup
Aug 22 21:25:19 elastix pppd[24333]: Connection terminated.
Aug 22 21:25:19 elastix pppd[24333]: Exit.
Aug 22 21:25:19 elastix pppoe-connect: PPPoE connection lost; attempting re-connection.
Aug 22 21:25:24 elastix pppd[24421]: pppd 2.4.4 started by root, uid 0
Aug 22 21:25:24 elastix pppd[24421]: Using interface ppp0
Aug 22 21:25:24 elastix pppd[24421]: Connect: ppp0 <--> /dev/pts/2
Aug 22 21:25:24 elastix pppoe[24422]: PPP session is 14315 (0x37eb)
Aug 22 21:25:24 elastix pppd[24421]: PAP authentication succeeded
Aug 22 21:25:24 elastix pppd[24421]: local  IP address 41.185.1.1
Aug 22 21:25:24 elastix pppd[24421]: remote IP address 41.185.2.1
Julian ParkerSenior Systems Administrator

Commented:
Unfortunately my server is down at present.... I'll respond tomorrow if thats ok.

Author

Commented:
G8 Thank you - when ever you can.

Author

Commented:
can you please have a look at my request.
Assuming that Redhat is still following Fedora rather closely see if you have a filename /etc/ppp/ip-up.local and if you do add the mail command to this file.

chmod 700 /etc/ppp/ip-up.local

When the interface comes up this should send the email as expected.
Top Expert 2014

Commented:
I'm not sure what you are asking.

I think you want somebody to be able to send you e-mail using "youraddress@1.1.1.1"  where 1.1.1.1 is your IP address.  This can't be done, email addresses need to end in @hostname.tdl.
Hi!

Use this command to send your ppp0 IP as a mail!

Regards, Tobias
/sbin/ifconfig ppp0|awk '{if(NR==2)printf$2"\n"}'|mail -s ip yourmail@yourdomain.com

Open in new window

Sjef BosmanGroupware Consultant

Commented:
And what about fetchmail?
To automatically check if the IP's changed you can add a check script and run it with cron.

To add an entry to cron, use (run it as root):

# crontab -e

To check the IP every hour, you can use (first press "i" to enter INSERT mode in vi):
0 * * * * /root/chkip.sh

Then save the file (ESC and the keys ":wq" + ENTER).

Create the script with

# vi /root/chkip.sh

and paste in the attached code.

After this you'd have to set the X-bit on the script to be able to run it:

# chmod u+x /root/chkip.sh

Regards, Tobias
#!/bin/sh

fileip=/tmp/oldip.txt

touch $fileip
oldip=`awk ‘{printf$1}’ $fileip`
newip=`/sbin/ifconfig ppp0|awk ‘{if(NR==2)printf$2¿\n”}’`
if [ "$newip" != "$oldip" ]; then
 echo $newip|mail -s ip yourname@yourdomain.com
 echo $newip > $fileip
fi

Open in new window

Author

Commented:
Tobias,

Thank you for the detailed script. If I run it every minute do you expect it to put a heavy load on the processor. I need ip updates soon after they happen...
No, I don't think the script will take much cpu if you run it every minute. Just change the crontab to:

* * * * * /root/chkip.sh
hmm... I now see that EE editor seems to have changed some chars in the script!

I'll paste in in again:

#!/bin/sh

fileip=/tmp/oldip.txt

touch $fileip
oldip=`awk '{printf$1}' $fileip`
newip=`/sbin/ifconfig ppp0|awk '{if(NR==2)printf$2"\n"}'`
if [ "$newip" != "$oldip" ]; then
 echo $newip|mail -s ip yourname@yourdomain.com
 echo $newip > $fileip
fi
#!/bin/sh

fileip=/tmp/oldip.txt

touch $fileip
oldip=`awk '{printf$1}' $fileip`
newip=`/sbin/ifconfig ppp0|awk '{if(NR==2)printf$2"\n"}'`
if [ "$newip" != "$oldip" ]; then
 echo $newip|mail -s ip yourname@yourdomain.com
 echo $newip > $fileip
fi

Open in new window

Also, first test so that your system can mail. Use the following command:
echo test|mail -s ip yourname@yourdomain.com

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial