· 8 years ago · Feb 19, 2018, 10:36 AM
1#!/usr/bin/python2.5 -u
2# -*- coding: utf-8 -*-
3
4/*
5
6SET FOREIGN_KEY_CHECKS=0;
7-- ----------------------------
8-- Table structure for `ip_access`
9-- ----------------------------
10DROP TABLE IF EXISTS `ip_access`;
11CREATE TABLE `ip_access` (
12 `ip` varchar(20) NOT NULL,
13 `access` tinyint(3) NOT NULL,
14 PRIMARY KEY (`ip`)
15) ENGINE=InnoDB DEFAULT CHARSET=utf8;
16
17-- ----------------------------
18-- Records of ip_access
19-- ----------------------------
20INSERT INTO `ip_access` VALUES ('193.111.2.19', '1');
21
22*/
23
24import sys
25import MySQLdb
26
27def log(s):
28 sys.stderr.write(s)
29
30def main(arg):
31 db=MySQLdb.connect(user='squid', passwd='squid', db='squid', host='10.0.10.100')
32 c=db.cursor()
33
34 while 1:
35 l = sys.stdin.readline().split()
36
37 try:
38 login = l[0]
39 except:
40 continue
41
42 c.execute('SELECT access from ip_access where ip=%s', [ip])
43 r = c.fetchone()
44 if r is None:
45 permit = 0 # по умолчанию доÑтуп разрешён
46 if r == 1:
47 permit = 1
48
49 log('IP = %s, access = %s' % (ip,permit))
50 if permit:
51 sys.stdout.write('OK\n')
52 else:
53 sys.stdout.write('ERR\n')
54
55 c.close()
56
57if __name__ == '__main__':
58 sys.exit(main(arg=sys.argv[1:]))