User Tools

Site Tools


en:linux:scripts:connect

/home/<username>/bin/connect

Utility to mount remote disks.

#!/bin/bash

# configuration
MNTDIR="/mnt"
SSHFS_OPTIONS="-o follow_symlinks -o workaround=rename -o reconnect"

function AddMount()
{
	n=${#mountPoints[*]}
	#echo $n $1 $2
	mountPoint="$1"
	host="$2"
	mountPoints[$n]="$mountPoint"
	hosts[$n]="$host"
	shift
	shift

	labels[$n]="$@"
}

AddMount "<hostname>"		"<username>@<hostname>:/home/<username>"	c calc 31
AddMount "<hostname>"		"<username>@<hostname>:/home/<username>"	c calc 30
AddMount "<hostname>"		"xdaq@<hostname>.domain.com:/home/xdaq" 08
AddMount "o-totmon02"		"<username>@o-totmon02.domain.com:/home" m
AddMount "ttf20"			"<username>@ttf20.domain.com:/home/<username>" t
AddMount "harp"				"<username>@pcoharp106.domain.com:/home/<username>" h
AddMount "fzu"				"<username>@samson.fzu.cz:" fzu
AddMount "artax"			"<username>@domain.com:/home/<username>" a artax
AddMount "<hostname>"		"monitor@<hostname>.domain.com:/home/monitor" 33
AddMount "<hostname>"		"monitor@<hostname>.domain.com:/home/monitor" 34
AddMount "cmsusr0"			"<username>@cmsusr0.domain.com:/nfshome0/<username>" cms
AddMount "<hostname>"		"<username>@<hostname>.domain.com:/" 40
AddMount "<hostname>"		"dqm@<hostname>.domain.com:/home/dqm" 43
AddMount "cr8"				"rp@totem-cr-08:/home/rp" cr cr8

keys=${#hosts[*]}

# get choice
if [ -n "$1" ]
then
	ans="$1"
else
	# print menu
	i=0
	while [ "$i" -lt "$keys" ]
	do
		title=${hosts[$i]}
		title=${title#*@}
		title=${title%%.*}
		echo "${labels[$i]} ... $title"
		let "i+=1"
	done
	
	# get answer and set it as positional parameters
	echo -n "choice (empty for none): "
	read ans
	if [ -n "$ans" ]
	then
		set $ans
	fi
fi

# go through all choices
disconnect=0
verbose=0
until [ -z "$1" ]
do
	# retrieve key
	key=$1

	if [ "$key" == "--home" ]
	then
		SSHFS_OPTIONS="$SSHFS_OPTIONS -o ssh_command=ssh_cern"
		shift
		continue
	fi
	
	if [ "$key" == "--cms" ]
	then
		SSHFS_OPTIONS="$SSHFS_OPTIONS -o ssh_command=ssh_cmsusr0"
		shift
		continue
	fi
	
	if [ "$key" == "--v" ]
	then
		verbose=1
		shift
		continue
	fi

	# check for minus sign
	if [ "${key:0:1}" == "-" ]
	then
		disconnect=1
		key="${key:1}"
	fi

	# find index
	j=0
	found=0
	index=$keys
	while [ "$j" -lt "$keys" ]
	do
			keyarray=(${labels[$j]})
			k=0
			while [ "$k" -lt ${#keyarray[*]} ]
			do
				if [ "${keyarray[$k]}" == "$key" ]
				then
					index=$j
					found=1
					break
				fi
				let "k+=1"
			done

			if [ "$found" -gt 0 ]; then break; fi

			let "j+=1"
	done

	if [ "$found" -gt 0 ]
	then
		# get the mount dir
		dir=${mountPoints[$index]}
		if [ "${dir:0:1}" != "/" ]
		then
			dir="$MNTDIR/$dir"
		fi

		host=${hosts[$index]}

		if [ "$disconnect" -eq "0" ]
		then
			echo -e "\e[30;34mconnecting '$host' to '$dir'\e[0m"
			command="sshfs $SSHFS_OPTIONS $host $dir"
			if [ ! $verbose -eq 0 ]; then echo $command; fi
			$command
		else
			echo -e "\e[30;34mdisconnecting '$host from '$dir'\e[0m"
			command="fusermount -u $dir"
			if [ ! $verbose -eq 0 ]; then echo $command; fi
			$command || echo "Try 'killall -9 sshfs' before 'fusermount -u ...', in case the sshfs got stuck."
		fi
	else
		echo -e "\e[30;31m'$key' is unknown key. Omitted.\e[0m"
	fi

	# reset disconnect flag
	disconnect=0;
	
	# move on
	shift
done
en/linux/scripts/connect.txt · Last modified: 2011/08/05 10:23 (external edit)