Aug 01

# less the youngest file in the dir that is not a dot file.
function lless () {
less `ls -Art1|tail -1`
}

written by admin

Aug 01

Whenever I start a new gig I have to key all the boxes – it’s tedious at best. Here’s a bash function to put a key on a far box. I always make sure I have an “eval `ssh-agent`” and “ssh-add” in my shell before running ‘keybox’.

Usage:
keybox [user@]box
Example:
keybox root@hammerforge

Warnings:
Uses ‘mktemp’ which might not be on every OS. Assumes a POSIXy shell at both ends. Tested with bash and zsh.


##############################
##########
# Thu May 28 13:57:25 2009
# will key a box for you
# use like "keybox foobar" OR "keybox user@foobar" it will take
# ~/.ssh/id_dsa.pub (change for your keytype) and write it to the far
# $USER/.ssh/authorized_keys file. Assumes you are using ssh-agent, ssh-add
# for passwdless logins.
#-Tony Thu May 28 16:59:22 2009

function keybox () {

# correct for your keytype
SSH_PUB_KEY=~/.ssh/id_dsa.pub
#SSH_PUB_KEY=~/.ssh/id_rsa.pub
###

# see is we got user@box, key the 'user' (ie root@hammerforge will
# have your key added to ~root/.ssh/authorized_Keys)
if (echo $1|grep -q @)
then
USER_TO_KEY=`echo $1|cut -f1 -d'@'`
MACHINE_TO_KEY=`echo $1|cut -f2 -d'@'`
else
USER_TO_KEY=$USER
MACHINE_TO_KEY=$1
fi

AUTH_KEYS=.ssh/authorized_keys
if [ -f ${SSH_PUB_KEY} ]
then
echo found ${SSH_PUB_KEY}
else
echo "did not file a public key, generating a new dsa key"
ssh-keygen -t dsa
echo
echo "continuing to keybox $MACHINE_TO_KEY"

fi
LOCAL_KEY=`cat ${SSH_PUB_KEY}`

TEMP_FILE=`mktemp -u`
# use some ssh options so it doesn't complain about known_hosts.
ssh -o StrictHostKeyChecking=no ${USER_TO_KEY}@${MACHINE_TO_KEY} "cp ~${USER_TO_KEY}/${AUTH_KEYS} ~${USER_TO_KEY}/.ssh/hold_authorized_keys ;echo \"$LOCAL_KEY\" > $TEMP_FILE ; cat $TEMP_FILE >> ~${USER_TO_KEY}/${AUTH_KEYS}"
# do another ssh to confirm you can get back to the box
# Batchmode causes ssh it quit if the autologin doesn't work
ssh -o BatchMode=yes ${USER_TO_KEY}@${MACHINE_TO_KEY} "hostname; date ; rm -v $TEMP_FILE"

}

written by admin \\ tags:

Jan 07

I always seem to need a tmp file, I used to do ‘vi /tmp/foo’ but it usually had something in it from last time.  This function opens a new file and stores the file name in $f.

I use it like:

vt
<paste some stuff, clean it up>
perl -pe ’s/foo/bar/’ $f

####
function vt () {
    for i in `seq 0 255`;
    do
        FILE=/tmp/$USER-foo-$i;
        if [ -f "$FILE" ]; then
            echo -n '.';
        else
            f=$FILE;
            vi $FILE;
            echo $FILE;
            return;
        fi;
    done
}

###### Cleanup
function cleanvt () {
for i in `seq 0 255`
do
    FILE=/tmp/$USER-foo-$i

    if [ -f "$FILE" ]
    then
    echo -n '.'
    rm $FILE
    else
        echo
        return
fi
done
echo
}

written by admin

Sep 06
I love writing shell commands and functions. Everyone does something with ‘ll’ mine is:
        alias ll=’ls -lart’ # because I want to see the newest file last.
Lots of times I also want the full path of a file. I could just type
it, but I make a lot of errors when I type long things. A long time
ago I decided cut-and-paste are a dyslexsics best friend. Here are a
couple of helper tools:
  ’lll’ gets me a full path
  ’llll’ gets me a path suitable for use in scp.
I do this a lot:
        tony-ws:log> llll setup.log
        tony-ws:/var/log/setup.log
On another box:
        betlong:log> scp tony-ws:/var/log/setup.log . 
#quick demo
for i in lll llll 
 do echo “this is what $i does: “
    $i hacked-footertracking.html foo.*
 echo
 done
this is what lll does: 
/home/tony/tmp/hacked-footertracking.html
/home/tony/tmp/foo.idx
/home/tony/tmp/foo.pl
/home/tony/tmp/foo.pl~
/home/tony/tmp/foo.sh
/home/tony/tmp/foo.sh~
this is what llll does: 
tony-ws:/home/tony/tmp/hacked-footertracking.html
tony-ws:/home/tony/tmp/foo.idx
tony-ws:/home/tony/tmp/foo.pl
tony-ws:/home/tony/tmp/foo.pl~
tony-ws:/home/tony/tmp/foo.sh
tony-ws:/home/tony/tmp/foo.sh~
############
# often want full path to file(s), this gets it for me
# Tony Hansmann (t o n y replacewith-at-sign open source consulting com)
lll () 
    if [ ! -z "$*" ]
        then
        for i in $*;
          do
            if (echo $i |egrep -q “^/|\.\./”)
                then
                ls $i
            else
                \ls `pwd`/$i;
            fi
        done
    else
        pwd;
    fi
}


##########
# often need to have a file or dir name in scp format
# box:/full/path/to/file, this gets it for me
# Tony Hansmann (t o n y replacewith-at-sign open source consulting com)
llll () { 
  if [ ! -z "$*" ]
  then
    if [ ! -z "$*" ]
    then
      for i in $*
      do
        if (echo $i |egrep -q “^/|\.\./”)
        then
          echo $(hostname):$(\ls $i);
        else
          echo $(hostname):$(\ls `pwd`/$i);
        fi
      done
    fi

  else
    echo $(hostname):`pwd`;
  fi
}

written by admin