03 March, 2010

Powershell - Sending IP address in email via GMAIL

I wanted to be able to Remote Desktop to my Home PC from work...

So, I had to enable RDP on the PC and set up port forwarding on my wireless router for  port 3389 (the machine I'm connecting to is Hard wired) - Port forwarding is easy enough, but the steps are specific to your brand of router (Google is your friend!). 

The last step was to open RDP on my work computer and connect to my home computer. To do this I needed the EXTERNAL IP address on my router. Your EXTERNAL IP information is not found on / in the physical machine you want to connect to if you use a router. It is presented to the outside facing portion of your network connection so you have to ask the outside world what IP address they see you using...

And the EXTERNAL IP is subject to change at various points in time depending on the dynamic IP lease / renew rate your ISP dictates... Unless you have a Static IP address. That is not to say that your dynamic IP address will change, but it can.

Rather than spending the money on a Static IP address, I decided to find a way to make my EXTERNAL IP address available to me while I was away (somehow). Enter Powershell...

(Note: I run Win 7 64 bit, so I use PS v2.0)

After some digging around / trial and error. I found a method that will ask for my EXTERNAL IP address (at 'http://checkip.dyndns.com:8245/') and send that information via e-mail (I use GMAIL) from my machine to my GMAIL account.

The initial return from grabbing the results of that websites information are brought in to Powershell as raw text (including the HTML tags):    
<html><head><title>Current IP Check</title></head><body>Current IP Address: xx.xx.xx.xx</body></html>




There are (of course) plenty of ways to remove the extraneous data and there are also ways to embed the HTML into the email...


I used this '-replace "[^\d\.]"' to strip out the tags and the letters (it replaces anything that is not a 'number' or a 'period', with a ~null~ value) - its function is also commented in the script:
    
This script is seeminly specific to GMAIL port and SSL requirements, but it may work for other systems as well.

I hope that this information can help someone else - in part or in whole:


##############  START ###############
$wc=New-Object net.webclient
$Results = $wc.downloadstring("http://checkip.dyndns.com:8245/") -replace "[^\d\.]"

#############################################################################################

####                       NOTES about " -replace "[^\d\.]"                              ####
####                                                                                     ####
#### "-Replace" replace "^" all Characters excluding "\d" numbers or "\." decimal points ####
####  ~ it literally only retains numbers and decimal points ~                           ####
####                                                                                     ####
#############################################################################################

$Username = "
XXXXXXXXXX" # account username goes inside the parenthesis
$PW = "xxxxxxxxxx" # account password goes inside the parenthesis"

$EmailFrom = "XXXXXXXX@gmail.com"
#FROM 'GMAIL' address goes inside the quote marks
$EmailTo = "xxxxxxxx@gmail.com"
# RECIPIENT address goes inside the quote marks
$Subject = "$Results - Home IP Address"

$Body = "$Results - Home IP Address"


$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $PW);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

##############  END ###############

10 comments:

  1. Wow i can say that this is another great article as expected of this blog.Bookmarked this site.. https://192-168-i-i.com/192-68-0-1/

    ReplyDelete
  2. This is truly a decent and enlightening, containing all data furthermore greatly affects the new innovation. A debt of gratitude is in order for sharing it my ip location

    ReplyDelete
  3. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..
    find social media links

    ReplyDelete
  4. Thanks For sharing this Superb article.I use this Article to show my assignment in college.it is useful For me Great Work. Aged 2008 to 2015 Old Gmail Accounts

    ReplyDelete
  5. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. https://hotmail-iniciar-sesion.email/

    ReplyDelete
  6. Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. Buy PVA Gmail Accounts

    ReplyDelete
  7. This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform! entrar no hotmail

    ReplyDelete
  8. Extremely helpful post. This is my first time i visit here. I discovered such a large number of intriguing stuff in your blog particularly its exchange. Truly its extraordinary article. Keep it up. گاد بت

    ReplyDelete
  9. Thanks for sharing this valuable content. In my view, if all webmasters and bloggers made good content as you did, the web will be a lot more useful than ever before. one funnel away challenge

    ReplyDelete
  10. Thanks for your previous informative and important post and specially this post.
    Buy Verified Stripe Accounts

    ReplyDelete