Sunday, August 10, 2014

Shell script to get Public IP from Router with cURL

A few days ago I explained how to get public IP address of your Router. I showed a shell script that uses a utility called, "wget". "wget" is very convenient for casual use but there is another utility called, "cURL".

It seems that cURL follows MIT/X license and wget uses GPL. A big difference for me is that there is a C language library version of cURL called libcurl but wget doesn't have anything like that. Although I am a big fan of GPL, GPL is a kind of restrictive than MIT license. Also cURL seems to have support more network protocols. In many ways, cURL is more attractive.

Thus I decided to make another script with cURL, which is not too different from the wget version.
pi@desktoppi ~ $ sudo apt-get install curl
Reading package lists... Done
Building dependency tree       
Reading state information... Done
curl is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 64 not upgraded.
pi@desktoppi ~ $

Make sure you have curl installed with a command: sudo apt-get install curl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/sh
ID=admin
PW=your_password
URL=http://192.168.0.1/RgSetup.asp
Filter="IP Address"

CURL=/usr/bin/curl
OPTION=-s
GREP=/bin/grep
TR=/usr/bin/tr

if [ ! -f $CURL ]; then
        echo $CURL not found
        exit 1
fi
if [ ! -f $GREP ]; then
        echo $GREP not found
        exit 2
fi
if [ ! -f $TR ]; then
        echo $TR not found
        exit 3
fi

$CURL $OPTION --user $ID:$PW $URL | $GREP "$FILTER" | $TR -d '</>a-zA-Z '

Create a script file; I named it public_ip_curl.sh. Don't forget to use your own ID and Password.
Note that the script now uses curl instead of wget.

pi@desktoppi ~ $ ./public_ip_curl.sh
68.6.230.226
pi@desktoppi ~ $ 

You can get the same result.
*PS: make sure you gave executable permission with a command: chmod a+x public_ip_curl.sh

1 comment:

  1. I noticed my IP address hasn't been changed. hmmm...

    ReplyDelete

About Me

My photo
Tomorrow may not come, so I want to do my best now.