Sublime Forum

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

#1

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]
<![CDATA[
if($_SERVER[‘REMOTE_ADDR’]=="${1:123.456.789.012}")echo("

".htmlentities(print_r(${0:var},true))."
");

]]>

prjm

source.php

[/code]

0 Likes

#2

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:

http://ipecho.net/plain
  1. How to perform a GET request from python (assuming python 3, ie. ST3)

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 :smile:

0 Likes