/
pandp_register.sh
executable file
·113 lines (99 loc) · 2.42 KB
/
pandp_register.sh
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/sh
# most functions copied from vladik update
register_url="http://pnp.spondoolies-tech.com/devices/registerDevice"
no_bootsource=201
mount_fail=204
NO_CONNECTION=205
# Mount boot partition if not mounted already.
mount_boot_partition()
{
grep -q /mnt/${bootfrom}-boot /proc/mounts ||
mount /mnt/${bootfrom}-boot 2>/dev/null ||
{ echo "Cannot mount /mnt/${bootfrom}-boot"; exit ${mount_fail}; }
}
umount_boot_partition(){
umount /mnt/${bootfrom}-boot 2>/dev/null
}
detect_boot_source()
{
# bootfrom=<BOOTSOURCE> is always the last element of kernel
# command line so may get it this way.
grep -q bootfrom /proc/cmdline &&
bootfrom=`sed 's/.*bootfrom=//' /proc/cmdline`
}
# get external ip
external_ip(){
ext_ip=`curl -s --head http://myexternalip.com/ | awk '/External-Ip/{print $2}'`
}
# get lan ip
lan_ip(){
lan_ip=`ifconfig | grep -A1 eth0 | awk '/inet/{print $2}' | cut -c 6-`
if [ -z "$lan_ip" ]; then
lan_ip="0.0.0.0"
fi
}
# get wan ip
wan_ip(){
wan_ip=`ifconfig | grep -A1 wan0 | awk '/inet/{print $2}' | cut -c 6-`
if [ -z "$wan_ip" ]; then
wan_ip="0.0.0.0"
fi
}
board_id(){
EEPROM_DEVICE=/sys/bus/i2c/devices/0-0050/eeprom
board_id=`dd bs=12 skip=7 count=1 if=$EEPROM_DEVICE 2>/dev/null`
}
check_connection()
{
ping -w2 "8.8.8.8" # ip, not fqdn. dns lookup will hang if there is no connection
}
debug(){
printf "WAN IP: %s\n" $ext_ip
printf "Lan IP: %s\n" $lan_ip
printf "Firmware: %s\n" $firmware
printf "Model: %s\n" $board
printf "Board ID: %s\n" $board_id
}
send_data(){
s=`curl -s -k --include --header "Content-Type: application/json" \
--request PUT \
--data-binary "{
\"deviceId\": \"$board_id\",
\"modelNumber\": \"$board\",
\"lanAddress\": \"$lan_ip\",
\"wanAddress\": \"$ext_ip\",
\"fwVersion\": \"$firmware\"
}" \
http://pnp.spondoolies-tech.com/devices/registerDevice \
| head -1 | awk '{print $2}'`
debug
}
check_enabled()
{
grep -q '"registerDevice":"false"' /etc/minepeon.conf && exit 1
}
main(){
check_enabled
check_connection
if [ $? != 0 ]; then
echo 'cannot register device, no connection' >> /var/log/messages
return $NO_CONNECTION
fi
detect_boot_source
mount_boot_partition
board=`cat /board_ver`
firmware=`cat /fw_ver`
board_id
external_ip
lan_ip
wan_ip
send_data
umount_boot_partition
if [ $s=200 ]; then
echo "saved"
else
printf "error registering machine, %s" $s | tee -a /var/log/messages
fi
}
main
return $?