| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# Example script called by Mender client to collect device identity data. The |
| 4 |
# script needs to be located at |
| 5 |
# $(datadir)/mender/identity/mender-device-identity path for the agent to find |
| 6 |
# it. The script shall exit with non-0 status on errors. In this case the agent |
| 7 |
# will discard any output the script may have produced. |
| 8 |
# |
| 9 |
# The script shall output identity data in <key>=<value> format, one |
| 10 |
# entry per line. Example |
| 11 |
# |
| 12 |
# $ ./mender-device-identity |
| 13 |
# mac=de:ad:ca:fe:00:01 |
| 14 |
# cpuid=1112233 |
| 15 |
# |
| 16 |
# The example script collects the MAC address of a network interface with the |
| 17 |
# type ARPHRD_ETHER and it will pick the interface with the lowest ifindex |
| 18 |
# number if there are multiple interfaces with that type. The identity data is |
| 19 |
# output in the following format: |
| 20 |
# |
| 21 |
# mac=00:01:02:03:04:05 |
| 22 |
# |
| 23 |
|
| 24 |
set -ue |
| 25 |
|
| 26 |
SCN=/sys/class/net |
| 27 |
min=65535 |
| 28 |
arphrd_ether=1 |
| 29 |
ifdev= |
| 30 |
|
| 31 |
# find iface with lowest ifindex, skip non ARPHRD_ETHER types (lo, sit ...) |
| 32 |
for dev in $SCN/*; do |
| 33 |
if [ ! -f "$dev/type" ]; then |
| 34 |
continue |
| 35 |
fi |
| 36 |
|
| 37 |
iftype=$(cat $dev/type) |
| 38 |
if [ $iftype -ne $arphrd_ether ]; then |
| 39 |
continue |
| 40 |
fi |
| 41 |
|
| 42 |
# Skip dummy interfaces |
| 43 |
if echo "$dev" | grep -q "$SCN/dummy" 2>/dev/null; then |
| 44 |
continue |
| 45 |
fi |
| 46 |
|
| 47 |
idx=$(cat $dev/ifindex) |
| 48 |
if [ $idx -lt $min ]; then |
| 49 |
min=$idx |
| 50 |
ifdev=$dev |
| 51 |
fi |
| 52 |
done |
| 53 |
|
| 54 |
if [ -z "$ifdev" ]; then |
| 55 |
echo "no suitable interfaces found" >&2 |
| 56 |
exit 1 |
| 57 |
else |
| 58 |
echo "using interface $ifdev" >&2 |
| 59 |
# grab MAC address |
| 60 |
echo "mac=$(cat $ifdev/address)" |
| 61 |
fi |