dyn-logon.py
1 |
#! /usr/bin/env python
|
---|---|
2 |
# -*- coding: UTF-8 -*-
|
3 |
|
4 |
###########################################################################
|
5 |
#
|
6 |
# Eole NG - 2008
|
7 |
# Copyright Pole de Competence Eole (Ministere Education - Academie Dijon)
|
8 |
# Licence CeCill cf /root/LicenceEole.txt
|
9 |
# eole@ac-dijon.fr
|
10 |
#
|
11 |
###########################################################################
|
12 |
|
13 |
"""
|
14 |
Utilitaire de génération de fichiers netlogon depuis LDAP
|
15 |
"""
|
16 |
import sys |
17 |
import time |
18 |
from optparse import OptionParser |
19 |
#pour evite un parsedico
|
20 |
#from scribe.cadoleseoletools import my_escape
|
21 |
from scribe.login import logon |
22 |
|
23 |
|
24 |
def my_escape(string): |
25 |
"""
|
26 |
remplacement de caractères pour dyn-logon
|
27 |
-> ne pas remplacer les "."
|
28 |
"""
|
29 |
return string.strip().replace(' ','').replace('"', "\'").replace(';', '\;').replace("'", "\\'") |
30 |
|
31 |
#############################################################
|
32 |
## Boucle principale du programme - gestion des options
|
33 |
#############################################################
|
34 |
|
35 |
def main(): |
36 |
"""
|
37 |
boucle principale (gestion des options)
|
38 |
"""
|
39 |
parser = OptionParser() |
40 |
parser.add_option("-u", "--user", dest="user", help=u"nom du login") |
41 |
parser.add_option("-o", "--os", dest="ostype", help=u"type OS") |
42 |
parser.add_option("-m", "--machine", dest="machine", |
43 |
help=u"Nom de la machine")
|
44 |
parser.add_option("-i", "--ip", dest="adresse_ip", help=u"adresse IP") |
45 |
parser.add_option("-p", "--pid", dest="pid", help=u"numéro du PID", default="unknown") |
46 |
|
47 |
(options, args) = parser.parse_args() |
48 |
|
49 |
debuglogfile = '/tmp/dyn-logon-debug.log'
|
50 |
try:
|
51 |
with open(debuglogfile, 'a') as dbgfile: |
52 |
msg = 'DYNLOGONINF : %s'%[options, args]
|
53 |
dbgfile.write('\n%s %s\n'%(time.strftime('%Y/%m/%d %H:%M:%S'), msg)) |
54 |
except: pass |
55 |
|
56 |
#all options are required
|
57 |
if None in [options.user, options.ostype, options.machine, |
58 |
options.adresse_ip]: |
59 |
parser.print_help() |
60 |
sys.exit(1)
|
61 |
|
62 |
user = my_escape(options.user.lower()) |
63 |
#if first character is not alphanum set None
|
64 |
if not user[0].isalnum(): |
65 |
print "Le premier caractère du nom doit être alphanumérique" |
66 |
sys.exit(1)
|
67 |
if user == "nobody": |
68 |
print "utilisateur nobody : ne rien faire" |
69 |
sys.exit(1)
|
70 |
|
71 |
ostype = my_escape(options.ostype) |
72 |
machine = my_escape(options.machine.lower()) |
73 |
adresse_ip = my_escape(options.adresse_ip) |
74 |
pid = my_escape(options.pid) |
75 |
|
76 |
try:
|
77 |
logon(user, ostype, machine, adresse_ip, pid) |
78 |
except Exception, err: |
79 |
import traceback |
80 |
trcbck = traceback.format_exc() |
81 |
try:
|
82 |
with open(debuglogfile, 'a') as dbgfile: |
83 |
msg = 'DYNLOGONERR : %s :: %s\nENDERR'%(err, trcbck)
|
84 |
dbgfile.write('\n%s %s\n'%(time.strftime('%Y/%m/%d %H:%M:%S'), msg)) |
85 |
except: pass |
86 |
sys.exit(1)
|
87 |
|
88 |
if __name__ == '__main__': |
89 |
#import hotshot
|
90 |
#from hotshot import stats
|
91 |
|
92 |
#prof = hotshot.Profile("hotshot_stats")
|
93 |
#prof.runcall(main)
|
94 |
#prof.close()
|
95 |
#s = stats.load("hotshot_stats")
|
96 |
#s.sort_stats("time").print_stats()
|
97 |
main() |