Please can someone help me customise this powershell script?

Soldato
Joined
8 Jun 2005
Posts
5,275
Hi all,

I found this on another website:

Code :

# Create a web client object with the required credentials to access the router:
# -----------------------------------------------------------------------------
$WebClient = new-object System.Net.WebClient
$WebClient.Credentials = new-object System.Net.NetworkCredential accountname,password
# change "accountname,password" in the above line to that required for your router,
# or uncomment the following if you want to enter the username/password at run time
# instead of hard-coding it into the script:
# $WebClient.Credentials = Get-Credential
# Get the status page from the router and extract the IP address from it:
# ----------------------------------------------------------------------
$StatusPage = $WebClient.DownloadString( "http://192.168.1.1/Status_Router.asp" )
$StatusPage -match 'var wan_ip ".+"' | out-null
$IPAddress = $matches[0].Split('"')[1]
# The last two lines above may need to be customized to the particular layout
# of your router's status page


# Complain if the IP address is different:
# ---------------------------------------
if ( $IPAddress -ne "10.10.10.10" )
{
write-host -foregroundcolor red "`nIP Address has changed - it is now: $IPAddress`n"
exit 1
}
else
{
write-host -foregroundcolor cyan "`nIP Address is still $IPAddress`n"
}

Basically it checks your external IP from the webpage on your router and tells you if it has changed.

I would like to be able to run it as a scheduled task and have it email me if it's changed.

Some details that might be needed to customise it:

My router is a BT HH3 and the status page is:

http://192.168.0.1/html/settings/a_internet.html

The IP text on the status page is written as:

Broadband network IP address: ??.???.??.???

My Email server is 192.168.0.3

Does anyone have any pointes on this one please?

Thanks,

G
 
I have just tried this just to see if the original script works:

# Create a web client object with the required credentials to access the router:
# -----------------------------------------------------------------------------
$WebClient = new-object System.Net.WebClient
$WebClient.Credentials = new-object System.Net.NetworkCredential admin,password
# change "accountname,password" in the above line to that required for your router,
# or uncomment the following if you want to enter the username/password at run time
# instead of hard-coding it into the script:
# $WebClient.Credentials = Get-Credential
# Get the status page from the router and extract the IP address from it:
# ----------------------------------------------------------------------
$StatusPage = $WebClient.DownloadString( "http://192.168.0.1/html/settings/a_internet.html" )
$StatusPage -match 'var Broadband network IP address: ".+"' | out-null
$IPAddress = $matches[0].Split('"')[1]
# The last two lines above may need to be customized to the particular layout
# of your router's status page


# Complain if the IP address is different:
# ---------------------------------------
if ( $IPAddress -ne "10.10.10.10" )
{
write-host -foregroundcolor red "`nIP Address has changed - it is now: $IPAddress`n"
exit 1
}
else
{
write-host -foregroundcolor cyan "`nIP Address is still $IPAddress`n"
}

But it fails with:

Cannot index into a null array.
At C:\ip.ps1:13 char:31
+ $IPAddress = $matches[ <<<< 0].Split('"')[1]
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray


IP Address has changed - it is now:
 
That means that your array is null because it couldn't find the string you matched to.

I put this together quickly, but I'm at work so can't verify if it can find the match. It probably needs tweaked to get it perfect.

Code:
# This script scrapes the External IP address from BT HH3
# and sends out an email if it is changed
# Version 1.0

# Create a web client object with the required credentials to access the router:
# -----------------------------------------------------------------------------
$WebClient = new-object System.Net.WebClient
$WebClient.Credentials = new-object System.Net.NetworkCredential accountname,password
# change "accountname,password" in the above line to that required for your router,
# or uncomment the following if you want to enter the username/password at run time
# instead of hard-coding it into the script:
# $WebClient.Credentials = Get-Credential
# Get the status page from the router and extract the IP address from it:
# ----------------------------------------------------------------------
# The Original string
# $StatusPage = $WebClient.DownloadString( "http://192.168.1.1/Status_Router.asp" )
# $StatusPage -match 'var wan_ip ".+"' | out-null
# $IPAddress = $matches[0].Split('"')[1]
# The BT HH3 String
$StatusPage = $WebClient.DownloadString( "http://192.168.0.1/html/settings/a_internet.html" )
$StatusPage -match 'Broadband network IP address: ".+"' | out-null
$IPAddress = $matches[0].Split('"')[1]
# The last two lines above may need to be customized to the particular layout
# of your router's status page


# Complain if the IP address is different:
# ---------------------------------------
if ( $IPAddress -ne "10.10.10.10" )
{
Send-MailMessage -From "[email protected]" -To "[email protected]" -Subject "IP Address has changed" -Body "It is now: $IPAddress"
}
 
Last edited:
That means that your array is null because it couldn't find the string you matched to.

I put this together quickly, but I'm at work so can't verify if it can find the match. It probably needs tweaked to get it perfect.

Code:
SNIP

Thanks for that :) I added

Code:
$PSEmailServer = "192.168.0.3"

To the file and it sent the email so thanks very much for you help :D

Really don't know how I am going to get the IP off the page though, I'm a little stumped cause the BT HH3 is odd/pooh.
 
Took a bit of a different take on this. whatismyip.com have a page for programmers which just gives you the IP in a string. So here's what I got. Works for me.
Code:
# This script scrapes the External IP address from BT HH3
# and sends out an email if it is changed
# Version 1.0

$staticIP = "10.10.10.10"

# Create a web client object and grab IP Address from whatismyip.com
# -----------------------------------------------------------------------------
$client = new-object System.Net.WebClient
$ip = $client.DownloadString("http://automation.whatismyip.com/n09230945.asp");

# Complain if the IP address is different:
# ---------------------------------------
if ( $ip -ne $staticIP )
{
Send-MailMessage -From "[email protected]" -To "[email protected]" -Subject "IP Address has changed" -Body "It is now: $ip"
}
 
Last edited:
Awesome that works fine with the email side of things :) Thank you!!

Would it be possible to have it take the IP on the first run from whatismyip, write it to a temp file, and compare the latest IP from whatismyip to the one in the temp file so that it actually only emails when it's changed?
 
This is where I've got to now, it does write the IP to the file specified, but it seems to send the email every time the script is run regardsless... have I done something wrong?

Thanks :)

Code:
# This script scrapes the External IP address from WhatIsMyIP
# and sends out an email if it is changed
# Version 1.0

$staticIP = get-content C:\ip.txt
$PSEmailServer = "192.168.0.3"

# Create a web client object and grab IP Address from whatismyip.com
# -----------------------------------------------------------------------------
$client = new-object System.Net.WebClient
$ip = $client.DownloadString("http://automation.whatismyip.com/n09230945.asp");


# Complain if the IP address is different:
# ---------------------------------------
if ( $ip -ne $staticIP )
{
Send-MailMessage -From "monitoring@?.co.uk" -To "GuruHome@?.co.uk" -Subject "IP Address has changed" -Body "It is now: $ip"
Send-MailMessage -From "monitoring@?.co.uk" -To "GuruWork@?.co.uk" -Subject "IP Address has changed" -Body "It is now: $ip"
}



# PowerShell Out-File
$File ="C:\ip.txt"
$ip | Out-File $File

I'm not convinced about this bit:


$staticIP = get-content C:\ip.txt
 
Ok I seem to have got it working now... I've set a scheduled task to run every 15 minutes which opens IPTask.bat:

Code:
powershell.exe -Command set-executionpolicy unrestricted
powershell.exe -Command "c:\Scripts\IPChanged.ps1"

This runs:

Code:
# This script scrapes the External IP address from whatismyip.com
# and sends out an email if it is changed
# Version 1.0

$staticIP = get-content C:\Scripts\ip.txt
$PSEmailServer = "192.168.0.3"
$File ="C:\scripts\ip.txt"

# Create a web client object and grab IP Address from whatismyip.com
# -----------------------------------------------------------------------------
$client = new-object System.Net.WebClient
$ip = $client.DownloadString("http://automation.whatismyip.com/n09230945.asp");


# Complain if the IP address is different:
# ---------------------------------------
if ($ip -eq $staticIP) {$ip | Out-File $File}

else {Send-MailMessage -From "monitoring@?.co.uk" -To "GuruHome@?.co.uk" -cc "GuruWork@?.co.uk" -bcc "administrator@?.co.uk" -Subject "IP Address has changed" -Body "Your public facing IP address has changed 

it has changed from $staticIP and is now: $ip"}

# PowerShell Out-File
$ip | Out-File $File
Set-ExecutionPolicy restricted

I didn't figure out how to do multiple recipients in the -To field, but this does the job :)
 
Last edited:
Back
Top Bottom