Skip to content

Instantly share code, notes, and snippets.

@KillerGoldFisch
Created May 25, 2013 14:08
Show Gist options
  • Save KillerGoldFisch/5649176 to your computer and use it in GitHub Desktop.
Save KillerGoldFisch/5649176 to your computer and use it in GitHub Desktop.
This BeanShell script loads the IP-Informations from IP-Address using the Geo-IP API.
/**************************************************
*
* This BeanShell script loads the IP-Informations
* from IP-Address using the Geo-IP API.
*
* usage: bsh <this-file>.bsh <the-ip>
*
* dependency:
* - smart-ip.net
* - gson
*
* author:
* - Kevin Gliewe
*
***************************************************/
addClassPath("./gson-2.2.4.jar");
import com.google.gson.Gson;
import java.io.*;
import java.net.*;
public class HTTP {
public static String get(String urlToRead) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
public class IPInfo {
public String source;
public String host;
public String lang;
public String countryName;
public String countryCode;
public String city;
public String region;
public String latitude;
public String longitude;
public String timezone;
}
//bsh.args
Gson gson = new Gson();
String ip = "12.34.56.78";
if(bsh.args.length > 0)
ip = bsh.args[0];
IPInfo info = gson.fromJson(HTTP.get("http://smart-ip.net/geoip-json/" + ip), IPInfo.class);
print(info.source);
print(info.host);
print(info.lang);
print(info.countryName);
print(info.countryCode);
print(info.city);
print(info.region);
print(info.latitude);
print(info.longitude);
print(info.timezone);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment