Thursday, August 7, 2014

Shell script to get Public IP from Router

When you have a RaspberryPi at home, you can use local IP address that starts with 192.168.XX.XX. This IP address will allow you to access your RaspberryPi at home but not from outside of your home such as your work, Starbucks or your gym. In order to access your Raspberry Pi from outside of your home, you need to know Public IP address of your Router. Then you will need to "Port forward" from the Router to your RaspberryPi.
First drawing with Wacom Bamboo I bought a year ago. lol

I am not going to explain how to setup your Router to "Port forward" but I am going to explain how to get the Public IP address in your Raspberry Pi. Do not forget to change the password of your default ID, pi. You can change the password with a command: "passwd" or "passwd pi".
pi@desktoppi ~ $ whoami
pi
pi@desktoppi ~ $ passwd
Changing password for pi.
(current) UNIX password: 
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
pi@desktoppi ~ $ 

When you change your passwd, it may ask you again if the password is too simple but you can force setting it with "sudo passwd pi", if you insist.


Most of time, the local IP address of your Router is 192.168.0.1 or 192.168.1.1; if they don't work, try 192.168.0.0 although I have never seen 0.0.Mine happens to be 192.168.0.1 and when I type in the IP address to web browser, it asks me login ID and Password. You need to figure out what ID and Password are for your router. You can ask your ISP( Internet Service Provider ) or try some default ID/Password.


Once you login, you may see something like this screenshot above. In my case, when I clicked on "Basic Settings", it shows me the public IP address, which is shown as "WAN IP Address". It also says that the public IP will expire and be changed to something else tomorrow; today is Aug/6/2014.


When I click on the link, "Basic Settings", while I am holding "Ctrl" key on the keyboard, I can open the specific web page in a new browser tab. It can tell me what the direct URL is, which is "http://192.168.0.1/RgSetup.asp" in this case. The URL is what I needed to figure out my Public IP address of the Router.

pi@desktoppi ~ $ sudo apt-get install wget
Reading package lists... Done
Building dependency tree       
Reading state information... Done
wget is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 64 not upgraded.
pi@desktoppi ~ $ 

I am going to access the Router webpage from RaspberryPi and filter out the IP address numbers. I will show you how to do it with a program called "wget". You can install "wget" by a command "sudo apt-get install wget". I have already installed, so the screenshot above will be different from what you may see.

pi@desktoppi ~ $ wget -qO- --user=admin --ask-password http://192.168.0.1/RgSetup.asp | grep "IP Address" | tr -d "</>trdb WANIPAddress"
Password for user `admin': 
68.6.129.115
pi@desktoppi ~ $ 
"wget" is a program that can access a webpage and store as a file. I am using four options. "q" is to quiet and "O-" means I want to see the webpage HTML on the screen instead of saving it as a file. "--user=admin" means if the website asks login information, the ID is "admin". "--ask-password" means I will type-in the password. The URL is followed by all the options. Note that "options" must be listed before the URL. The output from wget is sent to another program, "grep". "grep" will show lines that I am interested in. The output from "grep" is sent to the 3rd program, "tr". The option I typed-in means that I want to delete all the characters, "< / > trdb WANIPAdres", so that I want to see only numbers. I guess I can do "tr -d "a-zA-Z < / >", instead.

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

WGET=/usr/bin/wget
GREP=/bin/grep
TR=/usr/bin/tr

if [ ! -f $WGET ]; then
        echo $WGET 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

$WGET -qO- --user=$ID --password=$PW $URL | $GREP "$Filter" | $TR -d '</>a-zA-Z '

Now we know how to get the public IP address but it is too long to type-in all the time. Here is a shell script that saves our time. I am not going to explain how to write a script. As you can see, the script is much longer than just one line command I showed you above. I cannot reason all the details why I wrote it this way. Here is some bullet points:
  • There are a few different Shell programs such as "sh", "bsh", "bash", "csh" and "tcsh". Among them, "sh", is the most basic shell program. The reason why I prefer "sh" over others is because most of Unix/Linux systems have it almost always while other shell programs might not be installed. Downside of "sh" is that it doesn't have fancy syntax or features that other shell programs have.
  • I place User specific settings at the very beginning.
  • I place System specific setting next with full file path; sometimes a system has two or more than two identical executable file names in different folders so it is better to use full file path.
  • I always make sure that those system files actually exists.
  • Lastly the real command is placed at the end.

Once you have the script saved as a file, you will need to give an executable permission by a command: "chmod a+x public_ip.sh" assuming the file name is public_ip.sh. When you give a command, "ls -l public_ip.sh", you should see that there are three "x" characters indicating that it has execution permission.
pi@desktoppi ~ $ ./public_ip.sh
68.8.129.115
pi@desktoppi ~ $ 

When everything went right, now you can simply execute the script and get the IP address. Don't forget to have "./" in front of the script file name; I am not gonna explain why.

With the public IP address retrieved, you can send it to your email address; this will be another long article soon. Once you know the public IP address of your Router, you should be able to access your RaspberryPi from outside of your home.

No comments:

Post a Comment

About Me

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