SmartThings CLI in Termux (Android)

When I try to run the Smartthings CLI in Termux (Android Linux shell) I’m getting the following error:

~/ST3 $ ./smartthings devices --token=...
    Error: getaddrinfo EAI_AGAIN api.smartthings.com
    Code: EAI_AGAIN

Name resolver works:

~/ST3 $ nslookup api.smartthings.com
Server:         62.109.121.17
Address:        62.109.121.17#53

Non-authoritative answer:
Name:   api.smartthings.com
Address: 54.77.208.161
Name:   api.smartthings.com
Address: 46.137.152.66
Name:   api.smartthings.com
Address: 54.72.46.217
Name:   api.smartthings.com
Address: 34.252.8.126
Name:   api.smartthings.com
Address: 34.250.148.12
Name:   api.smartthings.com
Address: 52.209.180.133
Name:   api.smartthings.com
Address: 54.194.210.61
Name:   api.smartthings.com
Address: 108.128.33.190

Debugging on:

[2024-08-23T20:24:12.500] [DEBUG] rest-client - making axios request: {"url":"https://api.smartthings.com/devices","method":"get","headers":{"Content-Type":"application/json;charset=utf-8","Accept":"application/vnd.smartthings+json;v=20170916","User-Agent":"@smartthings/cli/1.9.0 linux-arm node-v18.5.0","Accept-Language":"en-US","Authorization":"Bearer 3a3b0e61-xxxx-xxxx-xxxx-xxxxxxxxxxxx"},"params":{"capabilitiesMode":"and"}}
Error: getaddrinfo EAI_AGAIN api.smartthings.com
    at Function.from (/snapshot/smartthings-cli/node_modules/axios/lib/core/AxiosError.js:86:14)
    at RedirectableRequest.handleRequestError (/snapshot/smartthings-cli/node_modules/axios/lib/adapters/http.js:391:25)
    at RedirectableRequest.emit (node:events:537:28)
    at ClientRequest.<anonymous> (/snapshot/smartthings-cli/node_modules/follow-redirects/index.js:38:24)
    at ClientRequest.emit (node:events:537:28)
    at TLSSocket.socketErrorListener (node:_http_client:465:9)
    at TLSSocket.emit (node:events:537:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

This is a test if the node module Axios works:

~/ST3 $ cat test.js
const axios = require('axios');

// GET-Anfrage an eine API
axios.get('https://api.smartthings.com/devices/')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('Error:', error);
  });

Execution:

~/ST3 $ node test.js
Error: AxiosError: Request failed with status code 401
    at settle (/data/data/com.termux/files/home/node_modules/axios/dist/node/axios.cjs:2015:12)
    at Unzip.handleStreamEnd (/data/data/com.termux/files/home/node_modules/axios/dist/node/axios.cjs:3131:11)
    at Unzip.emit (node:events:532:35)
    at endReadableNT (node:internal/streams/readable:1696:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
    at Axios.request (/data/data/com.termux/files/home/node_modules/axios/dist/node/axios.cjs:4262:41)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_BAD_REQUEST',

[...]

response: {
    status: 401,
    statusText: 'Unauthorized',
    headers: Object [AxiosHeaders] {
      date: 'Fri, 23 Aug 2024 20:59:19 GMT',
      'transfer-encoding': 'chunked',
      connection: 'keep-alive',
      server: 'openresty',
      'access-control-allow-origin': '*',
      'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS',
      'access-control-allow-headers': 'DNT,Keep-Alive,User-Agent,If-Modified-Since,Cache-Control,Content-Type,Accept,Authorization,X-ST-Client,X-ST-Api-Version,X-ST-Client-AppVersion,X-ST-Client-OS,X-ST-Client-DeviceModel'
    },

As you can see it basically works, just not authorized but it can resolve api.smartthings.com.

The same Smartthings CLI executable works on a virtual server but not in Termux. It is statically linked (smartthings-linuxstatic-armv7.tar.gz) so maybe something is missing?

IIRC, neither nslookup or dig use getaddrinfo from the resolver library, so those wouldn’t tell you whether the address can be successfully resolved or not by the resolver library. The error you are getting is for a resolver timeout so it’s some kind of a connectivity issue.

I suspect either 1) you have the https_proxy environment variable set but there is no proxy; or 2) you need something to proxy the https requests so you need to set it up.

Also, verify that you have an /etc/resolv.conf file with reachable DNS server IPs listed.

1 Like

Nothing special in the environment variables and when I tested the axios module (see test.js) it works as expected.

I can try it with a proxy later this evening.

Edit: quick test with

export http_proxy=http://192.0.2.0
export https_proxy=http://192.0.2.0

and it just hangs which could be a good sign…

Edit2: tried it with /etc/hosts

34.252.8.126 api.smartthings.com

Doesn’t work.

1 Like

Solution: with a curl request in front of it, it works. Not sure how often it is necessary but for now I’m doing it before every execution of smarttings.

~/ST3 $ cat st
curl https://api.smartthings.com/devices
./smartthings devices --token=abcdef
~/ST3 $ ./st
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 #   Label                         Name                                    Type     Device Id
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 1   Aqara-FP2                     aqara-fp2-zoneDetection                 LAN      47a54693-0592-4de0-a0ef-3b1d18xxxxxx
 2   Badezimmerlampe               c2c-switch                              VIPER    53bda9dc-522b-4e14-a452-177ff7xxxxxx
 3   Badezimmerlampe               c2c-switch                              VIPER    37aae6fc-a11d-493c-99fd-5c1b0cxxxxxx
 4   Badezimmerlampe               light-binary                            MATTER   fd961d4e-ac26-4b67-a57f-726da3xxxxxx
[...]

Edit: I’ve been trying to make it work since April. Every now and then I came back to the problem and nothing I tried fixed it. The debugger was pretty much useless and now this…