Publish Your Home Internet Connection IP Address Using Azure Blobs- Part 2

This is part 2 of a hack to publish your Home Internet Connection IP Address to Azure. Please check part 1 of the article here
https://blogs.technet.microsoft.com/meamcs/2018/12/23/publish-your-home-internet-connection-ip-address-using-azure-blobs-part-1/

Scenario

Ever wanted to be able to access your home server while your on the go, but you don’t know its IP Address ? Or maybe it changed ?

Background

I have a server at home which I use to host number of virtual machines that I use for performing some labs or tests. I have a VM on that server that is published on the internet through my home internet connection. Since I travel a lot, I find it sometimes challenging to connect to this VM and hence to my home server to work remotely.

On any usual day, I usually open Bing and just type What is my IP and Bing just write it back to me or gives me number of websites that can tell what is my public IP address, I record that IP somewhere and connect to it whenever I want to play with my home server. But from time to time, the IP address changes for various reasons, and I need to check again what is the new IP address or ask somebody at home to try to figure it out (and I spend considerable amount of time attempting to explain how to do that, and that is IP and what are those numbers 😃), so for the soul purpose of saving myself all this hassle, I went looking for any free solution that can help me figure out my home connection IP address while I’m not connected to my home network.

In my quest, I managed to come with an idea that is not so bad which allows me to easily find my home IP address, the only requirement for this solution is having an Azure Subscription, and the Azure Powershell module

In this 2 parts blog, I will be discussing the details of this solution

The Solution

The basic idea of the solution is to utilize the Azure Blob Storage to write a file that contains my home connection IP address from a VM or a PC running at home, and since the file is writing to a Blob in my Azure subscription, I can access it from anywhere on the planet

There are mainly to parts to this solution:

  1. Finding my home connection IP address
  2. Writing the IP address to a file and uploading it to an Azure Blob

In this blog post, I will be discussing the second part.

Part 2 – Writing the IP address to a file and uploading it to an Azure Blob

So now that I have the IP Address of my home internet connection, it’s time to save it to a file. This is extremely easier than you can imagine. Remember from the first part of this blog post, we created a function that retrieves the IP address that looks like the following

function GetMyIP(){
   some code…
}

Now it’s time to utilize it as follow

$MyIP = GetMyIP

Then write the IP Address to a file

$MyIP | Out-File C:TempMyIP.txt -Force   # Use the -Force switch to over write any existing file if necessary

So now that we have the file with the IP address, it’s time to upload it to Azure. To achieve that, we need to perform number of steps in preparation for uploading the file

  1. Create a Blob container in a Storage Account
  2. Obtain container Shared Access Key
  3. Install the Azure Powershell module
  4. Write Blob into container

So if you already have an existing Storage Account (and in turn a Resource Group) ready for this, you can utilize it, otherwise, you’ll need to create a new one. A quick guide on creating a storage account can be read here (https://docs.microsoft.com/en-us/azure/storage/common/storage-quickstart-create-account?tabs=portal)

Once the storage account is created, you’ll need to create a Blob container to host the uploaded file. Simply log on to the Azure Portal, browse to the storage account that you created, then click on Blob in the overview page

Then click on +Container

Give it a name and make sure you choose Private under the Public access level (you don’t want the public to know your IP address)

Now you have the container created successfully

Now back to the myipsa storage account overview page, click on Access Keys

Once in the Access Keys page, copy the Key under Key1 and paste it somewhere (Notepad will be a good choice)

The third step is to install the Azure Powershell module on them VM or PC that will be scheduled to run this script
The simplest way to achieve this is by executing the following from an elevated Powershell window

Install-Module Azure

You might get a prompt asking for your permission to install the NuGet provider as follow

If you are okay (which you need to be) , type Y for Yes and press Enter (Or Just press Enter without typing anything) which will give you another prompt asking for your confirmation to install the Azure Powershell module from a public repository, then the installation will start

Now back to our script from part 1, add the following lines to the previous script

$SAAccessKey = "<access key1>" #Paste key1 here
$SAName = "myipsa"
$ContainerName = "myip"
$LocalFile = "C:TempMyIP.txt"

In the above lines, we are just preparing things, essentially saving the Access Key to a variable along with the storage account name and the container name, and the path to a file on your VM or PC that we will upload to the Blob container.

Now it’s time to utilize the function we created in part1, use it to obtain the IP address into a variable

$MyIPAddr = GetMyIP
$MyIPAddr | Out-File $LocalFile -Force

An better one-liner approach is as follow

GetMyIP | Out-File $LocalFile -Force

Note that I’m using the -Force option as I don’t want the script to ask me if I need to over right an existing file or not, just go ahead and overwrite it

Now we create a storage context defining the path in the storage container and how to access it, this is also a one-liner

$saContext = New-AzureStorageContext -StorageAccountName $SAName -StorageAccountKey $SAAccessKey

And finally, to upload the created file which contains the IP address

Set-AzureStorageBlobContent -File $LocalFile -Container $ContainerName -Blob "MyIP.txt" -Context $saContext -Force

So now the whole script (in a simple form)

<# 
 .SYNOPSIS 
    Captures internet connection public IP address and write it to a file on Azure Blob. 
 .DESCRIPTION 
    The purpose of this script is to help anybody with detecting the public IP address of an internet connection and 
    write it to a local file, then upload that file into an Azure Blob storage account
    Based on script by Aman Dhally (http://www.amandhally.net/2012/10/04/powershell-script-to-get-static-ip-address-or-public-ip-address-of-internet-connection/)
 .NOTES 
    Author  : Muhammad Raslan 
    Requires: Azure PowerShell Module
 .LINK 
    WebClient Class
        https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient?view=netframework-4.7.2
    Regular Expression Language
        https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
    New-AzureStorageContext
        https://docs.microsoft.com/en-us/powershell/module/azure.storage/new-azurestoragecontext?view=azurermps-6.13.0
    Set-AzureStorageBlobContent
        https://docs.microsoft.com/en-us/powershell/module/azure.storage/set-azurestorageblobcontent?view=azurermps-6.13.0
#> 
 

function GetMyIP(){
    $DDNSIPCheckerUrl = "http://checkip.dyndns.com"
    $WebClient = New-Object System.Net.Webclient
    $DDNSResponse = $WebClient.DownloadString($DDNSIPCheckerUrl)
    
    $Regex = [regex] "d{1,3}.d{1,3}.d{1,3}.d{1,3}"
    $RegExResult = $Regex.Match($DDNSResponse)

    If($RegExResult.Success){
        $IPAddress = $DDNSResponse.Substring($RegExResult.Index,$RegExResult.Length)
        return $IPAddress     
    }
    else{
        return 0
    }
}

$SAAccessKey = "<replace with your access key>" #Write your Access Key here
$SAName = "myipsa" #Write your Storage Account name here
$ContainerName = "myip" #Write your container name here
$LocalFile = "C:TempMyIP.txt" #Write the full path to the temporary text file here

GetMyIP | Out-File $LocalFile -Force

$saContext = New-AzureStorageContext -StorageAccountName $SAName -StorageAccountKey $SAAccessKey
Set-AzureStorageBlobContent -File $LocalFile -Container $ContainerName -Blob "MyIP.txt" -Context $saContext -Force

One cool idea is writing to an Azure Table instead of writing to a Blob, or you might have a different idea to utilize this.

This idea can also be extended to several different scenarios. For example you can gatehr on a period basis some information about servers in your on-premises environment and write them into Azure for further analysis. Also storing log files, generating configurations and uploading / downloading them, the possibilities with Azure are limitless, you can do whatever you want.

 

Till the next blog…

Cheers 😃

 

Disclaimer – All scripts and reports are provided ‘AS IS’

This sample script is not supported under any Microsoft standard support program or service. This sample script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of this sample script and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of this script be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use this sample script or documentation, even if Microsoft has been advised of the possibility of such damages.

Leave a Reply