返回列表 發帖

Chan_Dongle 收SMS亂碼

有兩種情況, 有一種直情整死個process, quit左, 連有SMS都唔知.
第二種係出怪碼, SMS forward去email會變成一個垃圾 noname attachment. 會知道收左個SMS但係內容唔知.
有冇解決方法呢?

用既係華為 E180.

發現同時入兩個sms的話就會亂.. 所以再引入多一個message reference 既variable試fix..
但唔post出來啦... 太亂... lol.

TOP

本帖最後由 mcdull 於 2017-10-5 18:37 編輯

Hit a major issue in python.
when sys.exit after calling webdriver.PhantomJS(), it will not clean up the phantomJS child process.   If I add driver.quit(), the phantomJS will not be closed either and the dialplan will not continue.

After a few tens of SMS, the server will run out of memory.  No fix yet.  Damn this simple things coz hell of troubles.

temp fix by issuing SIGKILL

TOP

本帖最後由 mcdull 於 2017-9-27 22:41 編輯

Some stupid copy&paste issue in the code above..... dont use....

fixed with code below.. it should now be properly formatted.
  1. [from-pstn-custom]
  2. include => dongle-incoming-sms
  3. include => dongle-incoming-ussd

  4. [dongle-incoming-sms]
  5. exten => sms,1,Noop(Incoming SMS from ${CALLERID(num)} ${BASE64_DECODE(${SMS_BASE64})})
  6. exten => sms,3,System(echo '${STRFTIME(${EPOCH},Hongkong,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME} - ${CALLERID(num)} - ${CALLERID(dnid)}: ${BASE64_DECODE(${SMS_BASE64})}  /////  ${CMGR}' >> /var/log/asterisk/sms${DONGLENAME}.txt)
  7. exten => sms,4,System(echo "INSERT INTO sms (DONGLENAME,CALLERID,EPOCH,SMS_BASE64,DONGLENUMBER,CMGR) values ('${DONGLENAME}','${CALLERID(num)}','${STRFTIME(${EPOCH},Hongkong,%Y-%m-%d %H:%M:%S)}','${BASE64_DECODE(${SMS_BASE64})}','${DONGLENUMBER}','${CMGR}')\;" > /var/log/asterisk/smssql${DONGLENAME}.txt)
  8. exten => sms,5,System(mysql -uxxx -pxxx -hxxx.xxx.xxx.xxx -P3307 smslog < /var/log/asterisk/smssql${DONGLENAME}.txt)
  9. exten => sms,6,Set(channelID=${CHANNEL:-10:8})
  10. exten => sms,7,System(echo '${CMGR}' > /var/log/asterisk/smsCMGRraw${DONGLENAME}_${channelID})
  11. exten => sms,8,System(awk '{if ( NR == "2" ) print}' /var/log/asterisk/smsCMGRraw${DONGLENAME}_${channelID} | tee -a /var/log/asterisk/smsPDUraw${DONGLENAME}_${channelID})
  12. exten => sms,9,NoOp(cmdstr = /usr/bin/python /etc/asterisk/pdudecode.py $(cat /var/log/asterisk/smsPDUraw${DONGLENAME}_${channelID})))
  13. exten => sms,n,Set(completeSMS=${SHELL(/usr/bin/python /etc/asterisk/pdudecode.py $(cat /var/log/asterisk/smsPDUraw${DONGLENAME}_${channelID}))})
  14. exten => sms,n,NoOp(result is ${completeSMS})
  15. exten => sms,n,Set(completeSMS=${completeSMS:0:3})
  16. exten => sms,n,Set(smsTotal=${completeSMS:-1:1})
  17. exten => sms,n,Set(smsPart=${completeSMS:0:1})
  18. exten => sms,NoOp(Total ${smsTotal}  Part ${smsPart})
  19. exten => sms,n,System(echo '${BASE64_DECODE(${SMS_BASE64})}' > /var/log/asterisk/smscontent${DONGLENAME}_${smsPart})
  20. exten => sms,n,System(rm -f /var/log/asterisk/smsPDUraw${DONGLENAME}_${channelID})
  21. exten => sms,n,System(rm -f /var/log/asterisk/smsCMGRraw${DONGLENAME}_${channelID})
  22. exten => sms,n,ExecIf($[ ${smsTotal}!=${smsPart} ]?Hangup())
  23. ;exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?wait(1))
  24. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo 'Subject: SMS received from ${CALLERID(num)}' > /var/log/asterisk/smsmail${DONGLENAME})
  25. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo ' ' >> /var/log/asterisk/smsmail${DONGLENAME})
  26. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo '${STRFTIME(${EPOCH},Hongkong,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME}' >> /var/log/asterisk/smsmail${DONGLENAME})
  27. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo ' ' >> /var/log/asterisk/smsmail${DONGLENAME})
  28. exten => sms,n,Set(i=1)
  29. exten => sms,n,Set(errorCount=0)
  30. exten => sms,n,While($[${i} <= ${smsTotal}])
  31. exten => sms,n,NoOp(Loop ${i})
  32. exten => sms,n,Set(fileExist=0)
  33. exten => sms,n,ExecIf($[ ${SHELL(test -f /var/log/asterisk/smscontent${DONGLENAME}_${i} && echo -n 1 || echo -n 0)}=1 ]?Set(fileExist=1))
  34. exten => sms,n,ExecIf($[ ${fileExist}=1 ]?System(head -c -1 -q '/var/log/asterisk/smscontent${DONGLENAME}_${i}' >> /var/log/asterisk/smsmail${DONGLENAME})
  35. exten => sms,n,ExecIf($[ ${fileExist}=1 ]?Set(i=$[${i} + 1]))
  36. exten => sms,n,ExecIf($[ ${fileExist}=0 ]?wait(2))
  37. exten => sms,n,ExecIf($[ ${fileExist}=0 ]?Set(errorCount=$[${errorCount} + 1]))
  38. exten => sms,n,ExecIf($[ ${errorCount} > 5 ]?Set(i=$[${smsTotal} + 1]))
  39. exten => sms,n,EndWhile
  40. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(cat '/var/log/asterisk/smsmail${DONGLENAME}' | tr -d '\r' | mailx -s 'SMS Recevied from ${CALLERID(num)} by ${DONGLENUMBER} ' -r 'xxx@xxx.com' -S smtp='xxx.xxx.xxx.xxx' xxx@xxx.com)
  41. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(rm -f /var/log/asterisk/smscontent${DONGLENAME})
  42. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo 'I am DONE' >> /var/log/asterisk/test${DONGLENAME})
  43. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo '${STRFTIME(${EPOCH},Hongkong,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME} - ${completeSMS}' >> /var/log/asterisk/test${DONGLENAME})
  44. exten => sms,n,System(asterisk -rx 'dongle cmd ${DONGLENAME} AT+CMGD=1,4')
  45. exten => sms,n,Hangup()


  46. [dongle-incoming-ussd]
  47. exten => ussd,1,Noop(Incoming USSD: ${BASE64_DECODE(${USSD_BASE64})})
  48. exten => ussd,n,System(echo '${STRFTIME(${EPOCH},Hongkong,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME}: ${BASE64_DECODE(${USSD_BASE64})}' > /var/log/asterisk/ussd${DONGLENAME}.txt)
  49. exten => ussd,n,System(cat '/var/log/asterisk/ussd${DONGLENAME}.txt' | tr -d '\r' | mailx -s 'USSD Recevied from ${CALLERID(num)} by ${DONGLENUMBER}' -r 'xxx@xxx.xxx' -S smtp='xxx.xxx.xxx.xxx' xxx@xxx.com)
  50. exten => ussd,n,Hangup()
複製代碼

TOP

本帖最後由 mcdull 於 2017-9-27 18:37 編輯
  1. [from-pstn-custom]
  2. include => dongle-incoming-sms
  3. include => dongle-incoming-ussd

  4. [dongle-incoming-sms]
  5. exten => sms,1,Noop(Incoming SMS from ${CALLERID(num)} ${BASE64_DECODE(${SMS_BASE64})})
  6. exten => sms,3,System(echo '${STRFTIME(${EPOCH},Hongkong,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME} - ${CALLERID(num)} - ${CALLERID(dnid)}: ${BASE64_DECODE(${SMS_BASE64})}  /////  ${CMGR}' >> /var/log/asterisk/sms${DONGLENAME}.txt)
  7. exten => sms,4,System(echo "INSERT INTO sms (DONGLENAME,CALLERID,EPOCH,SMS_BASE64,DONGLENUMBER,CMGR) values ('${DONGLENAME}','${CALLERID(num)}','${STRFTIME(${EPOCH},Hongkong,%Y-%m-%d %H:%M:%S)}','${BASE64_DECODE(${SMS_BASE64})}','${DONGLENUMBER}','${CMGR}')\;" > /var/log/asterisk/smssql${DONGLENAME}.txt)
  8. exten => sms,5,System(mysql -uxxxxx -pxxxxx -hxxx.xxx.xxx.xxx -P3307 smslog < /var/log/asterisk/smssql${DONGLENAME}.txt)
  9. exten => sms,6,Set(channelID=${CHANNEL:-10:8})
  10. exten => sms,7,System(echo '${CMGR}' > /var/log/asterisk/smsCMGRraw${DONGLENAME}_${channelID})
  11. exten => sms,8,System(awk '{if ( NR == "2" ) print}' /var/log/asterisk/smsCMGRraw${DONGLENAME}_${channelID} | tee -a /var/log/asterisk/smsPDUraw${DONGLENAME}_${channelID})
  12. exten => sms,9,NoOp(cmdstr = /usr/bin/python /etc/asterisk/pdudecode.py $(cat /var/log/asterisk/smsPDUraw${DONGLENAME}_${channelID})))
  13. exten => sms,n,Set(completeSMS=${SHELL(/usr/bin/python /etc/asterisk/pdudecode.py $(cat /var/log/asterisk/smsPDUraw${DONGLENAME}_${channelID}))})
  14. exten => sms,n,NoOp(result is ${completeSMS})
  15. exten => sms,n,Set(completeSMS=${completeSMS:0:3})
  16. exten => sms,n,Set(smsTotal=${completeSMS:-1:1})
  17. exten => sms,n,Set(smsPart=${completeSMS:0:1})
  18. exten => sms,NoOp(Total ${smsTotal}  Part ${smsPart})
  19. exten => sms,n,System(echo '${BASE64_DECODE(${SMS_BASE64})}' > /var/log/asterisk/smscontent${DONGLENAME}_${smsPart})
  20. exten => sms,n,System(rm -f /var/log/asterisk/smsPDUraw${DONGLENAME}_${channelID})
  21. exten => sms,n,System(rm -f /var/log/asterisk/smsCMGRraw${DONGLENAME}_${channelID})
  22. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?wait(1))
  23. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo 'Subject: SMS received from ${CALLERID(num)}' > /var/log/asterisk/smsmail${DONGLENAME})
  24. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo ' ' >> /var/log/asterisk/smsmail${DONGLENAME})
  25. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo '${STRFTIME(${EPOCH},Hongkong,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME}' >> /var/log/asterisk/smsmail${DONGLENAME})
  26. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo ' ' >> /var/log/asterisk/smsmail${DONGLENAME})
  27. exten => sms,n,Set(i=1)
  28. exten => sms,n,While($[${i} <= ${smsTotal}])
  29. exten => sms,n,NoOp(Loop ${i})
  30. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(cat '/var/log/asterisk/smscontent${DONGLENAME}_${i}' >> /var/log/asterisk/smsmail${DONGLENAME})
  31. exten => sms,n,ExecIf($[ ${SHELL(test -f /etc/asterisk/asterisk.conf && echo -n 1 || echo -n 0)}=1 ]?Set(i=$[${i} + 1]))
  32. exten => sms,n,ExecIf($[ ${SHELL(test -f /etc/asterisk/asterisk.conf && echo -n 1 || echo -n 0)}=0 ]?wait(2))
  33. exten => sms,n,EndWhile
  34. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(cat '/var/log/asterisk/smsmail${DONGLENAME}' | tr -d '\r' | mailx -s 'SMS Recevied from ${CALLERID(num)} by ${DONGLENUMBER} ' -r 'xxx@xxx.com' -S smtp='xxx.xxx.xxx.xxx' xxx@xxx.com)
  35. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(rm -f /var/log/asterisk/smscontent${DONGLENAME})
  36. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo 'I am DONE' >> /var/log/asterisk/test${DONGLENAME})
  37. exten => sms,n,ExecIf($[ ${smsTotal}=${smsPart} ]?System(echo '${STRFTIME(${EPOCH},Hongkong,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME} - ${completeSMS}' >> /var/log/asterisk/test${DONGLENAME})
  38. exten => sms,n,System(asterisk -rx 'dongle cmd ${DONGLENAME} AT+CMGD=1,4')
  39. exten => sms,n,Hangup()


  40. [dongle-incoming-ussd]
  41. exten => ussd,1,Noop(Incoming USSD: ${BASE64_DECODE(${USSD_BASE64})})
  42. exten => ussd,n,System(echo '${STRFTIME(${EPOCH},Hongkong,%Y-%m-%d %H:%M:%S)} - ${DONGLENAME}: ${BASE64_DECODE(${USSD_BASE64})}' > /var/log/asterisk/ussd${DONGLENAME}.txt)
  43. exten => ussd,n,System(cat '/var/log/asterisk/ussd${DONGLENAME}.txt' | tr -d '\r' | mailx -s 'USSD Recevied from ${CALLERID(num)} by ${DONGLENUMBER}' -r 'xxx@xxx.xxx' -S smtp='xxx.xxx.xxx.xxx' xxx@xxx.com)
  44. exten => ussd,n,Hangup()
複製代碼

TOP

本帖最後由 mcdull 於 2017-10-5 18:36 編輯

I have revised my work to potentially fix the problem.
  1. #!/usr/bin/env python
  2. #coding:utf-8
  3. import sys
  4. import signal

  5. from selenium import webdriver
  6. from bs4 import BeautifulSoup

  7. driver = webdriver.PhantomJS()
  8. pdu = str(sys.argv[1])
  9. concatpart = '1'
  10. concattotal = '1'
  11. url = 'http://192.168.100.253/pdudecode/jsPduDecoder-master/index.html?pdu='
  12. driver.get(url+pdu)
  13. soup = BeautifulSoup(driver.page_source, 'html.parser')
  14. udhs = soup.findAll('td', text = 'User Data Header')
  15. for i in udhs:
  16. #    print i.get_text()
  17.     if i.next_sibling.get_text().find('Concatenated message') != -1:
  18.         s = i.next_sibling.get_text()
  19. #        print s
  20.         concatpart = s[s.find("part")+5:s.find("part")+6]
  21.         concattotal = s[s.find("parts")-2:s.find("parts")-1]
  22. #        print concatpart
  23. #        print concattotal
  24.         if concatpart == concattotal:
  25.             print concatpart + ',' + concattotal + ' Message Completed'
  26.             driver.service.process.send_signal(signal.SIGKILL)
  27.             driver.quit()
  28.             sys.exit()
  29.         else:
  30.             print concatpart + ',' + concattotal + ' Message Incomplete. ' + s
  31.             driver.service.process.send_signal(signal.SIGKILL)
  32.             driver.quit()
  33.             sys.exit()
  34. print '1,1 Single Message'
  35. driver.service.process.send_signal(signal.SIGKILL)
  36. driver.quit()
  37. sys.exit()
複製代碼

TOP

This is the bug results....

券(可兑换等额 “沃尔玛、京东、天猫、唯品会、百果园”等30多家电子消费券),充值本金分5/5/10/10个月到账。办理步骤:1、微信
关注“广东联通”公众号—服务—话费充值相应金额;2、请按短信提示拨打10010135按3登记参与活动,电子券于3个工作日内下发;3、通
【红包任性派、惊喜享不停】即日起至10月8日期间,单笔充值100/200/300/500元送50/100/150/250元“红包”电子
过“广东联通”微信公众号—发现—电子券红包,查询并兑换电子券。多充多送,叠加生效,重复参与需重复登记,申请成功后不可取消。【深圳联通】

TOP

I am now still facing some issues.
If different message part come together, the python may finish in different time in reserve order and the sms will be displayed and sent in wrong order, and may receive multiple email or incomplete message email.

TOP

wow... you are amazing.

TOP

These should work with multiple dongles.  I have 5 installed currently but it is not stable.  I guess the hub cannot power 5 dongles so I removed one and left 4.

TOP

  1. #!/usr/bin/env python
  2. #coding:utf-8
  3. import sys
  4. import unittest2
  5. from selenium import webdriver
  6. from bs4 import BeautifulSoup

  7. driver = webdriver.PhantomJS()
  8. pdu = str(sys.argv[1])
  9. concatpart = '1'
  10. concattotal = '1'
  11. url = 'http://xxx.xxx.xxx.xxx/jsPduDecoder-master/index.html?pdu='
  12. driver.get(url+pdu)
  13. soup = BeautifulSoup(driver.page_source, 'html.parser')
  14. udhs = soup.findAll('td', text = 'User Data Header')
  15. for i in udhs:
  16.     if i.next_sibling.get_text().find('Concatenated message') != -1:
  17.         s = i.next_sibling.get_text()
  18.         concatpart = s[s.find("part")+5:s.find("part")+6]
  19.         concattotal = s[s.find("parts")-2:s.find("parts")-1]
  20.         if concatpart == concattotal:
  21.             print '0 Message Completed'
  22.             sys.exit(0)
  23.         else:
  24.             print '1 Message Incomplete. ' + s
  25.             sys.exit(1)
  26. print '0 Single Message'
  27. sys.exit(0)
複製代碼
I will use only the first return character as specified in the dialplan rule.

TOP

That was the tricky part.  I initially want to decode the pdu just for the concat msg information.  And it was found too complicated and I had to decode much variation of UDH. A lot of tools were available online only. And local library will NOT decode concat message correctly.  So I have to make reference to http://smspdu.benjaminerhart.com/
I have much hesitation to post my sms online as it may contain verification from online banking, so I pulled the javascript (source provided in the site).  With the same reason, I given up to port the javascript to python, so I have to set up a webserver (luckily I already got my own) and use beautifulsoup to capture the output for return.

TOP

How to check if that's a single SMS or last part of the concat msg?

TOP

What is the logic behind in your python to check if all SMS received?
ttmuskie 發表於 2017-9-26 19:51



the python will return 0 if the SMS is single or the last part of the concat message.
and 1 if the SMS is incomplete.. yet.

Originally I intended to use exit code but I cannot capture the return result properly. So I changed it to use the print out.

TOP

  1. exten => sms,10,Set(completeSMS=${SHELL(/usr/bin/python /etc/asterisk/pdudecode.py $(cat /var/log/asterisk/smsPDUraw${DONGLENAME}_${channelID}))})
複製代碼
What is the logic behind in your python to check if all SMS received?

TOP

返回列表