#!/usr/bin/python


'''
- Getexternalip v.alpha 2015-12-03 by hexufo.

Get the external ip randomly from a list of web sites.

Usage: getexternalip option

If your run the script without the option it returns just the ip.

Options:  more        prints the ip and the url of the server
          evenmore    prints the ip, the url of the server and the time of execution
          -h, --help  this text

Get the latest version here:
https://github.com/hexufo/misc

Project page on PyPI: https://pypi.python.org/pypi/misc-scripts

You can find me in various places on the internet:
hexufo @ {wordpress, twitter, github, gmail}

- This software is distributed under the Unlicense:

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>
'''


import sys
from random import randrange
import urllib2
import re
import time


useragents = [
  'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1',
  'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0',
  'Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0',
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:25.0) Gecko/20100101 Firefox/25.0',
  'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36',
  'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36',
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36',
  'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko',
  'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 7.0; InfoPath.3; .NET CLR 3.1.40767; Trident/6.0; en-IN)',
  'Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident/6.0)',
  'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))',
  'Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16',
  'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14',
  'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52',
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A',
  'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25',
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2',
  'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
  'Mozilla/5.0 (Linux; U; Android 2.3; en-us) AppleWebKit/999+ (KHTML, like Gecko) Safari/999.9',
  'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile myTouch 3G Slide Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1',
  'Opera/9.80 (J2ME/MIDP; Opera Mini/9 (Compatible; MSIE:9.0; iPhone; BlackBerry9700; AppleWebKit/24.746; U; en) Presto/2.5.25 Version/10.54',
  'Opera/9.80 (Android; Opera Mini/7.6.35766/35.5706; U; en) Presto/2.8.119 Version/11.10',
  'Opera/9.80 (J2ME/MIDP; Opera Mini/9.80 (S60; SymbOS; Opera Mobi/23.348; U; en) Presto/2.5.25 Version/10.54',
  'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)']

# Add your own server links here. Just google and you will get hundreds.
# Due to the terms of services of each site i do not include any more links, just some of the most popular.
servers = ['http://checkmyip.com/', 'http://cmyip.com/', 'http://icanhazip.com/', 'http://ip4.me/',
  'http://ipaddress.com/', 'http://ipcow.com/', 'http://ipgoat.com/', 'http://myexternalip.com/',
  'https://ipleak.net/', 'http://www.ipchicken.com/']


def usage():
  print "Getexternalip v.alpha 2015-12-09 by hexufo."
  print
  print "Get the external ip randomly from a list of web sites."
  print
  print "Usage: " + sys.argv[0] + " option"
  print
  print "If your run the script without the option it returns just the ip."
  print
  print "Options:  more        prints the ip and the url of the server"
  print "          evenmore    prints the ip, the url of the server and the time of execution"
  print "          -h, --help  this text"


def get(option):

  start = time.time()

  agentrnd = randrange(0,len(useragents))
  serverrnd = randrange(0,len(servers))

  url = servers[serverrnd]
  if (option == "more" or option == "evenmore"):
    print url

  try:
    req = urllib2.Request(url, headers={'User-Agent': useragents[agentrnd]})
    response = urllib2.urlopen(req)
    htmlpage = response.read()

    ips = re.findall('(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', htmlpage)
    print ips[0]

    end = time.time()
    executiontime = end - start

    if option == "evenmore":
      print "in " + str("%.2f" % executiontime) + " sec"
  except Exception, error_message:
    print "[-] Error: " + str(error_message)
    sys.exit(1)


if __name__ == "__main__":
  if (len(sys.argv) == 1):
    get('')
  elif (len(sys.argv) == 2):
    if sys.argv[1]:
      if str(sys.argv[1]) == "more":
        get("more")
      elif str(sys.argv[1]) == "evenmore":
        get("evenmore")
      elif (str(sys.argv[1]) == "-h" or str(sys.argv[1]) == "--help"):
        usage()
      else:
        usage()
  elif (len(sys.argv) > 2):
    usage()
  else:
    usage()


  sys.exit(0)


