Generating md5 encrypted password for chpasswd

If you want to generate properly encrypted password to feed to chpasswd, the most easier and proper way is to do that from command line :

echo "encryptedpassword"|openssl passwd -1 -stdin

If you want to generate in pure python you can do it like that :

    def md5crypt(password, salt, magic='$1$'):
        import md5
        m = md5.new()
        m.update(password + magic + salt)

        # /* Then just as many characters of the MD5(pw,salt,pw) */
        mixin = md5.md5(password + salt + password).digest()
        for i in range(0, len(password)):
            m.update(mixin[i % 16])

        # /* Then something really weird... */
        # Also really broken, as far as I can tell.  -m
        i = len(password)
        while i:
            if i & 1:
                m.update('x00')
            else:
                m.update(password[0])
            i >>= 1

        final = m.digest()
        # /* and now, just to make sure things don't run too fast */
        for i in range(1000):
            m2 = md5.md5()
            if i & 1:
                m2.update(password)
            else:
                m2.update(final)
            if i % 3:
                m2.update(salt)
            if i % 7:
                m2.update(password)

            if i & 1:
                m2.update(final)
            else:
                m2.update(password)

            final = m2.digest()

        # This is the bit that uses to64() in the original code.
        itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
        rearranged = ''
        for a, b, c in ((0, 6, 12), (1, 7, 13), (2, 8, 14), (3, 9, 15), (4, 10, 5)):
            v = ord(final[a]) < < 16 | ord(final[b]) << 8 | ord(final[c])
            for i in range(4):
                rearranged += itoa64[v & 0x3f]; v >>= 6

        v = ord(final[11])
        for i in range(2):
            rearranged += itoa64[v & 0x3f]; v >>= 6

        return magic + salt + '$' + rearranged

You need to feed it up with a salt, like this :

    def generate_salt(count):
        import random, string
        char = string.ascii_letters + string.digits + string.punctuation.replace(':', '')
        return string.join(map(lambda x,v=char: random.choice(v), range(count)), '')

Automatic best resolution with xrandr

If you like me you have a big screen with your laptop and wants to automate when your X session start to get the best resolution, you can use that script :

#!/bin/bash

function get_resolutions() {
xrandr|while read -a line;do
RES="${line[1]}x${line[3]} "
[[ ${RES} != [0-9]* ]] && continue
echo ${RES}
done
}

_BEST_RES=0
BEST_RES=
for res in $(get_resolutions);do
_res=${res/x/}
[[ $_res -ge ${_BEST_RES} ]] && {
BEST_RES=${res}
_BEST_RES=${_res}
}
done
xrandr -s ${BEST_RES}

Automate SSH known_hosts cleanup

If you like me, you have to do a lot of installs[1] of the same test machine with the same IP and have to ssh it you will notice this annoying message :

IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
54:9d:c0:37:3a:80:48:6c:82:ec:d1:84:93:61:24.
Please contact your system administrator.
Add correct host key in /home/cboudjnah/.ssh/known_hosts
to get rid of this message.
Offending key in /home/cboudjnah/.ssh/known_hosts:595
Password authentication is disabled to avoid
 man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid
man-in-the-middle attacks.
Agent forwarding is disabled to avoid man-in-the-middle attacks.

I have automated the cleanup by a script :

#!/bin/bash
H=$1

[[ -z ${H} ]] && { echo "Need a host as argument"; exit 1 ;}
LINE=$(ssh -o StrictHostKeyChecking=yes $1 'exit' 2>&1 | sed -n '/Offending key/ { s/.*://;s/r//;p }')
[[ -z ${LINE} ]] && { echo "Nothing to clean";  exit; }
sed -i -n "$LINE!p" ~/.ssh/known_hosts

[1] Like having to tests bunch of FAI.

Linus Torvalds on GIT

Pretty good video to look if you like to know more about git and its creation :

http://www.youtube.com/watch?v=4XpnKHJAok8

Mandriva Popular in Australia

I have conducting a lot of different interviews for a position available as Linux Sysadmin in my company. What surprised me is the number of people having Mandrake/Mandriva on there resume as there first and current Linux experience. Make me proud everytime to have worked in such company so well known everywhere i go.

Little one coming in July

Little one (Noah) and his mum are coming in July, this is pretty cool :)

Noah looking at us

Always search before coding

This is a annoying, even if it take 5mn to code thing like that :

(defun my-dired-rm-rf()
  "Rm -rf directories"
  (interactive)
  (let ((sel (selected-window)))
	(dolist (curFile (dired-get-marked-files))
	  (if (yes-or-no-p (concat "Do you want to remove \"" (file-name-nondirectory curFile) "\" ? "))
		  (progn
			(shell-command (concat "rm -rvf " curFile) 
"*Removing Directories*")
			(kill-buffer "*Removing Directories*")
			(select-window sel)
			(revert-buffer)
			)
		))
	))

you discover after a litlle while that if you have did a lilt bit of searching before, you will have discovered a variable call `dired-recursive-deletes` that would do the thing in a much better way.

Import Export to different tablespace names with Oracle 9i

Renaming Oracle Database is a pain, coming from OpenSource DB like
MySQL or PostGres where we do that all the time i did not think that
Oracle have to be such a pain.

My only way i can find.

- If i have the tablespace named tablesp1 and owned by the
user user1, and i want to import it to another tablespace
called tablesp2 in an another Oracle 9i tablespace with the user
name user2.

  • I import the tablesp1 in an Oracle 10 (if you are lucky to have
    one).
  • Make sure i create the user2 in the Oracle10 DB.
  • I connect as DBA access and i rename the tablespace with :

    ALTER TABLESPACE tablesp1 RENAME TO tablesp2
    ALTER TABLESPACE tablesp2 OWNER TO user2
      
  • Export the Oracle10 tablespace with Oracle 9 exp.
    Sometime sometime you may encounter that famous error

    "EXP-00003 : no storage definition found for segment .....".
    

    So you have have to
    do this as well.

  • That’s it. The dump should be under tablespace name tablesp2 with
    owner user2.

SVN Diff against changes in the remote repository.

A useful svn wrapper scripts. Get a diff of your local repostitory against the upstream repository changes. I wonder why it is not builtins though like a svn status -u but for dif.

#!/bin/bash
 
IFS="
"
for line in `svn status -u`;do
    [[ $line != "   "* ]] && continue
    rev=`echo $line|awk '{print $2}'`
    ff=`echo $line|awk '{print $3}'`
    svn diff -r${rev}:HEAD $ff
done

Stopped smoking

Cigarette Ashtray
While reading this entry. I just remembered that it has been a a litle bit more than a month that i stopped smoking. I was a real smoker, smoking a 25 red malboro every day. But one day i wake up (one of those
days you know) and like that i thought to maybe stop smoking. So far it has been good, the hardset part is the first days really, after it get OK. I hope it will last for me, i feel so much better i think since i stopped smoking.