Wednesday, November 15, 2023

SSH wrapping for autocompletion hostnames and domain

 intelligent ssh username and host completion


ssh u1106089
ssh u1106089.west.test.company.com
ssh p1106089
ssh p1106089.west.prod.company.com


Add this to your .zshrc or .bashrc and create the $HOME/sshwrap.sh and make that executable chmod 755 sshwrap.sh

ssh() {
if [[ "$0" == "ssh" ]]; then
_host=$1
shift
. ~/sshwrap.sh $_host $@
fi
}



The contents for sshwrap.sh

#!/bin/bash
_host="$1"
shift
if [[ $(echo "$_host" | grep "^p" | wc -l ) -eq 1 ]]
then
#xprod
if [[ $(echo "${_host}" | grep "prod.cloud.com" | wc -l ) -eq 1 ]]
then
_host="${_host}.prod.cloud.com"
fi
if [[ $(echo "$_host" | grep "^f751818" | wc -l) -eq 0 ]]
then
_host="ff751818@${_host}"
fi
echo "connecting production syste, $_host"
elif [[ $(echo "$_host" | grep "^u" | wc -l ) -eq 1 ]]
then
if [[ $(echo "$_host" | grep test.cloud.com | wc -l ) -eq 0 ]]
then
_host="${_host}.test.cloud.com"
fi
if [[ $(echo "$_host" | grep "^ff751818" | wc -l ) -eq 0 ]]
then
_host="ff751818@${_host}"
fi
echo "connecting test system, $_host"
elif [[ $(echo "$_host" | grep "^c" | wc -l) -eq 1 ]]
then
if [[ $(echo "$_host" | grep test.cloud.com | wc -l ) -eq 0 ]]
then
_host="${_host}.test.cloud.com"
fi
if [[ $(echo "$_host" | grep "^ak751818" | wc -l ) -eq 0 ]]
then
_host="ak751818@${_host}"
fi
echo "connecting test system on premises, $_host"
else
echo "other host definition $_host"
fi
echo "contacting $_host $@"
/usr/bin/ssh $_host $@