|
8 | 8 | script_path = "/root/openvpn-install.sh" |
9 | 9 |
|
10 | 10 |
|
11 | | -def create_user_on_server(name) -> bool: |
| 11 | +def create_user_on_server(name, expiry_date) -> bool: |
12 | 12 | try: |
13 | | - os.system(f"chmod +x {script_path}") |
14 | | - bash = pexpect.spawn(f"sudo bash {script_path}", encoding="utf-8", timeout=60) |
15 | | - |
16 | | - bash.expect("Option:") |
| 13 | + if not os.path.exists(script_path): |
| 14 | + logger.error("script not found on ") |
| 15 | + return False |
| 16 | + |
| 17 | + env = {"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"} |
| 18 | + bash = pexpect.spawn( |
| 19 | + "/usr/bin/bash", |
| 20 | + [script_path], |
| 21 | + env=env, |
| 22 | + encoding="utf-8", |
| 23 | + timeout=120, |
| 24 | + ) |
| 25 | + |
| 26 | + bash.expect(r"Option:", timeout=90) |
17 | 27 | bash.sendline("1") |
18 | 28 |
|
19 | | - bash.expect("Name:") |
| 29 | + bash.expect(r"Name:", timeout=90) |
20 | 30 | bash.sendline(name) |
| 31 | + bash.expect(pexpect.EOF, timeout=180) |
21 | 32 |
|
22 | | - bash.expect(pexpect.EOF) |
| 33 | + bash.close() |
23 | 34 | return True |
24 | 35 |
|
25 | | - except pexpect.exceptions.TIMEOUT: |
26 | | - logger.error("time out when read exceeds") |
| 36 | + except pexpect.TIMEOUT: |
| 37 | + logger.error("Timeout occurred while executing script!") |
| 38 | + return False |
| 39 | + except pexpect.EOF: |
| 40 | + logger.error("Script closed earlier than expected!") |
27 | 41 | return False |
28 | 42 | except Exception as e: |
29 | | - logger.error(f"error when create a user on server: {e}") |
| 43 | + logger.error(f"Error occurred: {e}") |
30 | 44 | return False |
31 | 45 |
|
32 | 46 |
|
33 | 47 | async def delete_user_on_server(name) -> bool | str: |
34 | 48 | try: |
35 | | - os.system(f"chmod +x {script_path}") |
36 | | - bash = pexpect.spawn(f"sudo bash {script_path}", encoding="utf-8", timeout=60) |
| 49 | + if not os.path.exists(script_path): |
| 50 | + logger.error("script not found on ") |
| 51 | + return False |
| 52 | + |
| 53 | + env = {"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"} |
| 54 | + bash = pexpect.spawn( |
| 55 | + "/usr/bin/bash", |
| 56 | + [script_path], |
| 57 | + env=env, |
| 58 | + encoding="utf-8", |
| 59 | + timeout=120, |
| 60 | + ) |
| 61 | + |
| 62 | + bash = pexpect.spawn(f"bash {script_path}", encoding="utf-8", timeout=60) |
37 | 63 |
|
38 | 64 | bash.expect("Option:") |
39 | 65 | bash.sendline("2") |
|
0 commit comments