forked from taniguti/ProfileManager_Helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pms_auto_add_device_to_group
executable file
·63 lines (51 loc) · 1.79 KB
/
pms_auto_add_device_to_group
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/sh
# Automatic add device to group by file.
# FILE FORMAT expocted. it must be csv text file.
# hostname,HWSerialNumber,GroupName
WID=`uuidgen`
ORDERFILE="/tmp/order.txt"
LOGFILE="/var/log/autoadd.log"
TMPDIR=/tmp/${WID}
TMPFILE=/tmp/${WID}/tmp
if [ "root" != `whoami` ]; then
echo "Use me as root."
exit 1
fi
# restart apsd: This makes service wrong.
# killall apsd; sleep 15
if [ -f "$1" ]; then ORDERFILE="$1"; fi
if [ ! -f "$ORDERFILE" ]; then exit 0 ; fi
cd `dirname "$0"`
mkdir ${TMPDIR}
cp "$ORDERFILE" "${TMPFILE}.order"
if [ $? -ne 0 ]; then exit 1 ; fi
cat "${TMPFILE}.order" | tr \\r \\n | sort | uniq | while read LINE
do
DEVICENAME=`echo "$LINE" | awk -F, '{print $1}' `
HWSERIAL=` echo "$LINE" | tr [:lower:] [:upper:] | awk -F, '{print $2}' | tr -d [:blank:]`
GROUPNAME="` echo "$LINE" | awk -F, '{print $3}'`"
if [ ${HWSERIAL}X = X ] ; then continue ; fi
if [ "${GROUPNAME}X" = X ] ; then continue ; fi
DN=`./pms_get_device_name $HWSERIAL`
if [ ${DN}X = X ]; then
echo "[`date +%D-%H:%M:%S`] [SKIPPED] $DEVICENAME is not yet enrolled." >> "$LOGFILE"
continue
else
DEVICENAME="$DN"
fi
if [ `./pms_get_devicemembers_of_group "${GROUPNAME}" | awk -F, -v HW="$HWSERIAL" '$3 == HW' | wc -l` -ne 0 ]; then
echo "[`date +%D-%H:%M:%S`] [INFO] $DEVICENAME is already member of ${GROUPNAME}. Nothing to do for it." >> "$LOGFILE"
continue
fi
./pms_add_device_to_devicegroup "$GROUPNAME" $HWSERIAL 2> ${TMPFILE}-err
if [ $? -eq 0 ]; then
echo "[`date +%D-%H:%M:%S`] [SUCCESS] $HWSERIAL ($DEVICENAME) added into ${GROUPNAME}." >> "$LOGFILE"
else
echo "[`date +%D-%H:%M:%S`] [ERROR] `cat ${TMPFILE}-err`" >> "$LOGFILE"
fi
done
rm -rf "$TMPDIR"
if [ `/bin/ls -l "$LOGFILE" | awk '{print $5}'` -gt 1000000 ]; then
gzip -S ".`date +%Y%m%d-%H%M%S`.gz" "$LOGFILE"
fi
exit 0