-
Notifications
You must be signed in to change notification settings - Fork 2
/
spong.py
112 lines (97 loc) · 3.17 KB
/
spong.py
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
#!/usr/bin/env python
"""
Spong : spy mongoDB connector
"""
import config
import re
import time
import pymongo
from datetime import datetime
import serial
from messaging.sms import SmsSubmit
class Spong(object):
def __init__(self, recipient=config.recipient, message=config.message):
client = pymongo.MongoClient("localhost", 27017)
log = client.log
sent = client.sent
self.logfile = open("modem.log","w")
self.open()
self.recipient = recipient
self.content = message
def open(self):
self.ser = serial.Serial(config.serial, 115200, timeout=config.timeout)
self.ser.flushInput()
self.ser.flushOutput()
self.SendCommand('ATZ\r',8)
self.SendCommand('AT+CMGF=0\r',16)
def rcpt(self, number):
self.recipient = number
def msg(self, message):
self.content = message
def send(self):
self.pdu = SmsSubmit(self.recipient, self.content)
for xpdu in self.pdu.to_pdu():
command = 'AT+CMGS=%d\r' % xpdu.length
self.SendCommand(command,len(str(xpdu.length))+14)
command = '%s\x1a' % xpdu.pdu
self.SendCommand(command,len(xpdu.pdu)+20)
self.logfile.write(str(datetime.now()))
self.logfile.write(' after send a sms \n')
def close(self):
self.ser.close()
def SendCommand(self,command,char,getline=True):
self.logfile.write(str(datetime.now()))
self.logfile.write(' before send command '+str(char)+' \n')
self.ser.write(command)
data = ''
if getline:
data=self.ReadLine(char)
self.logfile.write(str(datetime.now()))
self.logfile.write(' after send command '+str(char)+' \n')
return data
def ReadAll(self):
data = self.ser.readall()
return data
def ReadLine(self,char):
data = self.ser.read(char)
if 'OK' in data:
print ' berhasil '
if 'ERROR' in data:
print ' gagal '
print data+' char:<'+str(char)+'> '
return data
def unreadMsg(self):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGL=0\r\n'
self.SendCommand(command,getline=True)
data = self.ser.readall()
print data
def readMsg(self):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGL=1\r\n'
self.SendCommand(command,getline=True)
data = self.ser.readall()
print data
def allMsg(self):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGL=4\r\n'
self.SendCommand(command,getline=True)
data = self.ser.readall()
print data
def deleteMsg(self, idx):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGD=%s\r\n' % idx
self.SendCommand(command,getline=True)
data = self.ser.readall()
print data
def getMsg(self, idx):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGR=%s\r\n' % idx
self.SendCommand(command,getline=True)
data = self.ser.readall()
print data