From eb2c6b1db619cd311d7ed9b9ead0f2da26e237cd Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Thu, 30 Aug 2018 02:51:31 +0200 Subject: [PATCH] Added files --- .gitignore | 104 +++++++++++++++++++++++++++++++++++++++++ README.md | 1 + pastafariutils/unix.py | 74 +++++++++++++++++++++++++++++ setup.cfg | 3 ++ setup.py | 38 +++++++++++++++ 5 files changed, 220 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pastafariutils/unix.py create mode 100644 setup.cfg create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af2f537 --- /dev/null +++ b/.gitignore @@ -0,0 +1,104 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +.static_storage/ +.media/ +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..1361668 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Utils for scripts in *nix like systems. diff --git a/pastafariutils/unix.py b/pastafariutils/unix.py new file mode 100644 index 0000000..570f321 --- /dev/null +++ b/pastafariutils/unix.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import time +import os +import re +import argparse +import json +import pwd +import sys +import crypt +from subprocess import call, DEVNULL + +def add_user(user, password='', group='', user_directory='', shell='/usr/sbin/nologin'): + + if user_directory=='': + user_directory=user + + try: + user_pwd=pwd.getpwnam(user) + + return (False, 'User exists') + + except KeyError: + + # add user + + if password!='': + + salt=crypt.mksalt(crypt.METHOD_SHA512) + + password='-p \"%s\"' % crypt.crypt(password, salt).replace('$', '\$') + + if group!='': + + stat_group=os.stat('/home/%s' % user_directory) + gid=stat_group.st_gid + + func_user="sudo useradd -m -s %s -g %i %s -d /home/%s %s" % (shell, gid, password, user_directory, user) + + else: + + func_user="sudo useradd -m -s %s %s -d /home/%s %s" % (shell, password, user_directory, user) + + if call(func_user, shell=True, stdout=DEVNULL) > 0: + + return (True, '') + + else: + return (False, 'Error executing useradd command') + + +def change_password(user, new_password): + + try: + user_pwd=pwd.getpwnam(user) + + if call("sudo echo \"%s:%s\" | chpasswd" % (user, new_password), shell=True, stdout=DEVNULL) > 0: + + return (True, 'Change password successfully') + + except KeyError: + + return (False, 'I cannot change password, user exists?') + +def del_user(user): + + if call("sudo userdel -r %s" % user, shell=True, stdout=DEVNULL, stderr=DEVNULL) > 0: + + return (True, 'Deleted user successfully') + + else: + + return (False, '') + diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..8c28267 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,3 @@ +[metadata] +description-file = README.md + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..20bebdd --- /dev/null +++ b/setup.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +import sys +import os +from setuptools import setup, find_packages + + +if sys.version_info < (3, 3): + raise NotImplementedError("Sorry, you need at least Python 3.3 for use pastafariutils.") + +#import paramecio +# Pillow should be installed after if you need ImageField +# If you install passlib and bcrypt, the password system will use bcrypt by default, if not, will use native crypt libc + +setup(name='paramecio', + version='0.5.0', + description='Utils for make *nix scripts.', + long_description='Utils for make *nix scripts.', + author='Antonio de la Rosa Caballero', + author_email='antonio.delarosa@coesinfo.com', + url='https://bitbucker.org/paramecio/parameciofm/', + packages=['pastafariutils'], + include_package_data=True, + install_requires=[], + license='GPLV3', + platforms = 'any', + classifiers=['Development Status :: 1 - Beta', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: GPLV2 License', + 'Topic :: Software Development :: Libraries :: Utils for *nix servers', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7' + ], + )