.bashrc hack to get colours when ssh'ing into remote systems

User avatar
au3
Virgin
Posts: 6
Joined: Tue Apr 01, 2014 1:22 pm

.bashrc hack to get colours when ssh'ing into remote systems

Unread post by au3 » Wed Apr 23, 2014 2:15 am

Can't have .bashrc on Solaris machines I log into (or deviate from ksh as default shell for user), so use a hacky ssh alias to exec bash and declare rcfile, which does the following:
  • - shows colourised uptime
    - shows colourised prompt
    - updates term window title with 'user@system'
    - provides colourised ls alias 'lsd'
In my local .bashrc:

Code: Select all

function ssh_with_bash {
    # Set term name to remote user@server
    echo -en "\033]2;$@\007"

    # For hostnames containing 'sun' declare rcfile on cmdline
    if [[ "$@" == *sun* ]]; then
	# exec bash with rcfile declared on command line, needs to run inside bash to work (hence 2 x "exec bash")
        ssh -X "$@" -t 'exec bash -c "exec bash --rcfile <(echo \"
	# source local crap
	. /etc/profile;
	. ~/.profile;
	export TERM=xterm;
	export PAGER=`which less`;
	# define some colours
	co1=\\\"\[\e[48;5;16m\]\[\e[38;5;19m\]\\\";
	co2=\\\"\[\e[48;5;18m\]\[\e[1m\]\[\e[38;5;7m\]\\\";
	co3=\\\"\[\e[48;5;16m\]\[\e[38;5;76m\]\\\";
	co4=\\\"\[\e[48;5;16m\]\[\e[38;5;47m\]\\\";
	co5=\\\"\[\e[48;5;16m\]\[\e[1m\]\[\e[38;5;23m\]\\\";
	ccl=\\\"\[\e[0m\]\\\";

	# print colourised uptime stats
	echo -en \\\"\e[48;5;16m\e[38;5;19m\\\";
	uptime;

	# use xtermc for colourised vim (requires OpenCSW vim & ncurses package)
	alias vim=\\\"TERM=xtermc vim -u /var/tmp/.vimrc\\\";

	# Alias lsd is a coloured ls -ltrh
	alias lsd='\''ls -ltrh |perl -e '\''\'\'''\''
	while(<>){
		my \\\$c;
		if(/^d/){
		        \\\$c=\\\"\e[48;5;17m\\\";
		} elsif(/^-..x/){
		        \\\$c=\\\"\e[38;5;35m\\\";
		} elsif(/\.(zip|gz|bz2)\\\$/){
		        \\\$c=\\\"\e[38;5;26m\\\";
		} elsif(/^l/){
		        \\\$c=\\\"\e[38;5;28m\\\";
		}
		\\\$c&&s/^((\S+\s+){8})(.*)/\1\\\$c\3\e\[0m/g;
		print \\\"\\\$_\\\";
	}
	'\''\'\'''\'''\'';

	# Function to set prompt
	set_b_p() {
		DATEPR=\\\$(date \\\"+%y%m%d.%H%M%S\\\");
		PS1=\\\"\\\${co1}\\\${DATEPR}\\\${ccl} \\\${co4}\u\\\${ccl}@\\\${co5}\h\\\${ccl} \\\${co2}\w\\\${ccl} \\\${co3}#\\\${ccl} \\\";
	}; 

	# Use function PROMPT_COMMAND as fix for bash history linewrap issues
	PROMPT_COMMAND=set_b_p;
	 \")"'
    else
        ssh -X "$@"
    fi

    # Set term name back to local hostname when logged out
    echo -en "\033]2;${HOSTNAME}\007"
}

alias ssh="ssh_with_bash"
[/color]

Logging in _without_ the above in .bashrc:
Image

Logging in _with_ the above in .bashrc:
Image

Criticism/improvements welcome.

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: .bashrc hack to get colours when ssh'ing into remote sys

Unread post by wuxmedia » Wed Apr 23, 2014 7:06 am

Sexy. I do lots of ssh'ing and the colours brightens up my day!
not onto such shiny Sun boxen.
Welcome to you, sir.
From whence did you spring from?
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: .bashrc hack to get colours when ssh'ing into remote sys

Unread post by dkeg » Wed Apr 23, 2014 11:03 am

Interesting first post. Where as we all appreciate knowledge and experience sharing, thanks for that!, we more so appreciate a nice introduction. Please do...

Work hard; Complain less

Post Reply