get_net_ids.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 [[ ! -e $SERIAL_PORT ]] ; then return ; fi
  47. if [ -z "$query_command" ]; then
  48. query_command="AT"
  49. fi
  50. if [ -z "$reply_regex" ]; then
  51. reply_regex='.*'
  52. fi
  53. dest_name="/tmp/$$.`date +%s`.tmp"
  54. echo -n > $dest_name
  55. (collect_modem_results "$reply_regex" $dest_name < $SERIAL_PORT) 2>&1 > /dev/null &
  56. serial_coprocess="$!"
  57. sleep 2
  58. echo TX: $query_command >> $RAW_DROPFILE
  59. echo -e "$query_command\r" > $SERIAL_PORT
  60. (sleep $REPLY_TIMEOUT; echo TIMEOUT >> $RAW_DROPFILE; kill $serial_coprocess) 2>&1 > /dev/null &
  61. watchdog_coprocess="$!"
  62. wait $serial_coprocess 2>&1 > /dev/null
  63. echo "`cat $dest_name`"
  64. rm $dest_name
  65. kill $watchdog_coprocess 2> /dev/null
  66. }
  67. function query_mac_address
  68. {
  69. iface=$1
  70. if [ -z "$iface" ]; then
  71. iface="eth0"
  72. fi
  73. #/sbin/ifconfig $iface | head -n 1 | egrep '([0-9a-fA-F][0-9a-fA-F]:?)+' | tr -s ' ' | cut -d' ' -f5
  74. /sbin/ifconfig $iface | grep -P '^ *ether ' | head -n 1 | egrep -o '([0-9a-fA-F][0-9a-fA-F]:?)+' | head -n1
  75. }
  76. function setup_modem
  77. {
  78. if [[ ! -e $SERIAL_PORT ]] ; then return ; fi
  79. stty -F $SERIAL_PORT $PORT_CONFIG
  80. query_modem 'AT' 2>&1 > /dev/null
  81. sleep 2
  82. #echo -n "+++" > $SERIAL_PORT
  83. #sleep 2
  84. query_modem 'ATZ' 'OK' 2>&1 > /dev/null
  85. }
  86. maxtries=5
  87. tries=0
  88. while [[ ! -e $SERIAL_PORT && $tries -lt $maxtries ]] ; do
  89. echo "# $tries / $maxtries"
  90. sleep 5
  91. tries=$((tries + 1))
  92. done
  93. if [[ ! -e $SERIAL_PORT ]] ; then
  94. echo "IMEI = " > $COOKED_DROPFILE
  95. echo "IMSI = " >> $COOKED_DROPFILE
  96. echo "ETH0 = `query_mac_address eth0 2> /dev/null`" >> $COOKED_DROPFILE
  97. exit
  98. fi
  99. setup_modem
  100. query_modem 'AT+CMEE=1' 'OK' 2>&1 > /dev/null
  101. tries=0
  102. while !(echo $imei | egrep '[0-9]+') && [ $tries -lt $maxtries ]; do
  103. imei="`query_modem 'AT+CGSN' '[0-9]+|.*ERROR.*' 2> /dev/null`"
  104. tries=$((tries + 1))
  105. done
  106. tries=0
  107. while !(echo $imsi | egrep '[0-9]+') && [ $tries -lt $maxtries ]; do
  108. imsi="`query_modem 'AT+CIMI' '[0-9]+|.*ERROR.*' 2> /dev/null`"
  109. tries=$((tries + 1))
  110. done
  111. echo "IMEI = $imei" > $COOKED_DROPFILE
  112. echo "IMSI = $imsi" >> $COOKED_DROPFILE
  113. echo "ETH0 = `query_mac_address eth0 2> /dev/null`" >> $COOKED_DROPFILE
  114. echo "PPP0 = `query_mac_address ppp0 2> /dev/null`" >> $COOKED_DROPFILE