File: user.py

package info (click to toggle)
getmail6 6.19.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,124 kB
  • sloc: python: 6,634; sh: 897; makefile: 73
file content (44 lines) | stat: -rwxr-xr-x 919 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python3

import passlib.hash
import os
import pwd
import random
import string
import subprocess
import sys


def random_string(length):
    '''Return a random string, consisting of ASCII letters, with given
    length.'''

    s = ''
    maxind = len(string.ascii_letters)-1
    for _ in range(length):
        s += string.ascii_letters[random.randint(0, maxind)]
    return s.lower()


def login_exists(login):
    '''Checks whether the given login exists on the system.'''

    try:
        pwd.getpwnam(login)
        return True
    except KeyError:
        return False

login = None

while True:
    login = random_string(8)
    if not login_exists(login):
        break

password = random_string(8)
salt = random_string(2)

crypted = passlib.hash.sha512_crypt.hash(password, rounds=5000)
subprocess.check_call(['useradd', '-p', crypted, '-m', '-s', '/bin/bash', login])
print (password + " " + login)