Sunday, January 29, 2017

Forwarding ports with sshuttle, and making ssh require no password.

The first rule in *unix: if you type the sequence more than 10 times, write a script.
I've been using sshuttle over VPN to make my computer part of the network by forwarding all ports, even though I'm in the coffee shop across the street.
sudo sshuttle --dns -r me@thatserver.com 0/0
However, I actually use this to connect to many, many servers. I basically type the same 5 commands over and over to make SSH require no password after the first login.

I created a script that does all of the work for me. You just put in your username and server and then enter the password...and you never need to type your password again (unless you change your private key).
#!/bin/bash
#Before anything else, set the PATH_SCRIPT variable
 pushd `dirname $0` > /dev/null; PATH_SCRIPT=`pwd -P`; popd > /dev/null
 PROGNAME=${0##*/}; PROGVERSION=0.1.0 

printf '\nWarning!\nThis script creates the ssh key pair so that one does not need\n
to type a password to login more than once.  If you know that you\n
need to do this, you probably can check this scripts source\n
to see what is being done.\n\n'

read -r -p "Are you sure you want to continue? [y/N] " response
case "$response" in
    [yY][eE][sS]|[yY]) 
        ;;
    *)
        exit 1
        ;;
esac
printf "This script will put public key on the remove server\n
and now it will ask for your USERNAME and the REMOTESERVER\n"
printf "USERNAME [ENTER]:"
read USERNAME
printf "REMOTESERVER [ENTER]:"
read REMOTESERVER

echo "Will send pair to $USERNAME@$REMOTESERVER"

if [ -f "$HOME/.ssh/id_rsa.pub" ]; then
   echo "$HOME/.ssh/id_rsa.pub exists, skipping rsa key generation"
else
   #create the key pair
   echo "ssh-keygen -t rsa"
   ssh-keygen -t rsa
fi

#create the remote directory if it doesn't exist and change the mode
echo "ssh $USERNAME@$REMOTESERVER 'mkdir -p .ssh && chmod 700 .ssh'"
ssh $USERNAME@$REMOTESERVER 'mkdir -p .ssh && chmod 700 .ssh'

echo "cat .ssh/id_rsa.pub | ssh $USERNAME@$REMOTESERVER 'cat >> .ssh/authorized_keys'"
cat $HOME/.ssh/id_rsa.pub | ssh $USERNAME@$REMOTESERVER 'cat >> .ssh/authorized_keys'

Friday, January 20, 2017

256-bit multiplication in BASH, if you have the time.

In my continuing saga of using BASH to glue programs together with bashbignumbers.sh , it takes 8 seconds to do a 128-bit multiply resulting in a 256-bit result, and 31 second for a 512-bit result.
#!/bin/bash
# Required programs:
BIGNUMBERS=bashbignumbers.sh
if [ ! -f $BIGNUMBERS ]; then
    echo "File, $BIGNUMBERS, not found!"
    exit
fi
source "$BIGNUMBERS"
TESTSTR256_0="ce6a8c03135bf12ca7ca2e748c9c3557ca564f9b69a2565f6adee7000d9236ec"
TESTSTR256_1="a325aa75a7335e84f11f80c46f0921ada9a4887620c583eff95f09e669df8634"
BINARG0=$(bashUTILhex2bin $TESTSTR256_0) 
BINARG1=$(bashUTILhex2bin $TESTSTR256_1)
start=`date +%s`
RESULTFULL=$(bashMULbinstring $BINARG0 $BINARG1)
end=`date +%s`
runtime=$((end-start))
echo "runtime: $runtime"
RESULTHEX=$(bashUTILbin2hex $RESULTFULL)
echo "$RESULTHEX"
The resulting output is:
runtime: 31
838c35fdd0424be26879b9824ca6ad38b245bbb0425da4bdf5e9a51027687da7b7def3037554f1659df8a104630f5afe5bf4a3bdaff5d7c4e3ad6209ee06aff0

Thursday, January 5, 2017

Multiplying large numbers in bash.

In my saga to glue and manipulate results from my simontool program that emulates the hardware of the Simon Cipher, I have now added a multiplication instruction to my bashbignumbers.sh library.
#!/bin/bash
# Required programs:
BIGNUMBERS=bashbignumbers.sh
if [ ! -f $BIGNUMBERS ]; then
    echo "File, $BIGNUMBERS, not found!"
    exit
fi
source "$BIGNUMBERS"
STRBIN2_MUL="0000010001111000"
SSTRIN1_MUL="0001010000000000"
SRESULT_MUL=$(bashMULbinstring $SSTRIN1_MUL $STRBIN2_MUL) 
echo "$STRBIN2_MUL"  
echo "$SSTRIN1_MUL"        
echo "$SRESULT_MUL"  
The resulting output is:
0000010001111000
0001010000000000
000000000010110010110000000000000