OWASP O2 Platform Blog

Using a webservice to get the IP Address

These scripts show how to use this xml feed to access information about the ‘current user IP address’.

This first code snippet shows how to get the IP address value from the downloaded xml file:

var currentIpAddress = @"<a href="http://ip-address.domaintools.com/myip.xml">http://ip-address.domaintools.com/myip.xml</a>"
       .uri()
       .getHtml()
       .xRoot()
       .element("ip_address")
       .value();
return currentIpAddress; 

 

This second example will show a more advanced script which downloads the XML feed  and displays it in table list (the “IP Address history” was added for the cases where one wants to confirm a IPAddress change (for example in the case of connecting to a VPN)):

//var topPanel = panel.clear().add_Panel();
var topPanel = O2Gui.open<Panel>("IPAddress", 650,400);
var tableList = topPanel.add_TableList("IP Details (select to copy value to clipboard)");
tableList.afterSelect(
 (selectedRows)=>{      
      if(selectedRows.notNull() &&  selectedRows.size()==1)      
       selectedRows[0].values()[1].toClipboard();       
     });

var ipAddress = tableList.insert_Above<Panel>(40)
         .add_Label("Your current IP is:").size(20)
         .append_Label("...").size(20).autoSize();
var history = tableList.insert_Right<Panel>(250).add_GroupBox("IPAddress History").add_TextArea();
Func<Dictionary<string,string>> getIpData =
 ()=>{
   var ipServiceUrl = @"<a href="http://ip-address.domaintools.com/myip.xml">http://ip-address.domaintools.com/myip.xml</a>";
   var xmlData = ipServiceUrl.uri().getHtml();   
   if (xmlData.valid())
   {  
    var ipData = new Dictionary<string,string>();
    foreach(var element in xmlData.xRoot().elements())
     ipData.Add(element.name(), element.value());    
    return ipData;    
   }
   return null;
  };

Action showIpData =
 ()=>{
   tableList.clearTable();
   ipAddress.set_Text("fetching data...").textColor(Color.DarkGreen); 
   var ipData = getIpData(); 
   if(ipData.notNull())
   {    
    tableList.show(ipData).makeColumnWidthMatchCellWidth();        
    ipAddress.set_Text(ipData["ip_address"]).textColor(Color.DarkBlue);
    history.append_Line("[{0}]: {1}".format(DateTime.Now.str(),ipData["ip_address"]));
   }
   else   
    ipAddress.set_Text("error!!!").textColor(Color.Red); 
  };

ipAddress.append_Link("refresh", ()=>showIpData()).left(450).top(12);

showIpData();
return "ok";

Note: this script is included in the Dynamic Scripts included with O2: Util – View Current IP Address info.h2

November 26, 2010 - Posted by | WS

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: