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

I/O REDIRECTION

There are always three default files open, stdin (the keyboard), stdout (the screen), and stderr (error messages output to the screen). Redirection simply means capturing output from a file, command, program, script, or even code block within a script and and sending it as input to another file, command, program, or script.

Numeric handles for file descriptors:

STDIN = 0 Keyboard input 
STDOUT = 1 Text output 
STDERR = 2 Error text output UNDEFINED = 3-9

REDIRECTION

command > filename                                               Redirect command output to a file
command >> filename                                             APPEND into a file 
command < filename                                               Pass a command from a text file
command A |  command B                                      Pipe the output from command A into command B 
command A & command B                                     Run command A and then run command B
command A && command B                                  Run command A, if it succeeds then run command B 
command A || command B                                      Run command A, if it fails then run command B

command 2 > filename                                            Redirect any error message into a file
command 2 >> filename                                          Append any error message into a file
command > file 2>&1                                               Redirect errors and output to one file
command > file 2<&1                                               Redirect output and errors to one file
command > fileA 2> fileB                                         Redirect output and errors to separate files
command 2>&1 >filename                   -------------->Wrong command                   

Redirect to NULL (hide errors)

command 2> /dev/null                                             Redirect error messages to NUL
command >/dev/null 2>&1                                      Redirect error and output to NUL
command >filename 2> /dev/null                           Redirect output to file but suppress error
 
We can also redirect to a printer with > PRN or >LPT1 
 
For more info read here and to read commands of linux shell and vbscript read here