September-5th-2018, 03:45 PM
(This post was last modified: April-7th-2021, 05:47 AM by Armin@netPI.)
You all know the tile Control Panel in netPI's web GUI you click on to make a basic setup.
But what is if you want to configure those settings from remote over Ethernet instead of using mouse and keyboard? Or even better from a container running on netPI ?
All you need to know is how to access the Control Panel's HTTP RESTful API. Here is the example if you want to do it from a container:
STEP 1: Load the Raspbian container from here onto netPI.
Choosing the network mode host or bridge decides on the destination IP address you have to address later
For network mode host (container shares TCP/IP stack of host) use the "localhost" IP address 127.0.0.1 as <IP address>
For network mode bridge (container has its own TCP/IP stack, bridge by a bridge) use the "docker0" bridge default IP address 172.17.0.1 as <IP address>
STEP 2: Login to this container with a terminal over SSH as described in the container's documentation.
STEP 3: Install the http CLI command (or any other command like curl supporting http requests) in the container with
$ apt-get update && apt-get install httpie
STEP 4: Login
Call
$ http --verify=no --json POST "https://<IP address>/login" username=admin password=<your admin password>
and you get a response back like this
HTTP/1.1 302 Found
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept
Access-Control-Allow-Methods: GET,POST,DELETE
Access-Control-Allow-Origin: undefined
Connection: keep-alive
Content-Length: 47
Content-Type: text/plain; charset=utf-8
Date: Wed, 05 Sep 2018 12:50:52 GMT
Location: /content/?page=infocenter
Server: nginx/1.12.1
Vary: Accept, Accept-Encoding
X-Powered-By: Express
set-cookie: sessionGWManager=s%3A7GsIC0es7HyUv1LfwhZStCPlsvqzVYao.Jk6c4PGDGnJeRW9yTKZDeV6JPQqjYF%2FyIa0P%2FnyJ1LU; Path=/; HttpOnly
Found. Redirecting to /content/?page=infocenter
The most important part of the response is the value set-cookie that has to be used for all subsequent http commands as session token.
STEP 5: Get Info Center HTML page
Call
$ http --verify=no --json GET "https://<IP address>/content/?page=infocenter" 'Cookie:sessionGWManager=s%3A7GsIC0es7HyUv1LfwhZStCPlsvqzVYao.Jk6c4PGDGnJeRW9yTKZDeV6JPQqjYF%2FyIa0P%2FnyJ1LU'
and you get a response back like this
HTTP/1.1 200 OK
Accept-Ranges: bytes
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept
Access-Control-Allow-Methods: GET,POST,DELETE
Access-Control-Allow-Origin: undefined
Cache-Control: public, max-age=0
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Wed, 05 Sep 2018 14:36:11 GMT
ETag: W/"72d-1653d845b68"
Last-Modified: Wed, 15 Aug 2018 12:16:49 GMT
Server: nginx/1.12.1
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Powered-By: Express
<!-- Copyright © 2016 Hilscher Gesellschaft fuer Systemautomation mbH See Hilscher_Source_Code_License.txt -->
<!DOCTYPE HTML>
<html>
<head>
<title>Control Panel</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv='Content-Type' content='text/html;charset=UTF-8' />
<script type="text/javascript" src="netIOTAccessPages/js/zxcvbn.js"></script>
<script src="../../resources/sap-ui-core.js"
id="sap-ui-bootstrap"
data-sap-ui-libs="sap.ui.commons,sap.ui.table,sap.ui.unified, sap.ui.core, sap.m"
data-sap-ui-theme="sap_goldreflection">
</script>
<!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->
<link rel="stylesheet" type="text/css" href="css/animate.css">
<link rel="stylesheet" type="text/css" href="css/WebCommissioning.css">
<link rel="stylesheet" type="text/css" href="css/OpenUI5Hilscher.css">
<script src="../../socket.io/socket.io.js"></script>
<script type="text/javascript" src="lib/jquery.noty.packaged.min.js"></script>
<!--<script src="lib/logger.min.js"></script>-->
<script type="text/javascript" src="DataModel/dataModels.js"></script>
<script type="text/javascript" src="Structures/CommissioningStructs.js"></script>
<script type="text/javascript" src="Helper/PasswordHelper.js"></script>
<script type="text/javascript" src="Helper/DataTypeHelper.js"></script>
<script type="text/javascript" src="Helper/DataValidationHelper.js"></script>
<script type="text/javascript" src="Helper/IPTextFieldHelper.js"></script>
<script type="text/javascript" src="WebCommissioning.js"></script>
</head>
<body class="sapUiBody" role="application">
<div id="content"></div>
<!--<div id="container"></div>
<div id="log_messages"></div>-->
</body>
</html>
STEP 6: LAN network settings
Call
$ http --verify=no --json POST "https://<IP address>/set/networkSettings/LANSettings" 'Cookie:sessionGWManager=s%3A7GsIC0es7HyUv1LfwhZStCPlsvqzVYao.Jk6c4PGDGnJeRW9yTKZDeV6JPQqjYF%2FyIa0P%2FnyJ1LU' 'networksettings=[{"name":"eth0","macaddress":"b8:27:eb:23:7a:c1","dhcp":true,"ipaddress":["10","11","5","18"],"networkmask":["255","255","0","0"],"gateway":["10","11","0","10"],"dns":[["192","168","100","21"],["192","168","100","22"],"192.168.100.50"]}]' 'category=LAN'
and you get a response back like this
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept
Access-Control-Allow-Methods: GET,POST,DELETE
Access-Control-Allow-Origin: undefined
Connection: keep-alive
Content-Length: 15
Content-Type: application/json; charset=utf-8
Date: Wed, 05 Sep 2018 14:31:04 GMT
ETag: W/"f-v/Y1JusChTxrQUzPtNAKycooOTA"
Server: nginx/1.12.1
Vary: Accept-Encoding
X-Powered-By: Express
{
"status": "OK"
}
Today we have no full documentation of the RESTful API ready for you.
Instead I recommend to use web browsers like Firefox or Google Chrome to analyse the RESTful API using "development sessions" like the appended picture shows.
Both browsers can record with their embedded network traffic analyser the HTTP requests they are sending during your manual mouse click/keyboard sessions you can analyse afterwards in terms of web page, http header, cookie, parameters .... Then you can construct exactly the same requests with the http CLI command (or any other HTTP capable program) as shown above.
But what is if you want to configure those settings from remote over Ethernet instead of using mouse and keyboard? Or even better from a container running on netPI ?
All you need to know is how to access the Control Panel's HTTP RESTful API. Here is the example if you want to do it from a container:
STEP 1: Load the Raspbian container from here onto netPI.
Choosing the network mode host or bridge decides on the destination IP address you have to address later
For network mode host (container shares TCP/IP stack of host) use the "localhost" IP address 127.0.0.1 as <IP address>
For network mode bridge (container has its own TCP/IP stack, bridge by a bridge) use the "docker0" bridge default IP address 172.17.0.1 as <IP address>
STEP 2: Login to this container with a terminal over SSH as described in the container's documentation.
STEP 3: Install the http CLI command (or any other command like curl supporting http requests) in the container with
$ apt-get update && apt-get install httpie
STEP 4: Login
Call
$ http --verify=no --json POST "https://<IP address>/login" username=admin password=<your admin password>
and you get a response back like this
HTTP/1.1 302 Found
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept
Access-Control-Allow-Methods: GET,POST,DELETE
Access-Control-Allow-Origin: undefined
Connection: keep-alive
Content-Length: 47
Content-Type: text/plain; charset=utf-8
Date: Wed, 05 Sep 2018 12:50:52 GMT
Location: /content/?page=infocenter
Server: nginx/1.12.1
Vary: Accept, Accept-Encoding
X-Powered-By: Express
set-cookie: sessionGWManager=s%3A7GsIC0es7HyUv1LfwhZStCPlsvqzVYao.Jk6c4PGDGnJeRW9yTKZDeV6JPQqjYF%2FyIa0P%2FnyJ1LU; Path=/; HttpOnly
Found. Redirecting to /content/?page=infocenter
The most important part of the response is the value set-cookie that has to be used for all subsequent http commands as session token.
STEP 5: Get Info Center HTML page
Call
$ http --verify=no --json GET "https://<IP address>/content/?page=infocenter" 'Cookie:sessionGWManager=s%3A7GsIC0es7HyUv1LfwhZStCPlsvqzVYao.Jk6c4PGDGnJeRW9yTKZDeV6JPQqjYF%2FyIa0P%2FnyJ1LU'
and you get a response back like this
HTTP/1.1 200 OK
Accept-Ranges: bytes
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept
Access-Control-Allow-Methods: GET,POST,DELETE
Access-Control-Allow-Origin: undefined
Cache-Control: public, max-age=0
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Wed, 05 Sep 2018 14:36:11 GMT
ETag: W/"72d-1653d845b68"
Last-Modified: Wed, 15 Aug 2018 12:16:49 GMT
Server: nginx/1.12.1
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Powered-By: Express
<!-- Copyright © 2016 Hilscher Gesellschaft fuer Systemautomation mbH See Hilscher_Source_Code_License.txt -->
<!DOCTYPE HTML>
<html>
<head>
<title>Control Panel</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv='Content-Type' content='text/html;charset=UTF-8' />
<script type="text/javascript" src="netIOTAccessPages/js/zxcvbn.js"></script>
<script src="../../resources/sap-ui-core.js"
id="sap-ui-bootstrap"
data-sap-ui-libs="sap.ui.commons,sap.ui.table,sap.ui.unified, sap.ui.core, sap.m"
data-sap-ui-theme="sap_goldreflection">
</script>
<!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->
<link rel="stylesheet" type="text/css" href="css/animate.css">
<link rel="stylesheet" type="text/css" href="css/WebCommissioning.css">
<link rel="stylesheet" type="text/css" href="css/OpenUI5Hilscher.css">
<script src="../../socket.io/socket.io.js"></script>
<script type="text/javascript" src="lib/jquery.noty.packaged.min.js"></script>
<!--<script src="lib/logger.min.js"></script>-->
<script type="text/javascript" src="DataModel/dataModels.js"></script>
<script type="text/javascript" src="Structures/CommissioningStructs.js"></script>
<script type="text/javascript" src="Helper/PasswordHelper.js"></script>
<script type="text/javascript" src="Helper/DataTypeHelper.js"></script>
<script type="text/javascript" src="Helper/DataValidationHelper.js"></script>
<script type="text/javascript" src="Helper/IPTextFieldHelper.js"></script>
<script type="text/javascript" src="WebCommissioning.js"></script>
</head>
<body class="sapUiBody" role="application">
<div id="content"></div>
<!--<div id="container"></div>
<div id="log_messages"></div>-->
</body>
</html>
STEP 6: LAN network settings
Call
$ http --verify=no --json POST "https://<IP address>/set/networkSettings/LANSettings" 'Cookie:sessionGWManager=s%3A7GsIC0es7HyUv1LfwhZStCPlsvqzVYao.Jk6c4PGDGnJeRW9yTKZDeV6JPQqjYF%2FyIa0P%2FnyJ1LU' 'networksettings=[{"name":"eth0","macaddress":"b8:27:eb:23:7a:c1","dhcp":true,"ipaddress":["10","11","5","18"],"networkmask":["255","255","0","0"],"gateway":["10","11","0","10"],"dns":[["192","168","100","21"],["192","168","100","22"],"192.168.100.50"]}]' 'category=LAN'
and you get a response back like this
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept
Access-Control-Allow-Methods: GET,POST,DELETE
Access-Control-Allow-Origin: undefined
Connection: keep-alive
Content-Length: 15
Content-Type: application/json; charset=utf-8
Date: Wed, 05 Sep 2018 14:31:04 GMT
ETag: W/"f-v/Y1JusChTxrQUzPtNAKycooOTA"
Server: nginx/1.12.1
Vary: Accept-Encoding
X-Powered-By: Express
{
"status": "OK"
}
Today we have no full documentation of the RESTful API ready for you.
Instead I recommend to use web browsers like Firefox or Google Chrome to analyse the RESTful API using "development sessions" like the appended picture shows.
Both browsers can record with their embedded network traffic analyser the HTTP requests they are sending during your manual mouse click/keyboard sessions you can analyse afterwards in terms of web page, http header, cookie, parameters .... Then you can construct exactly the same requests with the http CLI command (or any other HTTP capable program) as shown above.
„You never fail until you stop trying.“, Albert Einstein (1879 - 1955)