Commit ecf96ebf authored by Ad Schellevis's avatar Ad Schellevis

(captiveportal, new) work in progress script base

parent e6c1bf49
#!/usr/local/bin/python2.7
"""
Copyright (c) 2015 Deciso B.V. - Ad Schellevis
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------------
disconnect client
"""
import sys
import ujson
from lib.db import DB
from lib.arp import ARP
from lib.ipfw import IPFW
# parse input parameters
parameters = {'sessionid': None, 'zoneid': None, 'output_type':'plain'}
current_param = None
for param in sys.argv[1:]:
if len(param) > 1 and param[0] == '/':
current_param = param[1:].lower()
elif current_param is not None:
if current_param in parameters:
parameters[current_param] = param.strip()
current_param = None
# disconnect client
response = {'terminateCause': 'UNKNOWN'}
if parameters['sessionid'] is not None and parameters['zoneid'] is not None:
cp_db = DB()
# remove client
client_session_info = cp_db.del_client(parameters['zoneid'], parameters['sessionid'])
if client_session_info is not None:
cpIPFW = IPFW()
cpIPFW.delete_from_table(parameters['zoneid'], client_session_info['ip_address'])
client_session_info['terminateCause'] = 'User-Request'
response = client_session_info
# output result as plain text or json
if parameters['output_type'] != 'json':
for item in response:
print '%20s %s' % (item, response[item])
else:
print(ujson.dumps(response))
...@@ -103,8 +103,8 @@ class IPFW(object): ...@@ -103,8 +103,8 @@ class IPFW(object):
parts = line.split() parts = line.split()
if len(parts) > 5: if len(parts) > 5:
if 30001 <= int(parts[0]) <= 50000 and parts[4] == 'count': if 30001 <= int(parts[0]) <= 50000 and parts[4] == 'count':
in_pkts = int(parts[1]) line_pkts = int(parts[1])
out_pkts = int(parts[2]) line_bytes = int(parts[2])
last_accessed = int(parts[3]) last_accessed = int(parts[3])
if parts[7] != 'any': if parts[7] != 'any':
ip_address = parts[7] ip_address = parts[7]
...@@ -113,15 +113,23 @@ class IPFW(object): ...@@ -113,15 +113,23 @@ class IPFW(object):
if ip_address not in result: if ip_address not in result:
result[ip_address] = {'rule': int(parts[0]), result[ip_address] = {'rule': int(parts[0]),
'last_accessed': last_accessed, 'last_accessed': 0,
'in_pkts': in_pkts, 'in_pkts' : 0,
'out_pkts': out_pkts 'in_bytes' : 0,
'out_pkts' : 0,
'out_bytes' : 0
} }
result[ip_address]['last_accessed'] = max(result[ip_address]['last_accessed'],
last_accessed)
if parts[7] != 'any':
# count input
result[ip_address]['in_pkts'] = line_pkts
result[ip_address]['in_bytes'] = line_bytes
else: else:
result[ip_address]['in_pkts'] += in_pkts # count output
result[ip_address]['out_pkts'] += out_pkts result[ip_address]['out_pkts'] = line_pkts
result[ip_address]['last_accessed'] = max(result[ip_address]['last_accessed'], result[ip_address]['out_bytes'] = line_bytes
last_accessed)
return result return result
def add_accounting(self, address): def add_accounting(self, address):
......
...@@ -51,13 +51,15 @@ else: ...@@ -51,13 +51,15 @@ else:
# output result as plain text or json # output result as plain text or json
if parameters['output_type'] != 'json': if parameters['output_type'] != 'json':
heading = {'sessionid': 'sessionid', heading = {'sessionId': 'sessionid',
'username': 'username', 'userName': 'username',
'ip_address': 'ip_address', 'ipAddress': 'ip_address',
'mac_address': 'mac_address' 'macAddress': 'mac_address',
'total_bytes': 'total_bytes'
} }
print '%(sessionid)-30s %(username)-20s %(ip_address)-20s %(mac_address)-20s' % heading print '%(sessionId)-30s %(userName)-20s %(ipAddress)-20s %(macAddress)-20s %(total_bytes)-20s' % heading
for item in response: for item in response:
print '%(sessionid)-30s %(username)-20s %(ip_address)-20s %(mac_address)-20s' % item item['total_bytes'] = (item['bytes_out'] + item['bytes_in'])
print '%(sessionId)-30s %(userName)-20s %(ipAddress)-20s %(macAddress)-20s %(total_bytes)-20s' % item
else: else:
print(ujson.dumps(response)) print(ujson.dumps(response))
...@@ -10,6 +10,7 @@ create table cp_clients ( ...@@ -10,6 +10,7 @@ create table cp_clients (
, ip_address varchar , ip_address varchar
, mac_address varchar , mac_address varchar
, created number , created number
, deleted integer default (0)
, primary key (zoneid, sessionid) , primary key (zoneid, sessionid)
); );
...@@ -20,6 +21,14 @@ create index cp_clients_zone ON cp_clients (zoneid); ...@@ -20,6 +21,14 @@ create index cp_clients_zone ON cp_clients (zoneid);
create table session_info ( create table session_info (
zoneid int zoneid int
, sessionid varchar , sessionid varchar
, prev_packets_in integer
, prev_bytes_in integer
, prev_packets_out integer
, prev_bytes_out integer
, packets_in integer default (0)
, packets_out integer default (0)
, bytes_in integer default (0)
, bytes_out integer default (0)
, last_accessed integer
, primary key (zoneid, sessionid) , primary key (zoneid, sessionid)
); );
#!/usr/local/bin/python2.7
"""
Copyright (c) 2015 Deciso B.V. - Ad Schellevis
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------------
update captive portal statistics
"""
import sys
import ujson
from lib.db import DB
from lib.arp import ARP
from lib.ipfw import IPFW
db = DB()
cur = db._connection.cursor();
# update accounting
ipfw = IPFW()
#print ipfw.list_accounting_info()
db.update_accounting_info(ipfw.list_accounting_info())
# tmp = """
# create table session_info (
# zoneid int
# , sessionid varchar
# , prev_packets_in integer
# , prev_bytes_in integer
# , prev_packets_out integer
# , prev_bytes_out integer
# , packets_in integer default (0)
# , packets_out integer default (0)
# , bytes_in integer default (0)
# , bytes_out integer default (0)
# , last_accessed integer
# , primary key (zoneid, sessionid)
# );
# """
# cur.execute("drop table session_info");
# cur.execute(tmp);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment