How to get your own public IP address in real time using PowerShell

Source: Internet
Author: User

I do not know how long has not updated the blog, this is actually long ago wanted to write but a drag is a few months, the recent procrastination is really crazy outbreak. Let's talk about what you want to write about today, but it's very simple, that is, how to use PowerShell to get a computer's public IP address in real time. Public network IP is what does not need to say, real-time access to public IP is the meaning of what?


First of all, the background of this article, the home has a desktop for a variety of reasons often all day or whole week of the boot (do experiments or hang the machine and so on), many times in the unit want to see how the situation at home or want to do experiments are required to connect back remotely, even the way back to a few, One is through Remote Desktop, this method advantage is now Remote Desktop because of the reasons for optimization, efficiency is very high, when using the basic feeling is at home, very smooth. The disadvantage is also very obvious, open some need graphics card things are not very likely, after all, there is no remote FX this thing. There is also a kind of tool through the TeamViewer remote capture to the desktop, the advantage is because it is the local session so there will be no graphics card problems, but there are many shortcomings, one is not efficient enough, often card, there is a in case the account dropped the basic game over.


So in general I am a combination of two, the need for efficiency when using remote Desktop, the need for a video card when the TeamViewer. But the principle of remote Desktop does not need to say, need a public network IP to the computer NAT. Generally like Unicom this ISP will provide a separate public IP to you, in addition to 80 of this you can not use Remote Desktop is not limited to you, but this ISP provided to individual users of the public IP is generally dynamic, restart the next router will get a new public network IP, So want to go back to the remote to separate the method of IP address is basically not feasible, this kind of problem is also very easy to solve, you can register a DDNS service to bind a domain name method to solve this problem, general like peanut shell This free is enough, After registration is completed in your router login (provided that the router support) and then each time the router gets to the new public network IP will automatically update the IP address of the domain name, so you need to remember this domain name can be.


This method is really very convenient, most of the time can provide more stable service, but many times because of various reasons, the domain name corresponding IP address updates will lag, resulting in our inability to resolve through the domain name to the real-time public network IP, if Teamviewwer still can use the words of course fortunately, But if even TeamViewer is lost, it's really out of the way. If the morning out of this problem to do the experiment and do the rise, it is really anxious to hurry back to see what is going on.


In order to deal with this situation, I specialize in a solution, we can run a script on the computer, the current public IP will be sent to us on a regular basis, so that we do not need to worry about the above mentioned situation, as long as there is no network we can be real-time to grasp the current public network IP is what

So how to achieve, first of all to see the script how to write, in fact very simple, access to public IP we can go to a website to query http://www.ip138.com/, click on this site will see their public IP is what, By parsing the code of this website you can actually find that the real address of the query public IP is actually http://1212.ip138.com/ic.asp this. You directly access this address in the browser will get the most intuitive query results, so we want to get the public IP is very simple, send a WebRequest to this page, and then the return of the results of processing it OK


The way to send WebRequest in PowerShell is invoke-webrequest, except for some. NET class methods, and the code that gets the IP address is posted directly below

$body = Invoke-webrequest-uri "http://1212.ip138.com/ic.asp" $str = $nullif ($body. Statuscode-eq) {[string] $str = $body. Parsedhtml.body.innerhtml$startindex = $str. IndexOf ("[") $EndIndex = $str. IndexOf ("]") $length = $EndIndex-$StartIndex -1$ip = $str. Substring ($StartIndex + 1, $length) $ip}else{write-warning "Bad Request"}


Can be found quite simple, because the structure of the interface is very concise, so the basic processing is not difficult. You can run it on your own computer to see what the results are.


After that is how to send out the results, the most commonly used method of course is the mail, automatic e-mail should be said to be nothing new, there is an SMTP server inside the enterprise has an account with PowerShell to send an email is simple, but this method changes will be relatively large, After all, the initiative is not on your side. So we can use some public mailboxes, such as 163 or Microsoft's mailbox, I began to try to use Microsoft's MSN Mailbox to achieve, but later found that Microsoft's security mechanism is very strict, if a period of time with this automatic request, you will be required to enter a verification code, Otherwise the mail is unable to send, had to forget. Try to try NetEase Mail, netease or more flexible, NetEase will give you a name called the client authorization password thing, with this thing can be unblocked email. Take a look at how to set



First enter the NetEase mailbox after clicking on the account and email center

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/A3/wKioL1cGGwvC-sYYAABjbBbxcAI491.png "title=" Untitled picture.png "alt=" Wkiol1cggwvc-syyaabjbbbxcai491.png "/>

Then click on the client authorization password and select Turn on

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/A3/wKioL1cGG1SjjpZ6AAC7o-pdGvE812.png "title=" Untitled picture.png "alt=" Wkiol1cgg1sjjpz6aac7o-pdgve812.png "/>

Then get the verification code.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7E/A3/wKioL1cGG5ygCjXZAABkYFZD-v8506.png "title=" Untitled picture.png "alt=" Wkiol1cgg5ygcjxzaabkyfzd-v8506.png "/>


The authorization code is turned on after the setup is complete, you can use it directly as a password

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/A3/wKioL1cGHIejuUa-AABdzi_QpI0242.png "title=" Untitled picture.png "alt=" Wkiol1cghiejuua-aabdzi_qpi0242.png "/>


Then how to use this authorization code to send e-mail automatically, very simple, the code is as follows

$password = Convertto-securestring-asplaintext ' * * Your Authorization code * * '-force$username= ' ******@163.com ' $cred =new-object System.Management.Automation.PSCredential ($username, $password) send-mailmessage-from $username-to ' *****@ Outlook.com '-subject "IP Address"-body "IP Address: $ip"-smtpserver smtp.163.com-credential $cred-usessl


Then it's ready. Check the mail, the discovery has been received.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/A3/wKioL1cGHbnTFsi2AACaJQpaFy4199.png "title=" Untitled picture.png "alt=" Wkiol1cghbntfsi2aacajqpafy4199.png "/>

You can then combine the Windows Task Scheduler to run this script on a timed basis.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/A7/wKiom1cGHefAgrz1AACEbXxXk5E322.png "title=" Untitled picture.png "alt=" Wkiom1cghefagrz1aacebxxxk5e322.png "/>


Note that this option is best checked, because if the computer sleeps, the script will not be able to run

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/A3/wKioL1cGH1Pxy8lJAAB3iOzYBk8118.png "title=" Untitled picture.png "alt=" Wkiol1cgh1pxy8ljaab3iozybk8118.png "/>


OK, to here is done, as to how often run once to see their own needs, so long as the net we can master their own public network IP is what.

This article is from the "Just Make It Happen" blog, so be sure to keep this source http://mxyit.blog.51cto.com/4308871/1761385

How to get your own public IP address in real time using PowerShell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.