Last active
July 9, 2019 16:05
-
-
Save gaubert/15d02d0f5b16fd2c048040c627d1961f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import choice | |
import string | |
import subprocess | |
def generate_passwd(length=8, chars=string.letters+string.digits+'{}"&$@#%*.;'): | |
return ''.join([choice(chars) for i in range(length)]) | |
def generate_username(prefix="jason", index=1): | |
""" """ | |
return "{}{:05d}".format(prefix, index+1) | |
def create_linux_user_passwd(filepath): | |
""" | |
take a filepath and create users | |
""" | |
with open(filepath, "r+") as infile: | |
for line in infile: | |
(user,password) = line.split(",") | |
password = password.rstrip() | |
print("execute: echo {}:{}|chpasswd".format(user,password)) | |
print("execute: ln -s /sftp_root/jason-cs/jason-cs-data /sftp_root/jason-cs/jason-cs-users/{}".format(user)) | |
#subprocess.call("echo {}:{} | chpasswd".format(user,password)) | |
#subprocess.call("ln -s /sftp_root/jason-cs/jason-cs-data /sftp_root/jason-cs/jason-cs-users/{}".format(user)) | |
def main(nb_user, from_index, filepath): | |
""" | |
main | |
""" | |
cpt = 1 | |
with open(filepath, "w+") as outfile: | |
outfile.write("username,password\n") | |
while cpt < nb_user: | |
passwd = generate_passwd(9) | |
user = generate_username("jason", from_index) | |
print("user,passwd = {},{}".format(user, passwd)) | |
outfile.write("{},{}\n".format(user,passwd)) | |
cpt +=1 | |
from_index +=1 | |
print("Finished to generate {} users/passwds".format(cpt)) | |
#execute command on linux | |
create_linux_user_passwd(filepath) | |
if __name__ == "__main__": | |
nb_user=100 | |
from_index=10 | |
filepath= "./jason_userpasswd.txt" | |
main(nb_user,from_index, filepath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment