Home Download Buy Blog Forum Support

Is there a way to use an IP address in a snippet?

Is there a way to use an IP address in a snippet?

Postby jodi on Fri Apr 11, 2014 5:42 pm

I often need to access my own (external) IP address in a snippet. It would be great if there was a way to automatically fetch it and include it.

Here's the snippet as it stands:

Code: Select all
<snippet>
   <content><![CDATA[
if(\$_SERVER['REMOTE_ADDR']=="${1:123.456.789.012}")echo("<pre>".htmlentities(print_r(${0:var},true))."</pre>");

]]></content>
   <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
   <tabTrigger>prjm</tabTrigger>
   <!-- Optional: Set a scope to limit where the snippet will trigger -->
   <scope>source.php</scope>
</snippet>
jodi
 
Posts: 1
Joined: Fri Feb 14, 2014 10:49 am

Re: Is there a way to use an IP address in a snippet?

Postby qgates on Sat Apr 12, 2014 4:13 am

You won't be able to do this in a plain snippet, but it should be straightforward to make a simple plugin to either "insert external IP here" as a TextCommand, or insert a predefined snippet and populate a placeholder with the external IP address. Two pieces of the jigsaw you'll need:

1. How to get external IP address

You need a site that'll serve it up plain on a GET request. Here's one:
Code: Select all
http://ipecho.net/plain


2. How to perform a GET request from python (assuming python 3, ie. ST3)
Code: Select all
import urllib.request
...
urllib.request.urlopen("http://ipecho.net/plain").read()


The rest requires a little knowledge of Python but it'll be very simple. Take a look at some simple plugins on Github and you should be able to figure this out in 10-15 mins. Then you can post your working plugin in the "Plugin" forum :)
qgates
 
Posts: 205
Joined: Mon Nov 05, 2012 4:33 pm


Return to General Discussion

Who is online

Users browsing this forum: Sinnlosvoll and 31 guests