Page 1 of 1

Get notified when public facing IP changes

Posted: 14 Feb 2019 13:12
by ibraq07
Good afternoon all,

I am stuck on this and need help! Please keep in mind that I am tech savy, but I am new to the scripting world so please have some patience with me. I will try to explain exactly what I am trying to do to the best of my ability.

I currently have a network that has my primary network and my secondary network in case my primary goes down, it will switch to my secondary. I would like to know if someone can help me set up a script to do nothing when the primary public ip is captured but to send me a email to my gmail account if it catches the secondary network.

I was doing some research and found the following:

If I open up CMD and type the following, it will show me what the public facing IP is on the command prompt. The command is this:

nslookup -debug myip.opendns.com. resolver1.opendns.com 2>NUL |findstr /C:"internet address"

If I go to the web and type the following website, it gives me the current public facing IP:

http://myip.dnsomatic.com/

So here is the scenario:

Public Facing IP 1: 111.111.111.111
Public Facing IP 2: 222.222.222.222

Here is what i need the script to do:

If 111.111.111.111 = 111.111.111.111
Then do nothing
Else send email to email@gmail.com saying your IP has been changed and is currently 222.222.222.222

I also do not have a exchange server. Please let me know if this is possible and can be done. I appreciate any help anyone has to offer and if there is more information or if I was not clear about something, please let me know and I will try my best to answer it correctly.

Thank you!

Re: Get notified when public facing IP changes

Posted: 14 Feb 2019 22:18
by Squashman
Why do you think you need an exchange server to do this?

You essentially have two options for the email. Use a third party command line client or use vbscript. That seems all you have left to do.

Re: Get notified when public facing IP changes

Posted: 15 Feb 2019 14:16
by ibraq07
Squashman wrote:
14 Feb 2019 22:18
Why do you think you need an exchange server to do this?

You essentially have two options for the email. Use a third party command line client or use vbscript. That seems all you have left to do.

I have no idea why I said exchange server, but I figured it out. I did it through Windows Powershell and Command Prompt

Here is what worked for me:

Open in Notepad and save as filename.ps1

Code: Select all

$ipinfo = Invoke-RestMethod http://ipinfo.io/json
$x=$ipinfo.ip
$recipients= "Name1 <email1@domain.com>, Name2 <email2@domain.com>"
if ($x -ne "111.111.111.111") { $EmailFrom = “email1@domain.com”
$EmailTo = “$recipients"
$Subject = “Add Subject Line”
$Body = “Add body line.

Current Public Facing IP: $x”
$SMTPServer = “smtp.domain.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“email1@domain.com”, “Password”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body) }
Open another Notepad with the following and save as filename.bat:

Code: Select all

powershell.exe -file C:\filename.ps1 "exit"
Save both files directly on the C drive.

Next I will use Windows task scheduler to run the batch about every minute and when it detects that I am not on my primary network, it will automatically send an email out to both myself and someone else I have set it to. For my first time doing this, I think I did pretty well. I am sure there are better ways but like I said I am new to this.

Re: Get notified when public facing IP changes

Posted: 15 Feb 2019 14:42
by Squashman
I wouldn't be hammering someone's website every minute. They will see that as abuse and try to block you.

Gonna assume you need a newer version of Powershell to run that cmdlet. Doesn't work on Powershell Version 2.

Re: Get notified when public facing IP changes

Posted: 18 Feb 2019 03:24
by penpen
ibraq07 wrote:
14 Feb 2019 13:12
I currently have a network that has my primary network and my secondary network in case my primary goes down, it will switch to my secondary.
How exactly do your network switches from primary to secondary (and vice versa)?
Could it be that is done by a script in which case you could add a line to that script to simply send a mai?


penpen

Re: Get notified when public facing IP changes

Posted: 19 Feb 2019 09:21
by ibraq07
I have a layer 2 switch that will detect when the primary is down and swap over to the secondary. Everything I posted works with the default programs from windows 10. I used Windows PowerShell and the Command Prompt. I used windows task scheduler with highest admin privileges to run this script every 5 minutes. So far everything is working flawlessly.

Re: Get notified when public facing IP changes

Posted: 19 Feb 2019 10:07
by ibraq07
Replace the first two lines of the powershell script with the following:

$ipPub = (curl -uri 'https://domains.google.com/checkip').Content
$x=$ipPub

It is from google and it will work much better.

Re: Get notified when public facing IP changes

Posted: 24 Feb 2019 05:08
by penpen
Sorry, currently i have 'few time' so my answers might take a while.
ibraq07 wrote:
19 Feb 2019 09:21
I have a layer 2 switch that will detect when the primary is down and swap over to the secondary.
Just because i'm curious, and i am probably missing a scenario on how to build such a network, or i am missing a possible answer to the question "How does your switch detect if the primary is down?".

Ad hoc i only see two (probably unlikely) possible scenarios (misusing code blocks; copy paste these to notepad to see a more accurate view):
1)

Code: Select all

                            (192.168.000.001) Router 1 (111.111.111.111)
                         /                                              \
Your PC (192.168.002.001)                                                Switch --- Internet
                         \                                              /
                           (192.168.000.002) Router 2 (222.222.222.222) 
Hopefully you haven't placed a layer 2 switch into the internet structure, else that might be reason why your primary is down after a while (after the buffer for mac addresses is full, switches tend to broadcast to all ports, including the source port - which is send back to the switch...) - in that case sooner or later you also will get a letter from your ISP (=internet service provider; because sooner or later that will also happen with parts of his internet structure and he sure will not be happy of losing connection with parts of his infrastructure from time to time).

2)

Code: Select all

erver  (192.168.001.001)          (192.168.000.001) Router 1 (111.111.111.111)
                          \      /                                              \
                           Switch                                                Internet
                          /      \                                              /
Your PC (192.168.002.001)          (192.168.000.002) Router 2 (222.222.222.222) 
In case your switch is placed not in the internet, you simply could ping the internal address of your networks (like your swicth in such a case does).


penpen

Re: Get notified when public facing IP changes

Posted: 26 Feb 2019 10:41
by ibraq07
penpen wrote:
24 Feb 2019 05:08
Sorry, currently i have 'few time' so my answers might take a while.
ibraq07 wrote:
19 Feb 2019 09:21
I have a layer 2 switch that will detect when the primary is down and swap over to the secondary.
Just because i'm curious, and i am probably missing a scenario on how to build such a network, or i am missing a possible answer to the question "How does your switch detect if the primary is down?".

Ad hoc i only see two (probably unlikely) possible scenarios (misusing code blocks; copy paste these to notepad to see a more accurate view):
1)

Code: Select all

                            (192.168.000.001) Router 1 (111.111.111.111)
                         /                                              \
Your PC (192.168.002.001)                                                Switch --- Internet
                         \                                              /
                           (192.168.000.002) Router 2 (222.222.222.222) 
Hopefully you haven't placed a layer 2 switch into the internet structure, else that might be reason why your primary is down after a while (after the buffer for mac addresses is full, switches tend to broadcast to all ports, including the source port - which is send back to the switch...) - in that case sooner or later you also will get a letter from your ISP (=internet service provider; because sooner or later that will also happen with parts of his internet structure and he sure will not be happy of losing connection with parts of his infrastructure from time to time).

2)

Code: Select all

erver  (192.168.001.001)          (192.168.000.001) Router 1 (111.111.111.111)
                          \      /                                              \
                           Switch                                                Internet
                          /      \                                              /
Your PC (192.168.002.001)          (192.168.000.002) Router 2 (222.222.222.222) 
In case your switch is placed not in the internet, you simply could ping the internal address of your networks (like your swicth in such a case does).


penpen
There are 2 ISP's that connect to a dumb layer 2 switch. The primary is set to be defaulted, when it realized there are no packets being sent or received, it automatically switches to the secondary ISP. Those are the inlets to the layer 2 switch, this then hits the firewall which then hits the switches and out to the workstations. I hope this answers your question. This was a little project I was working on to find out exactly when the primary goes down and we are on our secondary in a automated way, which seems to be working from the example script I provided above. When the ISP changes, so does the external public facing IP address. That is what I had to ping but I didn't find a way to ping external IP's so I used googles website which will always show the current public facing IP.

I like your graph, it was neat.

Re: Get notified when public facing IP changes

Posted: 02 Mar 2019 19:03
by penpen
ibraq07 wrote:
26 Feb 2019 10:41
There are 2 ISP's that connect to a dumb layer 2 switch. The primary is set to be defaulted, when it realized there are no packets being sent or received, it automatically switches to the secondary ISP. Those are the inlets to the layer 2 switch, this then hits the firewall which then hits the switches and out to the workstations. I hope this answers your question.
I fear it doesn't answer my question, because both scenarios i gave (and some more) seem to be still possible.

What do you mean exactly, when saying "There are 2 ISP's that connect to a dumb layer 2 switch."?
Are you connected to each of your ISP's directly via a network cable, that you pluged into your switch (as shown in scenario 1)?
Or are you connected to each of your ISP's directly via a network cable; one from a network element with adress 111.111.111.111 the other with 222.222.222.222 (not listed scenario).
Or do you have two phone lines connected each to a DSL-Router and then these modems are connected to the switch (as shown in scenario 2)?
Or something different?

And what do you mean with "it automatically switches to the secondary ISP"?
Depending on where the switch is placed that could nearly mean anything (from "never send packages to a specific internal/external address" to "making the only DSL-modem in your network changing the ISP by dialing that up").

The main reason i asked for details is, that there are (simple) solutions to various of such scenarios (but not to all), for example:
In case you have implemented scenario 2, and
in case ""it automatically switches to the secondary ISP" means "never send packages to and from"192.168.000.001" again (until reset),
then you could simply ping 192.168.000.001 instead of your external primary IP.


penpen