Read ConnectPort X2's IP Address using Python

Hello,

I am getting started with Digi’s CP X2 and am trying to figure out a way to programatically read the gateway’s IP address in python.

I tried using the SIOCGIFADDR method described here:
http://code.activestate.com/recipes/439094-get-the-ip-address-associated-with-a-network-inter/

but it appears the X2 doesn’t support the 0x8915 command in the fcntl.ioctl() call (Invalid argument error).

When I telnet into the gateway, I noticed the “display netdevice” command prints the IP address, so I might be able to run that command and parse the output. But it appears Python doesn’t support the subprocess() or os.system() calls so that doesn’t seem to work.

Has anyone figured out a way to obtain the gateway’s IP address using python? Any ideas or suggestions would be very appreciated!

You can accomplish this by querying the settings via the Device Cloud. The following is an example Exported from the Device Cloud API Explorer found under the Documentation tab. You would fill in the items in bold as necessary. This will return all of the network settings, you would simply parse for the “ip”:

The following lines require manual changes

username = “YourUsername” # enter your username
password = “YourPassword” # enter your password

This example establishes a https connection, but doesn’t provide the server certificate validation.

Production code should implement certificate validation.

-------------------------------------------------

import httplib
import base64

create HTTP basic authentication string, this consists of

“username:password” base64 encoded

auth = base64.encodestring(“%s:%s”%(username,password))[:-1]

message to send to server

message = “”"

“”"
webservice = httplib.HTTPSConnection(“login.etherios.com”)

to what URL to send the request with a given HTTP method

webservice.putrequest(“POST”, “/ws/sci”)

add the authorization string into the HTTP header

webservice.putheader(“Authorization”, “Basic %s”%auth)
webservice.putheader(“Content-type”, “text/xml; charset="UTF-8"”)
webservice.putheader(“Content-length”, “%d” % len(message))
webservice.endheaders()
webservice.send(message)

get the response

response = webservice.getresponse()
statuscode = response.status
statusmessage = response.reason
response_body = response.read()

print the output to standard out

print (statuscode, statusmessage)
print response_body

Thank you very much for the quick answer! Not to sound picky, but is there any local python solution I can use to avoid the device cloud subscription?

The “display netdevice” command has the IP address in the output. There must be some way to locally obtain the IP address without going to the cloud (I hope!).

I think you can open a socket at the port 23 and issue the required command, I have done this in java for CPX4 not sure in python. Give a try and check.