Updated RPi Agent to be able to get IPs in all POSIX systems and NT

Shabirmean 9 years ago
parent d9bc5a3293
commit d29679dfd2

3
.gitignore vendored

@ -28,3 +28,6 @@ carbonapps
# Ignore generated device-type DBs # Ignore generated device-type DBs
*.h2.db *.h2.db
# Ignore generated .pyc pythin files
*.pyc

@ -1,4 +1,3 @@
# Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. # Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
# WSO2 Inc. licenses this file to you under the Apache License, # WSO2 Inc. licenses this file to you under the Apache License,
# Version 2.0 (the "License"); you may not use this file except # Version 2.0 (the "License"); you may not use this file except

@ -20,7 +20,7 @@
**/ **/
""" """
import time, commands import time
import ConfigParser, os import ConfigParser, os
import random import random
import running_mode import running_mode
@ -111,16 +111,32 @@ def initGPIOModule():
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Get the wlan0 interface via which the RPi is connected # Get the IP-Address of the interface via which the RPi is connected
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def getDeviceIP(): def getDeviceIP():
rPi_IP = commands.getoutput("ip route list | grep 'src '").split() # for POSIX system like MacOS and Linux
rPi_IP = rPi_IP[rPi_IP.index('src') + 1] if os.name != "nt":
if len(rPi_IP) <= 16: import commands
print "------------------------------------------------------------------------------------" rPi_IP = commands.getoutput("ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}'").split('\n')
print "IOT_UTILS: IP Address of RaspberryPi: " + rPi_IP if len(rPi_IP) > 0:
print "------------------------------------------------------------------------------------" print "------------------------------------------------------------------------------------"
return rPi_IP print "IOT_UTILS: IP Addresses of RaspberryPi: " + str(rPi_IP)
print "IOT_UTILS: IP Address used for HTTP Server: " + rPi_IP[0]
print "------------------------------------------------------------------------------------"
return rPi_IP[0]
# for windows systems
else:
from subprocess import check_output
rPi_IP = check_output("for /f \"tokens=14\" %a in ('ipconfig ^| findstr \"IPv4\" ^| findstr /v \"127.0.0.1\"') do echo %a", shell=True).decode()
rPi_IP = rPi_IP.replace('\r', '')
rPi_IP = rPi_IP.split('\n')
for IPs in rPi_IP:
if IPs != '' and len(aps) < 16:
print("------------------------------------------------------------------------------------")
print("IOT_UTILS: IP Address used for HTTP Server: " + IPs)
print("------------------------------------------------------------------------------------")
return IPs
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Loading…
Cancel
Save