? QA Design Gurus: Tip: How to get the IP Address of Raspberry Pi when performing IoT Testing

Oct 23, 2015

Tip: How to get the IP Address of Raspberry Pi when performing IoT Testing

As a tester when we test any IOT application, we need to know the IP Address of  Raspberry Pi. Even if you know the IP Address when you test the application on another network, again you need to find the IP Address.

Generally, you can find the IP Address of your machine using ifconfig/ipconfig command if it is already connected to a network. To execute this command on Raspberry Pi, you need to connect using any command line tool like Putty. To connect Raspberry Pi using any command line tool you need IP Address.

Using the following ways you can get the IP Address of  Raspberry Pi.
  • Using IP scanner tool:- There are some IP Scanner tools available in the market. For this, your PC and Raspberry Pi should be on the same network.
            E.g. "Advanced IP Scanner"


Ref: http://goo.gl/16dqGb

  • Using Mobile App:- You can find the Raspberry Pi address in any home network easily using "Wifi Watch" mobile app. You can just connect the ethernet cable to Raspberry Pi and scan the network using this app. It lists the Raspberry Pi with it's IP Address.
Ref: https://goo.gl/24xIqZ


Above options will work only if you use the ethernet cable to connect the network. The network should connect automatically without typing any password.

If you use WiFi, to connect the network you need to type the password. One way of finding is, you should connect Raspberry Pi to TV using HDMI cable so that you can see the desktop. You need to connect Keyboard to type Wifi password. Once the network is connected you can type ifconfig command on the terminal to get the IP Address. Next time onwords you can use above mentioned tools to find the IP Address.

Instead of using these tools, we can get the IP Address automatically when Raspberry Pi is rebooted. 

Steps:

1) Write a Python script to find the IP Address
2)  Add logic to send the IP Address to an email or to any endpoint
3) Create a Cron job to run this python script on system reboot

Example:

In this example, we are sending IP Address to a cloud application built using Progress Rollbase. Python Script invokes a REST Service to send IP Address. Rollbase application triggers an email to user.

import socket
import os
import urllib2
gw = os.popen("ip -4 route show default").read().split()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((gw[2], 0))
ipaddr = s.getsockname()[0]
gateway = gw[2]
host = socket.gethostname()
response = urllib2.urlopen("https://www.rollbase.com/rest/api/login?loginName=xxxxx&password=xxxxx").read()
start = "<sessionId>"
end = "</sessionId>"
token=(response.split(start))[1].split(end)[0]
ipRequestString="https://www.rollbase.com/rest/api/create2?sessionId="+token+"&objName=configObj&ipaddress="+str(ipaddr)
print ipRequestString
urllib2.urlopen(ipRequestString).read()
print ("IP:", ipaddr, " GW:", gateway, " Host:", host)


Cron job:

To open Crontab editor in Vi Editor
Open terminal and execute export EDITOR=Vi
Execute "crontab -e". This will open a Vi Editor.  Enter the following line and  enter Esc + shift colon + w +  q to save the file.

@reboot sleep 60 /usr/bin/python /home/ipaddress.py



1 comment: