/
sms_handling.py
102 lines (87 loc) · 3.66 KB
/
sms_handling.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
#!/usr/bin/python
import ConfigParser
import httplib
from urllib import unquote, quote
import sms_ssh
import sms_tolk
import re
import logging
# Read configuration file
conf = ConfigParser.ConfigParser()
conf.read('config.cfg')
# Function gets called from smsIO
# by the handler when a GET request has been made.
def incoming_sms(command_string, originator_string):
# Split command_string into HOST and COMMAND parts
(command_host, command) = command_string.split('!') if "!" in command_string else ['', command_string]
# Parse command string into actual Cisco IOS commands
complete_cmd_string = sms_tolk.parse(command)
print "HOST: "+command_host, "CMD: "+complete_cmd_string #DEBUGGING
# originator must be in format 0046XXXXXXXXXXX
originator = parse_phone(originator_string) if originator_string != '' else ''
(username, password) = get_credentials(originator)
host_ip = get_host_ip(command_host.strip())
# Send a command to a host via SSH and returns the output
if host_ip and username and password:
try:
recv_host_output = sms_ssh.ssh_connect(host_ip, complete_cmd_string, username, password)
# Logging SSH output
logging.info('SSH Output:%s', str(recv_host_output))
except Exception as e:
recv_host_output = e
# Logging SSH error
logging.error('SSH Error:%s', str(recv_host_output))
else:
recv_host_output = "No host specified (i.e R1, HQ_SW1)"
# Logging user input error
logging.warning('User Input Error:%s', str(recv_host_output))
# Send a reply to the user via the sms gateway
# TODO: Output needs formatting?
if originator:
print "TRYING TO SEND SMS TO ", originator
send_sms(originator, str(recv_host_output), conf.get('smsgateway', 'user'), conf.get('smsgateway', 'pass'))
else:
print "NO ORIGINATOR. NO SMS SENT."
# Print reply to console
#print recv_host_output
# Parse phone number
def parse_phone(phone_number):
# Make sure the number start with exactly two zeroes
return "00" + re.sub('^0*', '', phone_number)
# Get username/password from list of phonenumbers
def get_credentials(phone_number):
user = conf.get('users', phone_number) if phone_number else ''
return user.split(':') if user else [None, None]
# Get host IP from list of hostnames
def get_host_ip(hostname):
host = conf.get('hosts', hostname) if hostname else ''
return host if host else None
# Function for sending SMS data back to the SMS gateway for handling
def send_sms(recv, msg, user, passw):
# Adds the data to the their respective attribute of the HTTP path.
sms_file = '/' + conf.get('smsgateway', 'gateway_file') + '?'
sms_query = []
sms_query.append('username=' + user)
sms_query.append('password=' + passw)
sms_query.append('destination=' + recv)
sms_query.append('type=text')
sms_query.append('charset=' + conf.get('smsgateway', 'charset'))
sms_query.append('allowconcat=6')
sms_query.append('text=' + quote(msg))
# Makes the PATH for the GET message
gateway_request = sms_file + '&'.join(sms_query)
#print gateway_request
# Working HTTP GET, 'gateway_url' is the url from which you want to use GET on. NOTE: Tested against 'https://docs.python.org/2/'
http_connection = httplib.HTTPSConnection(conf.get('smsgateway', 'gateway_url'))
# Calling a function of the http connection object, from which we use GET with the specified path('gateway_request')
http_connection.request('GET', gateway_request)
# Get the response from sms gateway
connection_response = http_connection.getresponse()
if connection_response.status != 200:
print "SMS not sent."
# Logging SMS Error
logging.error('SMS Error:%s', connection_response.msg)
else:
print "SMS sent successfully."
# Logging SMS Success
logging.info('SMS Sent:%s', connection_response.msg)