forked from gartnera/tinyctf-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
60 lines (47 loc) · 1.47 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Low level deployment operations.
"""
from random import randint, Random
from os import path, makedirs
from spur import LocalShell
from time import time
from signal import SIGKILL
from crypt import crypt
class TimeoutError(Exception):
"""
Exception dealing with executed commands that timeout.
"""
pass
def execute(cmd, timeout=60, **kwargs):
"""
Executes the given shell command
Args:
cmd: List of command arguments
timeout: maximum alloted time for the command
**kwargs: passes to LocalShell.spawn
Returns:
An execution result.
Raises:
NoSuchCommandError, RunProcessError, FileNotFoundError
"""
shell = LocalShell()
#It is unlikely that someone actually intends to supply
#a string based on how spur works.
if type(cmd) == str:
cmd = ["bash", "-c"] + [cmd]
process = shell.spawn(cmd, store_pid=True, **kwargs)
start_time = time()
while process.is_running():
delta_time = time() - start_time
if delta_time > timeout:
process.send_signal(SIGKILL)
raise TimeoutError(cmd, timeout)
return process.wait_for_result()
def create_user(username, password):
"""
Creates a user account with the given username
Args:
username: the username to create
"""
execute(["useradd", "-s", "/bin/bash", "-g", "competitors", "-m", "-p", crypt(password, "42"), username])
execute(["chmod", "700", "/home/"+username])