cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1347
Views
0
Helpful
3
Replies

Script to send alerts when the Interface get new IP from DHCP

demichel2
Level 1
Level 1

Hi All,

I have some branch offices with a Cable ISP company with dynamic IP address,   I'm trying to create a script to automatically send an email if the interface get a new IP address if the interface get the previous IP do not send an email .

So far this is the script I created :

event manager applet TEST2
event syslog pattern "Interface FastEthernet1 assigned DHCP"
action 1.1 cli command "enable"
action 1.2 cli command "show int FastEthernet1 | i Internet address"
action 1.51 regexp "Internet address is 9.9.9.9 /30" "$_cli_result"
action 1.6 if $_regexp_result eq "0"
action 2.0 mail from testevent@anydomain.com to emai@anydomain.com subject "NEW IP ADDRESS " server SMTP.ANYDOMAIN.COM body "$_syslog_msg"
action 2.1 end

The only issue with the script is that if the interface get a new IP address I need to update the script in action 1.51 regexp with the new IP address so next time the interface change the IP compare with the variable Internet address is ...

Thank you,

1 Accepted Solution

Accepted Solutions

Joe Clarke
Cisco Employee
Cisco Employee

There are a number of ways to do what you want.  A file on flash is probably the easiest given it will persist across reboots.

event manager applet check-ip

 event syslog pattern "Interface FastEthernet1 assigned DHCP"

 action 1.0 cli command "enable"

 action 2.0 handle-error type ignore

 action 3.0 file open fd flash:myip.txt r

 action 4.0 if $_error ne FH_EOK

 action 4.1  set currip "0.0.0.0"

 action 4.2 else

 action 4.3  file gets fd currip

 action 4.4  file close fd

 action 4.5 end

 action 4.6 handle-error type exit

 action 4.7 cli command "show int Fa1 | inc Internet address"

 action 4.8 regexp "Internet address is ([0-9\.]+)" "$_cli_result" match myip

 action 4.9 if $myip ne $currip

 action 5.0  mail from testevent@anydomain.com to emai@anydomain.com subject "NEW IP ADDRESS " server SMTP.ANYDOMAIN.COM body "$_syslog_msg"

 action 5.1  file open fd flash:myip.txt w

 action 5.2  file puts fd nonewline $myip

 action 5.3  file close fd

 action 5.4 end

View solution in original post

3 Replies 3

Joe Clarke
Cisco Employee
Cisco Employee

There are a number of ways to do what you want.  A file on flash is probably the easiest given it will persist across reboots.

event manager applet check-ip

 event syslog pattern "Interface FastEthernet1 assigned DHCP"

 action 1.0 cli command "enable"

 action 2.0 handle-error type ignore

 action 3.0 file open fd flash:myip.txt r

 action 4.0 if $_error ne FH_EOK

 action 4.1  set currip "0.0.0.0"

 action 4.2 else

 action 4.3  file gets fd currip

 action 4.4  file close fd

 action 4.5 end

 action 4.6 handle-error type exit

 action 4.7 cli command "show int Fa1 | inc Internet address"

 action 4.8 regexp "Internet address is ([0-9\.]+)" "$_cli_result" match myip

 action 4.9 if $myip ne $currip

 action 5.0  mail from testevent@anydomain.com to emai@anydomain.com subject "NEW IP ADDRESS " server SMTP.ANYDOMAIN.COM body "$_syslog_msg"

 action 5.1  file open fd flash:myip.txt w

 action 5.2  file puts fd nonewline $myip

 action 5.3  file close fd

 action 5.4 end

I'm getting  this error *Jul  1 19:31:01.152: %HA_EM-3-FMPD_UNKNOWN_ENV: fh_parse_var: could not find environment variable: myip

Never mine ... I just have the incorrect interface

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: