#python #linux and check (and delete if existing) a user

import pwd
 
def check_user_and_delete(user):
    try:
        if pwd.getpwnam(user) is True:
            res = sudo("userdel {}".format(user))
        if res.return_code == 0:
            print "== > User {} deleted".format(user)
        else:
            print "== > User {} not deleted".format(user)
    except KeyError:
            pass

I just love #python

def bits(n):
    res = ''
    """
    Generates the binary digits of n
    """
    while n:
        res+= str(n & 1)
        n >>= 1
    return res[::-1]

#Jenkins and #fabric script to execute commands remotely

tee /tmp/fabfile.py <<EOF
from fabric.api import *
import wget

env.hosts     = open('hostfile', 'r').readlines()
env.user      = open('userfile', 'r').readline().strip()
env.password  = open('passwordfile', 'r').readline().strip()


def vm_run_cmd(cmd, priv=True):
    if priv is True:
        sudo(cmd)
    else:
        run(cmd)


def vm_exec_remote_script(uri, privt=False):
        temp_f = wget.download(uri)
        inf = open(temp_f)
        vm_run_cmd(inf.read().replace('\n', ' '), privt)
        inf.close()
EOF

tee /tmp/hostfile <<EOF
localhost
EOF

tee /tmp/userfile <<EOF
xxxxxx
EOF

tee /tmp/passwordfile <<EOF
xxxxxx
EOF

cd /tmp/
virtualenv venv
source venv/bin/activate
fab vm_exec_remote_script:'https://raw.githubusercontent.com/Flukas88/CodeUtils/master/hello.bash',privt=$sa
fab vm_run_cmd:"$cmdline",priv=$sa
deactivate

User Key Generation

Little idea for a code to associate to a user

#!/usr/bin/env python
#
# Author: Luca Francesca
# A function for key generation
#

import random
import string

def keyGen(uname, pwd):
key = random.choice(string.lowercase)
key += uname[1:4]
key += pwd[2:4] + random.choice(string.uppercase)
key += random.choice(string.lowercase)
key += str(random.randint(2,9))
key += str(random.randrange(1,10) * 3)
key += random.choice(string.uppercase)
key += random.choice(string.lowercase)
key += str(random.randint(random.randint(1,50) ,random.randint(50,70)))
return key;

if __name__ == ‘__main__’:
print keyGen(‘luca’, ‘luca’)
print keyGen(‘marco’, ‘marco’)