get_net_ids.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2019 Clementine Computing LLC.
  4. #
  5. # This file is part of PopuFare.
  6. #
  7. # PopuFare is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # PopuFare is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. export BASEDIR="/home/bus"
  21. . $BASEDIR/bin/common_values.sh
  22. SERIAL_PORT="/dev/ttyGPRS"
  23. PORT_CONFIG="115200 -raw"
  24. REPLY_TIMEOUT="3"
  25. RAW_DROPFILE="/tmp/raw_net_ids"
  26. COOKED_DROPFILE="$NETWORK_ID_DROPFILE"
  27. function collect_modem_results
  28. {
  29. regex="$1"
  30. dest="$2"
  31. while read line; do
  32. match="`echo "$line" | egrep "$regex"`"
  33. echo RX: $line >> $RAW_DROPFILE
  34. echo MATCH: $match >> $RAW_DROPFILE
  35. if [ -n "$match" ]; then
  36. echo DONE. >> $RAW_DROPFILE
  37. echo "$match" >> $dest
  38. exit 0;
  39. fi
  40. done
  41. }
  42. function query_modem
  43. {
  44. query_command="$1"
  45. reply_regex="$2"
  46. if [ -z "$query_command" ]; then
  47. query_command="AT"
  48. fi
  49. if [ -z "$reply_regex" ]; then
  50. reply_regex='.*'
  51. fi
  52. dest_name="/tmp/$$.`date +%s`.tmp"
  53. echo -n > $dest_name
  54. (collect_modem_results "$reply_regex" $dest_name < $SERIAL_PORT) 2>&1 > /dev/null &
  55. serial_coprocess="$!"
  56. sleep 2
  57. echo TX: $query_command >> $RAW_DROPFILE
  58. echo -e "$query_command\r" > $SERIAL_PORT
  59. (sleep $REPLY_TIMEOUT; echo TIMEOUT >> $RAW_DROPFILE; kill $serial_coprocess) 2>&1 > /dev/null &
  60. watchdog_coprocess="$!"
  61. wait $serial_coprocess 2>&1 > /dev/null
  62. echo "`cat $dest_name`"
  63. rm $dest_name
  64. kill $watchdog_coprocess 2> /dev/null
  65. }
  66. function query_mac_address
  67. {
  68. iface=$1
  69. if [ -z "$iface" ]; then
  70. iface="eth0"
  71. fi
  72. #/sbin/ifconfig $iface | head -n 1 | egrep '([0-9a-fA-F][0-9a-fA-F]:?)+' | tr -s ' ' | cut -d' ' -f5
  73. /sbin/ifconfig $iface | grep -P '^ *ether ' | head -n 1 | egrep -o '([0-9a-fA-F][0-9a-fA-F]:?)+' | head -n1
  74. }
  75. function setup_modem
  76. {
  77. stty -F $SERIAL_PORT $PORT_CONFIG
  78. query_modem 'AT' 2>&1 > /dev/null
  79. sleep 2
  80. echo -n "+++" > $SERIAL_PORT
  81. sleep 2
  82. query_modem 'ATZ' 'OK' 2>&1 > /dev/null
  83. }
  84. setup_modem
  85. query_modem 'AT+CMEE=1' 'OK' 2>&1 > /dev/null
  86. maxtries=5
  87. tries=0
  88. while !(echo $imei | egrep '[0-9]+') && [ $tries -lt $maxtries ]; do
  89. imei="`query_modem 'AT+CGSN' '[0-9]+|.*ERROR.*' 2> /dev/null`"
  90. tries=$((tries + 1))
  91. done
  92. tries=0
  93. while !(echo $imsi | egrep '[0-9]+') && [ $tries -lt $maxtries ]; do
  94. imsi="`query_modem 'AT+CIMI' '[0-9]+|.*ERROR.*' 2> /dev/null`"
  95. tries=$((tries + 1))
  96. done
  97. echo "IMEI = $imei" > $COOKED_DROPFILE
  98. echo "IMSI = $imsi" >> $COOKED_DROPFILE
  99. echo "ETH0 = `query_mac_address eth0 2> /dev/null`" >> $COOKED_DROPFILE