|
| 1 | +import os.path |
| 2 | +import paramiko |
| 3 | +from jinja2 import FileSystemLoader |
| 4 | +import src.lib.backend_utils as u |
| 5 | +import jinja2 |
| 6 | + |
| 7 | +__DEBUG__=0 |
| 8 | +def set_config(config): |
| 9 | + u.__CONFIG__ = config |
| 10 | + |
| 11 | +def open_ssh_conn(): |
| 12 | + pkey = paramiko.Ed25519Key.from_private_key_file('../.ssh/id_ed25519') |
| 13 | + client = paramiko.SSHClient() |
| 14 | + policy = paramiko.AutoAddPolicy() |
| 15 | + client.set_missing_host_key_policy(policy) |
| 16 | + host = u.config('host') |
| 17 | + user = u.config('user') |
| 18 | + try: |
| 19 | + client.connect(host, username=user, pkey=pkey) |
| 20 | + return client |
| 21 | + except paramiko.ssh_exception.SSHException as e: |
| 22 | + e_dict = e.args[0] |
| 23 | + print(u.returncode(1, "Erreur d'authentification")) |
| 24 | + exit(1) |
| 25 | + |
| 26 | + |
| 27 | +def exec_cmd(command): |
| 28 | + client=open_ssh_conn() |
| 29 | + stdin, stdout, stderr = client.exec_command(command) |
| 30 | + content=stdout.read().decode() |
| 31 | + del client, stdin, stdout, stderr |
| 32 | + return content |
| 33 | + |
| 34 | +def compose_dn(entity): |
| 35 | + rdnValue=u.find_key(entity,'cn') |
| 36 | + branchAttr=u.config('branchAttr','') |
| 37 | + branch = '' |
| 38 | + if branchAttr != '': |
| 39 | + branchValue=u.find_key(entity,branchAttr) |
| 40 | + |
| 41 | + match branchValue: |
| 42 | + case 'etd': |
| 43 | + branch=u.config('branchForEtd','') |
| 44 | + case 'esn': |
| 45 | + branch = u.config('branchForEsn', '') |
| 46 | + case 'adm': |
| 47 | + branch = u.config('branchForAdm', '') |
| 48 | + if branch != '': |
| 49 | + return 'cn=' + rdnValue+ ',' + branch + "," + u.config('base') |
| 50 | + else: |
| 51 | + return 'cn=' + rdnValue+ "," + u.config('base') |
| 52 | + |
| 53 | +def dn_superior(dn): |
| 54 | + tab=dn.split(',') |
| 55 | + tab.pop(0) |
| 56 | + return ','.join(tab) |
| 57 | + |
| 58 | + |
| 59 | +def gen_script_from_template(entity,template): |
| 60 | + data={ |
| 61 | + 'domain' :u.config('domain'), |
| 62 | + 'base': u.config('base'), |
| 63 | + 'dn' : compose_dn(entity), |
| 64 | + 'path': dn_superior(compose_dn(entity)), |
| 65 | + 'e': u.make_entry_array(entity) |
| 66 | + } |
| 67 | + environment = jinja2.Environment(loader=FileSystemLoader("../ps1_templates/")) |
| 68 | + template = environment.get_template(template) |
| 69 | + content=template.render(data) |
| 70 | + return content |
| 71 | + |
| 72 | +def ad_exec_script(entity,template,params=""): |
| 73 | + content=gen_script_from_template(entity,template) |
| 74 | + client = open_ssh_conn() |
| 75 | + sshfile = client.open_sftp() |
| 76 | + pid=os.getpid() |
| 77 | + if __DEBUG__ == 0 : |
| 78 | + scriptName='sesame_script.' + str(pid) + '.ps1' |
| 79 | + else: |
| 80 | + scriptName='sesame_script.ps1' |
| 81 | + with sshfile.open(scriptName, mode="w") as message: |
| 82 | + message.write(content) |
| 83 | + ##execution du script |
| 84 | + chan = client.get_transport().open_session() |
| 85 | + if params == '': |
| 86 | + cmd=scriptName |
| 87 | + else: |
| 88 | + cmd=scriptName + " " + params |
| 89 | + chan.exec_command('powershell -ExecutionPolicy Bypass -NonInteractive -File ' + cmd) |
| 90 | + exitCode = chan.recv_exit_status() |
| 91 | + content = chan.recv(4096).decode('utf-8') |
| 92 | + error = chan.recv_stderr(4096).decode() |
| 93 | + if __DEBUG__ == 0: |
| 94 | + chan = client.get_transport().open_session() |
| 95 | + ##supression du script |
| 96 | + chan.exec_command('del ' + scriptName) |
| 97 | + del client |
| 98 | + if exitCode == 0: |
| 99 | + print(u.returncode(0,content.rstrip("\n"))) |
| 100 | + exit(0) |
| 101 | + else: |
| 102 | + print(u.returncode(1,content.rstrip("\n"))) |
| 103 | + exit(1) |
| 104 | + |
0 commit comments