Skip to main content Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher


Published: 26-10-2009 | Author: Remy van Elst | Text only version of this article

Table of Contents

I wrote a script which combines dialog, awk and bash to make a nifty ssh script.First of, copy this script to your pc:

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)


#!/bin/bash### Copyright (c) 2010 Remy van Elst#Permission is hereby granted, free of charge, to any person obtaining a copy#of this software and associated documentation files (the "Software"), to deal#in the Software without restriction, including without limitation the rights#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell#copies of the Software, and to permit persons to whom the Software is#furnished to do so, subject to the following conditions:##The above copyright notice and this permission notice shall be included in#all copies or substantial portions of the Software.##THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN#THE SOFTWARE.VERSION="1.2 beta."TITLE="SSHdialog $VERSION"HOSTSFILE="~/hosts"if [ -e $HOSTSFILE ]; thenawk 'NF > 0' $HOSTSFILE > /tmp/sshost.hosts && cat /tmp/sshost.hosts > $HOSTSFILEelseecho "Host file does not exist, please create it"echo "Use the following format:"echo; echo "number,servername,user,url/ip,port"echo "Example:"echo "1,VPS1,-,,2223"echo "2,VPS2,john,,2222"echo "3,Company Webserver,www,,22"echo "4,Router,root,,22"echo; echo "If you want the current user to be used as the username then use a - (dash)."echo "Each value is separated by a , (comma), and make sure you don't have double numbers. If you have only the first number will work."echo "If the syntax is not correct the script will fail so please make sure there are no errors..."echoexit 1fiWIDTH=50HEIGHT=40MENUSIZE=6TEMPF=`TEMPF 2>/dev/null` || TEMPF=/tmp/sshost.$$OLDIFS=$IFSWHICHSSH="`whoami`@${HOSTN[$SSHOST]}"DIALOG=${DIALOG=dialog}IFS=$'n'ALINES=($(awk 'BEGIN{FS=","} {print $1}' $HOSTSFILE | wc -l))LINES=($(awk 'BEGIN{FS=","} {print $1, """ $2 """}' $HOSTSFILE))NAMES=($(awk 'BEGIN{FS=","} {print $2}' $HOSTSFILE))UNAME=($(awk 'BEGIN{FS=","} {print $3}' $HOSTSFILE))HOSTN=($(awk 'BEGIN{FS=","} {print $4}' $HOSTSFILE))HOSTP=($(awk 'BEGIN{FS=","} {print $5}' $HOSTSFILE))# if [ $ALINES -gt 6 ]; then MENUSIZE=$ALINES; else MENUSIZE=6; fiMENUSIZE=$ALINESSMURF="$DIALOG --extra-button --extra-label "Edit Hosts" --cancel-label "Exit" --ok-label "Connect" --menu "$TITLE" $HEIGHT $WIDTH $MENUSIZE ${LINES[*]}"eval $SMURF 2> $TEMPFRHOST=$?KEUZE=`cat $TEMPF`SSHOST=$[$KEUZE - 1]if [ ${UNAME[$SSHOST]} == "-" ]; thenUNAME2=`whoami`WHICHSSH="$UNAME2@${HOSTN[$SSHOST]}"elseUNAME2=${UNAME[$SSHOST]}WHICHSSH="$UNAME2@${HOSTN[$SSHOST]}"ficlearcase $RHOST in0)echo "Connecting user $UNAME2 to ${HOSTN[$SSHOST]} on port ${HOSTP[$SSHOST]}."ssh  $WHICHSSH -p ${HOSTP[$SSHOST]}echo "ssh terminated, byebye"echo "This script is made by Raymii from";;1)echo "You selected exit, we will quit"exit 0;;3)nano $HOSTSFILEexec bash $0exit 0;;255)echo "You pressed ESC, we will exit.";exit 0;;esacrm -f $TEMPFexit 0

Now comes the hard part, creating your hosts file.

The script wants it to be in the same directory as the script with the namehosts. This is the syntax:

ID,Friendly name,username,host or ip,port1,VPS 1,-,,222,VPS 2,www,,22223,Home Router,root,,224,Homeserver,-,,225,Work,ceo,,22 

If you want the current user to be used as the username then use a - (dash) asusername.

Please double check your comma's and your numbers, if something is wrong thescript can give strange errors.

Tags: bash, dialog, ncurses, software, ssh