首页 > 解决方案 > BASH 中的自动化——在 AWS-IoT 中创建事物。创建后如何检索thingId?

问题描述

这是我们在 AWS 中使用 BASH 脚本创建事物的时候。它运行良好,但我想检索 thing_ID 并将其存储在变量中。

这是创建事物的 bash 代码。这是蚊子的第一次调用,mosquitto_pub --cafile root.cert --cert deviceCertAndCACert.crt --key deviceCert.key -h xxxxx.iot.us-east-2.amazonaws.com -p 8883 -q 1 -t foo/bar -I anyclientID --tls-version tlsv1.2 -m "Hello" -d但在第一次连接中它只返回:

Client anyclientID18351 sending CONNECT
Error: The connection was lost.

创建后如何检索thing_Id?

这是代码:

openssl genrsa -out deviceCert.key 2048
openssl req -new -key deviceCert.key -out deviceCert.csr -subj "/C=CA/ST=ON/L=NY/O=SC/OU=DG/CN=EX"
openssl x509 -req -in deviceCert.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out deviceCert.crt -days 365 -sha256
cat deviceCert.crt rootCA.pem > deviceCertAndCACert.crt
wget https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem
mv VeriSign-Class\ 3-Public-Primary-Certification-Authority-G5.pem root.cert
mosquitto_pub --cafile root.cert --cert deviceCertAndCACert.crt --key deviceCert.key -h xxxxx.iot.us-east-2.amazonaws.com -p 8883 -q 1 -t foo/bar -I anyclientID --tls-version tlsv1.2 -m "Hello" -d
mosquitto_pub --cafile root.cert --cert deviceCertAndCACert.crt --key deviceCert.key -h xxxxx.iot.us-east-2.amazonaws.com -p 8883 -q 1 -t foo/bar -I anyclientID --tls-version tlsv1.2 -m "Thing $i is Working" -d

标签: bashautomationaws-cliaws-iot

解决方案


如果您知道事物的名称,则可以在 CLI 中使用describe-thing命令。

aws iot describe-thing --thing-name ENTER_THING_NAME_HERE

回复:

{
    "defaultClientId": "anyclientID",
    "thingName": "THING_NAME",
    "thingId": "40da2e73-c6af-406e-b415-15acae538797",
    "thingArn": "arn:aws:iot:us-east-2:123456789012:thing/THING_NAME",
    "thingTypeName": "LightBulb",
    "attributes": {
        "model": "123",
        "wattage": "75"
    },
    "version": 1
}

推荐阅读