Translate

sábado, 20 de octubre de 2012

How to make your computer send an email containing your public IP

For most of the Geeks I used to know, is something very usual to need acces to their/our home computers. In most of the cases we use Terminal Services, but this, supose the need to know the public IP of our home router which could route the Terminal Service to the correct Computer into our home network.
This is not a problem if you have a fixed public IP, but here in Spain, it supose about 10-20€ per month depending on the ADSL provider.
Other way to solve this problem, is to use a DynDNS, No-IP, etc... who cares for the changes in your public IP, but actually most of this Companies started to charge a little amount of many for their services.
So,.... we are in crisis, we need a free-cost way to solve the problem, and for this, we have the help of PowerShell in the new windows OS.
 
The idea is to have a computer inside our network, wich could obtain the public IP of the router, and finally sent it to an email account that we can access to see the desired IP to use with the Terminal Services.

So this is what we need:

  • A computer at home which is going to obtain the IP and sent it to us via email. This computer must have a Windows OS with PowerShell (W2k8, W7, etc...)
  • A google account to receive the mail with the IP.
  • A Script that search the public IP using whatismyip.com, put this IP into in email, and sent it to our email.


This is the code for the script file:

" Ugehidalgo@gmail.com. "
" "
"   Script for powershell to obtain the public Ip for the server and send it to an email account. "
" "
" " 
"Obtaining the public IP: C:\discIP\temp.txt"
$client = new-object System.Net.WebClient
$client.DownloadFile("
 
$Content = [IO.File]::ReadAllText("C:\discIP\PublicIP.txt")
" "
"Public IP is: $Content"
" "
"Preparing the mail"
$From = "user mail"
$To = "user mail"
$Subject = “Public IP for your Router”
$Body = “The Public IP for your home´s router is: $Content”
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.mail.SmtpClient($SMTPServer,587)
$SMTPClient.EnableSsl = $true
$SMTPUser = "usuario"
$SMTPPass = "password"
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPUser",“$SMTPPass”)
" "
"Sending de Mail -- Please wait"
$SMTPClient.Send($From, $To, $Subject, $Body)
 
To write and execute this script you need PowerShell. Would be better if you have acces to PowerShell ISE, because it could correct posible mistakes during the typing process.
To execute scripts you need to grant access to script execution. To see if you OS is script execution permited type this directly to the PowerShell:
 
Get-ExecutionPolicy
 
Powershell will respond indicating if execution is restricted or not. In case it  is restricted we must tape:
 
Set-ExecutionPolicy Unrestricted
 
 
Basically the script goes to the web whatismyip wich has a service that provides your IP in a web page in plain text including only the value we need.
Using DownloadFile it download this page into a txt file called PublicIP.TXT.
 
The next step is to pass the entire file to a variable into our script. Don´t worry, the file only has your public IP, so using [IO.File]::ReadAllText we could have our public IP inside a variable to use into the script. In this case I used the variable $Content.
 
Now it´s time to prepare the mail with the "from", "to", "body" (of course our IP in $Content), and the rest of the data that SSMTPClient needs to send the mail.
There are a few of important points here. First the mail server, and the access to this mail server. its important to set the port to 587, the SSL access and to give both the user and the password. The server mail is of-course smtp.gmail.com
 
At this time I supose you must be very scared to put your mail account user and password  both together in the script code, there´s another way to do this without the need to put this password inside the script code, but this way uses a prompt to ask you for it, and when the program would run you wouldn´t be there to type the password, so we must do it in such this way, but we could use a secondary email account which passes this mails to our principal one, in this way we are not compromising our principal email account.
 
The last thing we must do is to set a Scheduled task in our Windows OS that launch the script once or twice a day. In future versions the script would sent the mail only if there was an IP Change avoiding unnecessary emails.

No hay comentarios: