#!/bin/bash # sshconf - ssh servers configuration utility - version 1.1 - July 11 # Copyright 2011 Giuseppe Cuccu, http://www.idsia.ch/~giuse all rights reserved # Licensed under CC BY-SA 3.0 http://creativecommons.org/licenses/by-sa/3.0/ # This script is provided AS I USE IT and under constant updating - NO WARRANTY # # Configure the variables below (or activate the interactive interface) # Provide an output file as argument or manually redirect stdout to file # # User name is supposed to be the same on all servers, as often the case. # Empty the proxy field to disable port forwarding configuration (VPN SSH tunneling) # # Outputs to stdout (redirect this from shell); alternatively, pass it a file name as argument. interactive="no" # NAME URL svlst=" salvia salvia.idsia.ch copernicio copernicio.idsia.supsi.ch ares ares.dti.supsi.ch " proxy=" bastion bastion.idsia.ch " user="cuccu" port="22" fwdbaseport="20000" fwdname="b" #################################################################################### # If a file name is provided, write output to it if [ $# -gt 0 ]; then if [ -f ${1} ]; then mv ${1} ${1}.bkp echo "A backup of ${1} has been made in ${1}.bkp." 1>&2 fi exec 1>${1} fi # INTERACTIVE INTERFACE if [ ${interactive} == "yes" ] then svlst=$(zenity --entry --title="Server names" --text="Enter server names" --entry-text="${svlst}") user=$(zenity --entry --title="User name" --text="Enter user name" --entry-text="${user}") port=$(zenity --entry --title="Port number" --text="Enter port to use" --entry-text="${port}") fi confblock(){ # ${1}: shortname ; ${2}: server ; ${3}: user ; ${4}: port; echo " " echo "Host ${1}" echo "HostName ${2}" echo "User ${3}" echo "Port ${4}" } svblock(){ # $1: server name ; $2: server url confblock "${1}" "${2}" "${user}" "${port}" } fwdblock(){ # $1: server name fwdport=$(( ${fwdport} + 1 )) confblock "${1}${fwdname}" "localhost" "${user}" "${fwdport}" } fwdline(){ # $1: url of server to forward fwdport=$(( ${fwdport} + 1 )) echo "LocalForward ${fwdport} ${1}:${port}" } aliasline(){ # $1: server name sv=${1} set - $(echo ${proxy}) echo "alias ${sv}${fwdname}='ssh -Nfq ${1}; ssh ${sv}${fwdname}'" \ >&2 #>> ${HOME}/.bashrc } # Set server entries #for sv in ${svlst}; do svblock "${sv}"; done set - $(echo ${svlst}) while [ $# -gt 0 ]; do svblock ${1} ${2} shift 2 done # If a proxy has been defined, set local port forwarding if [ -n "${proxy}" ] then svblock ${proxy} fwdport="${fwdbaseport}" # for sv in ${svlst}; do fwdline "${sv}"; done set - ${svlst} while [ $# -gt 0 ]; do fwdline ${2} shift 2 done # Set entries for forwarded ports fwdport="${fwdbaseport}" # for sv in ${svlst}; do fwdblock "${sv}"; done set - ${svlst} while [ $# -gt 0 ]; do fwdblock ${1} shift 2 done fi set - $(echo ${svlst}) echo -e "\nAppend the following to .bashrc for an handy vpn connection shortcut:\n" >&2 while [ $# -gt 0 ]; do aliasline ${1} shift 2 done echo -e "\n" echo -e "\n\tDone!\n" >&2 # TODO: # - use arrays instead of "set -"