05 October 2012

FIND YOUR PUBLIC IP FROM COMMAND LINE



Lots of times you need to determine your public IP address, if you are using Linux operating system to power your PC, you may use these one liners to find your IP -->

Using wget

[root@test ~]# wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

Using curl

[root@test ~]# curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

Using Lynx

[root@test ~]# lynx -dump checkip.dyndns.org                                           OR

[root@test ~]# lynx -dump www.whatismyip.com | grep 'Your IP Add'               OR

[root@test ~]# lynx -dump http://www.ip2location.com |grep -e 'IP Address' -e '\[_\] '     OR

Though at any point of time you can open the browser and enter any of the following URL to the same effect but I would rather prefer to Unleash the power of Linux command lines/shell.Here are some examples of Python scripts which give the same result.

Using Python scripts as follows :--

#!/usr/bin/python
import urllib 
import string
import re

f = urllib.urlopen("http://checkip.dyndns.org")
s = f.read()
f.close()
pattern = re.compile('<body>(.*?)</body>',re.I|re.S)
for each in pattern.findall(s):
    print each

==================================================================

#!/usr/bin/python
import lxml.html
import string

source = lxml.html.parse("http://www.minip.no")
title_tag = source.find(".//title").text
title = string.split(title_tag, " ")
ip = title[3]
print ip

No comments:

Post a Comment