#!/usr/bin/python # -*- coding: ISO-8859-15 -*- # # Copyright (C) 2005 David Guerizec # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA import getpass import sys import SSHproxy.pwdb import cmd from pprint import pprint as pp import readline pwdb = SSHproxy.pwdb.MySQLPwDB() class CommandLine(object): def __init__(self, args): if type(args) == type(''): self.args = self.decode(args) else: self.args = args def __len__(self): return len(self.args) def __getitem__(self, item): return self.args[item] def decode(self, args): l = [ e.strip() for e in args.split() ] l = [ e for e in l if e ] return l def encode(self, args=None): if not args: args = self.args return ' '.join(args) class DBConsole(cmd.Cmd): def __init__(self): self.current_site = None cmd.Cmd.__init__(self) def do_select_site(self, arg): """select_site [sitename]""" arg = CommandLine(arg) if not len(arg): self.onecmd('help select_site') return self.current_site = arg[0] self.set_prompt() def set_prompt(self): self.prompt = 'site: %s\n> ' % self.current_site def _complete_sites(self, text, line, begidx, endidx): sites = self._sites() w = len(text) l = [] for name, ip, loc in sites: if w > len(name): continue if text == name[:w]: l.append(name+' ') return l def _complete_groups(self, text, line, begidx, endidx): groups = self._groups() w = len(text) l = [] for id, name in groups: if w > len(name): continue if text == name[:w]: l.append(name+' ') return l def complete_list_users(self, text, line, begidx, endidx): return self._complete_sites(text, line, begidx, endidx) def complete_select_site(self, text, line, begidx, endidx): return self._complete_sites(text, line, begidx, endidx) def complete_list_groups(self, text, line, begidx, endidx): return self._complete_sites(text, line, begidx, endidx) def complete_list_sites(self, text, line, begidx, endidx): return self._complete_groups(text, line, begidx, endidx) ############################### sites ################################## def do_add_site(self, arg): """add_site [name] [IP address] [port] [location]""" arg = CommandLine(arg) if len(arg) != 4: self.onecmd('help add_site') return else: pwdb.add_site(arg[0], arg[1], int(arg[2]), arg[3]) def _sites(self, group=None): sites=[] for site in pwdb.list_sites(group=group): sites.append((site['name'], site['ip'], site['location'])) sites.sort(lambda x,y: x[0] < y[0]) return sites def do_list_sites(self, arg): """list_sites""" arg = CommandLine(arg) group = None if len(arg): group = arg[0] sites = self._sites(group=group) if len(sites): name_width = max([ len(e[0]) for e in sites ]) ip_width = max([ len(e[1]) for e in sites ]) #loc_width = max([ len(e[2]) for e in sites ]) for name, ip, loc in sites: print name + ' '*(name_width - len(name)), \ ip + ' '*(ip_width - len(ip)), loc print len(sites), 'sites in the database' ################################ users ################################## def set_password(self, user): pass1 = "" pass2 = " " print 'Setting password for user %s' % user try: while (pass1 != pass2): pass1 = getpass.getpass("Enter new password: ") pass2 = getpass.getpass("Confirm password: ") except EOFError: print 'Abort' return print 'Password set for user %s' % user return pass1 def do_add_user(self, arg): """add_user [uid] [site] [primary]""" arg = CommandLine (arg) if (len(arg) != 3): self.onecmd('help add_user') return else: pass1 = self.set_password(arg[0]) pwdb.add_user_to_site(arg[0], arg[1], pass1, int(arg[2])) def do_list_users(self, arg): """list_users [sitename]""" arg = CommandLine(arg) if not len(arg): if not self.current_site: self.onecmd('help list_users') return site = self.current_site else: site = arg[0] res = pwdb.get_site_for_script(site) if res: site_id = res['id'] else: self.onecmd('help list_users') return users=[] for user in pwdb.list_users(site_id=site_id): users.append((user['uid'], user['password'])) users.sort(lambda x,y: x[0]/?''') if __name__ == '__main__': import sys dbc = DBConsole() if len(sys.argv) > 1: dbc.onecmd(CommandLine(sys.argv[1:]).encode()) else: dbc.cmdloop()