sql使用mysql models来完成

pull/2/head
ibuler 2014-11-02 13:36:14 +08:00
parent 765ac5e714
commit e38fb5992f
2 changed files with 21 additions and 31 deletions

View File

@ -22,7 +22,7 @@ cur_dir = os.path.dirname(__file__)
sys.path.append('%s/webroot/AutoSa/' % cur_dir) sys.path.append('%s/webroot/AutoSa/' % cur_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'AutoSa.settings' os.environ['DJANGO_SETTINGS_MODULE'] = 'AutoSa.settings'
from UserManage.models import User,Logs from UserManage.models import User, Logs, Assets
cf = ConfigParser.ConfigParser() cf = ConfigParser.ConfigParser()
@ -118,16 +118,16 @@ def connect(host, port, user, password):
log_date_dir = '%s/%s' % (log_dir, time.strftime('%Y%m%d')) log_date_dir = '%s/%s' % (log_dir, time.strftime('%Y%m%d'))
if not os.path.isdir(log_date_dir): if not os.path.isdir(log_date_dir):
os.mkdir(log_date_dir) os.mkdir(log_date_dir)
structtime_now = time.localtime() structtime_start = time.localtime()
datetime_now = time.strftime('%Y%m%d%H%M%S', structtime_now) datetime_start = time.strftime('%Y%m%d%H%M%S', structtime_start)
logtime_now = time.strftime('%Y/%m/%d %H:%M:%S', structtime_now) logtime_start = time.strftime('%Y/%m/%d %H:%M:%S', structtime_start)
timestamp_now = int(time.mktime(structtime_now)) timestamp_start = int(time.mktime(structtime_start))
logfile_name = "%s/%s_%s_%s" % (log_date_dir, host, user, datetime_now) logfile_name = "%s/%s_%s_%s" % (log_date_dir, host, user, datetime_start)
logfile = open(logfile_name, 'a') logfile = open(logfile_name, 'a')
log = Logs(user=user, host=host, logfile=logfile_name, start_time=timestamp_now) log = Logs(user=user, host=host, logfile=logfile_name, start_time=timestamp_start)
log.save() log.save()
logfile.write('\n%s\n' % logtime_now) logfile.write('\n%s\n' % logtime_start)
try: try:
global foo global foo
foo = pxssh.pxssh() foo = pxssh.pxssh()
@ -136,6 +136,7 @@ def connect(host, port, user, password):
foo.sendline('') foo.sendline('')
signal.signal(signal.SIGWINCH, sigwinch_passthrough) signal.signal(signal.SIGWINCH, sigwinch_passthrough)
foo.interact(escape_character=chr(28)) foo.interact(escape_character=chr(28))
logfile.write('\n%s' % time.strftime('%Y/%m/%d %H:%M:%S'))
log.finish = 1 log.finish = 1
log.end_time = int(time.time()) log.end_time = int(time.time())
log.save() log.save()
@ -154,36 +155,25 @@ def ip_all_select(username):
"""select all the server of the user can control.""" """select all the server of the user can control."""
ip_all = [] ip_all = []
ip_all_dict = {} ip_all_dict = {}
db, cursor = connect_db(db_user, db_password, db_db, db_host, db_port) user = User.objects.get(username=username)
cursor.execute('select t2.ip, t2.comment from %s t1, %s t2, %s t3 where t1.username="%s" and t1.id=t3.uid_id and t2.id = t3.aid_id;' % all_assets_user = user.assetsuser_set.all()
(user_table, assets_table, assets_user_table, username)) for assets_user in all_assets_user:
ip_all_record = cursor.fetchall() ip_all.append(assets_user.aid.ip)
if ip_all_record: ip_all_dict[assets_user.aid.ip] = assets_user.aid.comment
for record in ip_all_record:
ip_all.append(record[0])
ip_all_dict[record[0]] = record[1]
db.close()
return ip_all, ip_all_dict return ip_all, ip_all_dict
def sth_select(username='', ip=''): def sth_select(username='', ip=''):
"""if username: return password elif ip return port""" """if username: return password elif ip return port"""
db, cursor = connect_db(db_user, db_password, db_db, db_host, db_port)
if username: if username:
cursor.execute('select ldap_password from %s where username="%s"' % (user_table, username)) user = User.objects.get(username=username)
try: password = user.password
password = cursor.fetchone()[0]
except IndexError:
password = ''
db.close()
return password return password
if ip: if ip:
cursor.execute('select port from %s where ip="%s"' % (assets_table, ip)) asset = Assets.objects.get(ip=ip)
try: port = asset.port
port = int(cursor.fetchone()[0])
except IndexError:
port = 22
db.close()
return port return port
return None return None

View File

@ -29,7 +29,7 @@ class Logs(models.Model):
logfile = models.CharField(max_length=1000) logfile = models.CharField(max_length=1000)
finish = models.SmallIntegerField(max_length=4, default=0) finish = models.SmallIntegerField(max_length=4, default=0)
start_time = models.IntegerField() start_time = models.IntegerField()
end_time = models.IntegerField() end_time = models.IntegerField(default=0)
def __unicode__(self): def __unicode__(self):
return self.logfile return self.logfile