Importing bash functions over SSH

Liam Stanley
Published 11 years ago

Ever wanted to import or carry over custom made functions and aliases over SSH, when logging into other desktop computers, and servers? Well, here's how I do it.

Note: This can pose a large security concern. I would highly ensure that the location you are logging in from is completely secure before implementing this. It has the full fledged functionality of running anything on the remote server. Regardless of how safe it is.

Installing the sshrc script

Simply start off by taking the below script, and dropping it into /usr/local/bin/sshrc (or your equivalent $PATH location), and running chmod +x /usr/local/bin/sshrc.

 1#!/usr/bin/env bash
 2
 3function sshrc {
 4    local SSHHOME=${SSHHOME:=~}
 5    if [ ! -f $SSHHOME/.sshrc ];then
 6        echo "No such file: $SSHHOME/.sshrc"
 7        exit 1
 8    fi
 9
10    local sshrcfile=.sshrc
11    SIZE=$(tar cz -h -C $SSHHOME .sshrc | wc -c)
12    if [ $SIZE -gt 65536 ];then
13        echo >&2 $'.sshrc must be less than 64kb\ncurrent size: '$SIZE' bytes'
14        exit 1
15    fi
16    ssh -t "$@" "
17        command -v xxd > /dev/null 2>&1 || { echo >&2 \"sshrc requires xxd to be installed on the server, but it's not. Aborting.\"; exit 1; }
18        export SSHHOME=/tmp;echo $'"$(cat "$0" | xxd -p)"' | xxd -p -r > \$SSHHOME/sshrc;chmod +x \$SSHHOME/sshrc
19
20        echo $'"$( cat << 'EOF' | xxd -p
21if [ -e /etc/etc/profile ];then source /etc/profile;fi
22if [ -e /etc/bashrc ];then source /etc/bashrc;fi
23if [ -e /etc/bash.bashrc ];then source /etc/bash.bashrc;fi
24if [ -e ~/.bashrc ];then source ~/.bashrc;fi
25export PATH=$PATH:$SSHHOME;source $SSHHOME/.sshrc;
26cat $SSHHOME/.sshrc | sed -rn "s:function ([a-zA-Z]+) .*:\1:p" | while read fnc;do export -f $fnc;done > /dev/null 2>&1
27rm -f $SSHHOME/.post-sshrc $SSHHOME/.sshrc $SSHHOME/sshrc > /dev/null 2>&1 || echo "Unable to cleanup sshrc"
28EOF
29        )"' | xxd -p -r > \$SSHHOME/.post-sshrc
30        echo $'"$(tar cz -h -C $SSHHOME .sshrc | xxd -p)"' | xxd -p -r | tar mxz -C \$SSHHOME
31        export SSHHOME=\$SSHHOME;bash --rcfile \$SSHHOME/.post-sshrc
32    "
33}
34if [ $1 ];then
35    command -v xxd >/dev/null 2>&1 || {
36        echo >&2 "sshrc requires xxd to be installed locally, but it's not. Aborting."
37        exit 1
38    }
39    sshrc $@
40else
41    ssh
42fi

Aliasing ssh

Since the functionality is useful for every server I login to, I alias ssh to the above command. As such, I simply open up my ~/.bashrc file, and add the following (be sure to modify this if you stored the sshrc script in another location):

1function ssh() {
2    /usr/local/bin/sshrc -oStrictHostKeyChecking=no $@
3    clear
4}

The above will ensure running ssh <args> passes over to sshrc <args>. I've added -oStrictHostKeyChecking=no to the above function, as this prevents me from having to accept public keys being added to my workstation every time I connect to a server, or if the public key changes. If you still want this, simply remove the section outlined beforehand.

Setting up the scripts to import

The simplest way to get started, is simply creating a file stored at ~/.sshrc.

1touch ~/.sshrc

Where from here?

Well, there are many things you could add to be extremely helpful in real world situations.


For example, a simple password generating function

Function:

1function genpw { < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-15};echo; }

Usage:

1$ genpw
2cxD3i4_1gtGaEkn
3$ genpw
4Hy9VopX5nnV8i2r
5$ genpw
6oqdh7HplyInBLVz

A geoip one liner, that adds GeoIP information to anything passed to it

Function:

1function geoip { while IFS= read x;do ip=$(echo $x | egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}");if [ -n "$ip" ];then location=$(curl -s geoip.pw/api/$ip/country);echo "$x" | sed -r "s#$ip#$ip ($location)#g"; else echo "$x";fi;done; }

Usage:

1$ cat /home/domlogs/liamstanley.io | awk '{print $1}' | tail -5 | geoip
254.149.154.72 (United States)
3198.190.131.2 (United States)
4207.46.13.40 (United States)
5201.250.35.137 (Argentina)
6187.87.199.209 (Brazil)

Disable SSH within the server you are SSH'd into

Function:

1function ssh { echo -e "\n\n\e[0;36mSSH has been disabled, you are daisychaining!\e[m\n\n"; }

Usage:

1$ ssh [email protected]
2
3
4SSH has been disabled, you are daisychaining!

Pull human-readable file modes

Function:

1function mode { while read filename;do echo "($(if [ -f "$filename" ];then stat -c '%a' "$filename";else echo "---";fi)) $filename";done; }

Usage:

1$ find /home/liam/public_html/test -maxdepth 1 -type f | mode
2(644) /home/liam/public_html/test/config.default.php
3(644) /home/liam/public_html/test/index.php
4(644) /home/liam/public_html/test/config.php
5(644) /home/liam/public_html/test/readme.md

There are simply too many possibilities here, that I cannot grasp as of writing this. Some day I may just simply integrate the function into my site to show all the bash functions I use, to share with others. For now, here you go.

Post Labels
Made with by lrstanley