Thursday, January 19, 2012

bk - a useful alias to go back to the last directory in bash

i find myself going back to the previous directory i was just in all the time in a bash terminal, and instead of commands like:


cd /some/directory/
cd /another/directory/
cd /some/directory/


you can remember the command:


cd ~-


but it is more useful to have an alias defined in an aliases file (usually ~/.bash_aliases is easily sourced by your ~/.profile, or ~/.bashrc)

a few cd related aliases i tend to include:


alias ..='cd ../'
alias ...='cd ../../'
alias bd='cd ~-'


on a related note; there are other directory changing commands (other than cd) that are good to learn (and use for functions or aliases):  popd, pushd, and dirs.

--
hope u find this useful...

Saturday, January 14, 2012

lkrkpass - a cli password gen script with key order in mind

hey everyone,

i apologize for the lack of recent posts. i have been busy with other projects.  i have been meaning to post a few scripts/projects recently including a yet to be released network knocking set of scripts (work still in progress), and the following lkrkpass (a cli password generator python script with key order in mind):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
PROJECT:    lkrkpass

DESCRIPTION:    a random password gen with key-order options in mind.
This script is designed to be primarily using left-key, right-key order.

USE:
    lkrkpass [options]

Default (no options) will create a 7 to 10 character password
    alternating between left-key, and right-key on a qwerty keyboard

Options:
    * Help message - this help message
        -h
        --help
    * Include Upper Case letters
        -u
        --upper-case
    * Include Symbols - see note below
        -s
        --symbols
    * Number of passwords in the password
        -n NUMBER
        --num NUMBER
    * Count of characters
        -c
        --char-count
    * Minimum number of characters
        -m NUMBER
        --min NUMBER
    * maXimum number of characters
        -x NUMBERS
        --max NUMBERS
    * Even keys
        -e KEYS
        --even KEYS
    * Odd keys
        -o KEYS
        --odd KEYS
    * Left keys to use
        -l KEYS
        --left-keys KEYS
    * Right keys to use
        -r KEYS
        --right-keys KEYS
    * Keys - Use only the given keys (one handed, or pin)
        -k KEYS
        --keys KEYS

Example use:
    * make a 7 character password using alternating keys selected at
    random from the left (asdf) and right (jkl):

        lkrkpass -c 7 -l "asdf" -r "jkl"

    * make a password, perhaps including upper-case letters for the
    keys:

        lkrkpass -u

    * make a left handed password without double (2 in a row) characters:

        lkrkpass -e asdfzxcv -o 1234qwer

    * make a left handed password, allowing for some double letters of
    only the home keys:

        lkrkpass -e asdfzxcv -o asdfqwer

    * list out 23 (6 to 9 character) passwords

        lkrkpass -n 23 -m 6 -x 9

    * make a 4 digit pin number

        lkrkpass -c 4 -k 1234567890

    * make a binary password:

        lkrkpass -k 10

    * make hex password:

        lkrkpass -k 1234567890abcdef

    * make a password with even characters as numbers, and odd
    characters of left home keys (asdf)

        lkrkpass -e 1234567890 -o asdf

*****
Please keep in mind, the only mechanism this script has of preventing
double (or more) characters is the set of alternating character
dictionaries.  The '-k', or '--keys' option negates this.  This is a
feature, not a bug. One can still have randomness in binary.

If you wish to use symbols it would be wisest to just use '-s'
or '--symbols', and not change options for keys/key-order (in other
words, dont use options such as: '-l', '-r', '-k', '-e', '-o', or
their longer option representations)

--  Hope you enjoy the script!

@copyright: 2010 by nairb <code@nairb.us>
@license: GNU GPL, see COPYING for details.
"""

import sys
import string as okchars
from getopt import getopt as theoptions
from random import randint as anum


def lkrk (passmin=7, passmax=10, uc="no", smbls="no", lk='12345qwertasdfgzxcvb', rk='67890yuiophjklnm'):

    mypass = ''
 
    if passmin == passmax:
        passlen = passmax
    elif passmin < passmax:
        passlen = anum(passmin, passmax)
    else:
        passlen = passmin
 
    for i in range(0, passlen):
        if (i%2) == 0:
            mypass = mypass + lk[anum(0, (len(lk)-1))]
        else:
            mypass = mypass + rk[anum(0, (len(rk)-1))]

    if uc == "yes":
        if smbls == "yes":
            return SymThat(RandUpCaseThat(mypass))
        else:
            return RandUpCaseThat(mypass)
    else:
        if smbls == "yes":
            return SymThat(mypass)
        else:
            return mypass


def RandUpCaseThat(mypass):

    uppass = ''

    for i in range(0, len(mypass)):
        if mypass[i] in okchars.lowercase:
            if ((anum(1, 10))%3) == 0:
                uppass = uppass + str.capitalize(mypass[i])
            else:
                uppass = uppass + mypass[i]
        else:
            uppass = uppass + mypass[i]

    return str(uppass).encode('utf8')
 

def SymThat(mypass):
 
    leetpass=''
    lk='12345qwertasdfgzxcvb'
    oklsmbls=u"""`~!@#$%^"""
    okrsmbls=u"""&*()-_=+[{]}|\\:;\"'<,>.?/"""
 
    for i in range(0, len(mypass)):
        if mypass[i] in okchars.lowercase:
            if ((anum(1, 10))%3) == 0:
                if mypass[i] in lk:
                    leetpass = leetpass + str(oklsmbls[anum(0, (len(oklsmbls)-1))]).encode('utf8')
                else:
                    leetpass = leetpass + str(okrsmbls[anum(0, (len(okrsmbls)-1))]).encode('utf8')
            else:
                leetpass = leetpass + mypass[i]
        else:
            leetpass = leetpass + mypass[i]
     
    return leetpass.encode('utf8')


def PrintPass(passmin=7, passmax=7, uc="no", smbls="no", lk='12345qwertasdfgzxcvb', rk='67890yuiophjklnm'):

    print str(lkrk(passmin, passmax, uc, smbls, lk, rk))

    return 0


def Main():

    passmin = 7
    passmax = 10
    numpasses=1
    uc = "no"
    smbls= "no"
    onlykeys = "no"
    lk = '12345qwertasdfgzxcvb'
    rk = '67890yuiophjklnm'

    try:
        opts, args = theoptions(sys.argv[1:], "husn:c:e:o:k:l:r:m:x:",
        ['help', 'upper-case', 'num=', 'char-count=', 'even=', 'odd=', 'keys=',
        'left-keys=', 'right-keys=', 'min=', 'max=', 'symbols'])
    except:
        print "\nERROR:  Invalid option given!!!\n\n"
        print __doc__
        exit(2)

    for o, a in opts:
        if o in ("-h", "--help"):
            print __doc__
            exit(0)
        elif o in ("-n", "--num"):
            try:
                numpasses = int(a)
            except:
                pass
        elif o in ("-c", "--char-count"):
            try:
                passmin = int(a)
                passmax = int(a)
            except:
                pass
        elif o in ("-m", "--min"):
            try:
                passmin = int(a)
            except:
                passmin = 7
        elif o in ("-x", "--max"):
            try:
                passmax = int(a)
            except:
                passmax = 10
        elif o in ("-u", "--upper-case"):
            uc = "yes"
        elif o in ("-s", "--symbols"):
            smbls = "yes"
        elif o in ("-k", "--keys"):
            thekeys = str(a).encode('utf8')
            if thekeys in ("", "-"):
                thekeys = (okchars.digits + okchars.lowercase)
            onlykeys = "yes"
        elif o in ("-l", "--left-keys", "-o", "--odd"):
            lk=a
            if lk in ("", "-"):
                lk='12345qwertasdfgzxcvb'
        elif o in ("-r", "--right-keys", "-e", "--even"):
            rk=a
            if rk in ("", "-"):
                rk='67890yuiophjklnm'
        else:
            assert False, "unhandled option"

    if passmin <= 0:
        passmin = 7

    if passmax <= 0:
        passmax = 10

    if onlykeys == "yes":
        lk = thekeys
        rk = thekeys

    for i in range(0, len(lk)):
        if lk[i] in (okchars.digits + okchars.lowercase):
            pass
        else:
            lk='12345qwertasdfgzxcvb'

    for i in range(0, len(rk)):
        if rk[i] in (okchars.digits + okchars.lowercase):
            pass
        else:
            rk='67890yuiophjklnm'

    if numpasses >> 1:
        for i in range(0, numpasses):
            PrintPass(passmin, passmax, uc, smbls, lk, rk)
        exit
    else:
        PrintPass(passmin, passmax, uc, smbls, lk, rk)

    return 0


if __name__ == '__main__':
    Main()


hope someone finds the script useful. i will likely still finish and post a cross-platform GUI/cli password-gen/safe including multiple engines (lkrk, pronounceable, ect), but izzapass (what im calling it for now) is still a work in progress.