from
GetMyIP - Retrieves external IP address & reverse nslookup
by Yair Altman
Retrieves the current external IP address and reverse DNS lookup value
|
| getmyip
|
function [ip,nslookup] = getmyip
% getmyip Retrieves the current external IP address and reverse DNS lookup value
%
% Syntax:
% [ip,nslookup] = getmyip
%
% Description:
% IP = GETMYIP retrieves the current machine's external IP address.
% [IP,NSLOOKUP] also retrieves the reverse lookup (nslookup) value
% for the external IP found.
%
% GETMYIP uses whatismyip.com to find the IP and name-space.com for
% the NSLOOKUP.
%
% Limitations:
% GETMYIP uses external webpages so Internet connection is required (Duh!)
% GETMYIP uses regexp, so it does not work on old Matlab versions
%
% Bugs and suggestions:
% Please send to Yair Altman (altmany at gmail dot com)
%
% Change log:
% 2007-11-23: First version posted on <a href="http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectType=author&mfx=1&objectId=1096533#">MathWorks File Exchange</a>
% 2010-09-24: Modified external IP lookup URL based on Doug Schwarz's suggestion in http://www.mathworks.com/matlabcentral/newsreader/view_thread/292100
% Programmed by Yair M. Altman: altmany(at)gmail.com
% $Revision: 1.1 $ $Date: 2010/09/24 09:19:32 $
try
% Initialize
ip = [];
nslookup = [];
% Try to read getmyip.org to find the external ip
%if nargout
% See the discussion in http://www.mathworks.com/matlabcentral/newsreader/view_thread/292100
%ip = searchInUrl('http://getmyip.org', 'Your IP address is:</b>([^<]+)');
ip = urlread('http://www.whatismyip.com/automation/n09230945.asp');
%end
% If external IP was found, try to find the reverse nslookup
if ~isempty(ip) && nargout>1
nslookup = searchInUrl(['http://name.space.xs2.net/cgi-bin/nslookup.pl?pageid=NSLOOKUP&submit=search&nsinput=' ip], 'Name:(.+)Address:');
end
% Error handling
catch
v = version;
if v(1)<='6'
err.message = lasterr; % no lasterror function...
else
err = lasterror;
end
try
err.message = regexprep(err.message,'Error using ==> [^\n]+\n','');
catch
try
% Another approach, used in Matlab 6 (where regexprep is unavailable)
startIdx = findstr(err.message,'Error using ==> ');
stopIdx = findstr(err.message,char(10));
for idx = length(startIdx) : -1 : 1
idx2 = min(find(stopIdx > startIdx(idx))); %#ok ML6
err.message(startIdx(idx):stopIdx(idx2)) = [];
end
catch
% never mind...
end
end
if isempty(findstr(mfilename,err.message))
% Indicate error origin, if not already stated within the error message
err.message = [mfilename ': ' err.message];
end
if v(1)<='6'
while err.message(end)==char(10)
err.message(end) = []; % strip excessive Matlab 6 newlines
end
error(err.message);
else
rethrow(err);
end
end
%% Read a webpage and search a substring within it
function data = searchInUrl(url, regexStr)
% Ensure that hFig is a figure handle...
str = urlread(url);
[dummy1,dummy2,dummy3,dummy4,data] = regexp(str,regexStr);
if ~isempty(data)
data = strtrim(data{1});
if iscell(data)
data = data{1};
end
end
|
|
Contact us at files@mathworks.com