@@ -93,22 +93,27 @@ def gen_script_from_template(entity,template):
9393 'base' : u .config ('base' ),
9494 'dn' : compose_dn (entity ),
9595 'path' : dn_superior (compose_dn (entity )),
96- 'e' : u .make_entry_array (entity )
96+ 'e' : u .make_entry_array (entity ),
97+ 'config' : u .get_config ()
9798 }
9899 environment = jinja2 .Environment (loader = FileSystemLoader ("../ps1_templates/" ))
99100 template = environment .get_template (template )
100101 content = template .render (data )
101102 return content
102103
103104def ad_exec_script (entity ,template ,params = "" ):
105+ if u .config ('debug' ,0 ) == "1" :
106+ __DEBUG__ = 1
107+ else :
108+ __DEBUG__ = 0
104109 content = gen_script_from_template (entity ,template )
105110 client = open_ssh_conn ()
106111 sshfile = client .open_sftp ()
107112 pid = os .getpid ()
108113 if __DEBUG__ == 0 :
109114 scriptName = 'sesame_script.' + str (pid ) + '.ps1'
110115 else :
111- scriptName = 'sesame_script. ps1'
116+ scriptName = os . path . splitext ( os . path . basename ( sys . argv [ 0 ]))[ 0 ] + ". ps1"
112117 with sshfile .open (scriptName , mode = "w" ) as message :
113118 message .write (content )
114119 ##execution du script
@@ -117,19 +122,21 @@ def ad_exec_script(entity,template,params=""):
117122 cmd = scriptName
118123 else :
119124 cmd = scriptName + " " + params
120- chan . exec_command ( 'powershell -ExecutionPolicy Bypass -NonInteractive -File ' + cmd )
121- exitCode = chan .recv_exit_status ( )
122- content = chan .recv ( 4096 ). decode ( 'utf-8' )
123- error = chan .recv_stderr (4096 ).decode ()
124- if __DEBUG__ == 0 :
125+ if __DEBUG__ == 0 :
126+ chan .exec_command ( 'powershell -ExecutionPolicy Bypass -NonInteractive -File ' + cmd )
127+ exitCode = chan .recv_exit_status ( )
128+ content = chan .recv (4096 ).decode ()
129+ error = chan . recv_stderr ( 4096 ). decode ()
125130 chan = client .get_transport ().open_session ()
126131 ##supression du script
127132 chan .exec_command ('del ' + scriptName )
128- del client
129- if exitCode == 0 :
130- print (u .returncode (0 ,content .rstrip ("\n " )))
131- exit (0 )
133+ del client
134+ if exitCode == 0 :
135+ print (u .returncode (0 ,content .rstrip ("\n " )))
136+ exit (0 )
137+ else :
138+ print (u .returncode (1 ,content .rstrip ("\n " )))
139+ exit (1 )
132140 else :
133- print (u .returncode (1 ,content .rstrip ("\n " )))
134- exit (1 )
141+ print (u .returncode (0 , "Backend in debug mode" ))
135142
0 commit comments