Skip to content

Instantly share code, notes, and snippets.

@criztovyl
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save criztovyl/4683cb3434268dbd19bf to your computer and use it in GitHub Desktop.
Save criztovyl/4683cb3434268dbd19bf to your computer and use it in GitHub Desktop.
Use server simplified.
#!/bin/bash
############
#===Simple Server Skript===
# Author Christoph Schulz <cs@chsc.de>
# Version of 06 Apr 2013
#
#==INFORMATION==
# This skript uses SFTP only!
#==Configuration==
# - server: The server, e.g. ssh.example.com
server=''
###
# - user: The remote user.
# Leave blank to use current user
user=''
###
# - key: SSH key for the account
# Leave blank to use key ~/.ssh/id_rsa
key=''
###
# - sshport: Set the SSH port. Leave blank to use default port.
sshport=''
###
# - sshfspath: Path to the point where the remote directories should be mounted.
# Leave blank to use directory ~/sshfs/(user)/(server)/(path)/(on)/(server)
sshfspath=''
###
# - sshfsoptions: Extra options for SSHFS, e.g. follow_symlinks
# Different SSH Port is set @sshport and automatically added here.
# Use Format option=value,option2=value2,option
sshfsoptions=''
#==Comments==
#
#
#
############
# Do not edit below this line! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#Change to Home
cd ~
#Checks if $server is empty
if [ -z $server ]; then
echo 'No server given!'
echo 'Please add a server to the configuration included in the script.'
exit 0
fi
#Checks if $user is emtpy
if [ -z $user ]; then
#Set $user to current user
user=$USER
fi
#Checks if the key is empty
if [ -z $key ]; then
#Set $key to default file name
key=$HOME'/.ssh/id_rsa'
fi
#Set name for (temporary) ftp bath file
batch=$server'.ftpbatch.temp'
#Set up the address connection to
address=$user'@'$server
#Set SSHFS Path to default if empty
if [ -z $sshfspath ]; then
sshfspath='sshfs/'$user'/'$server
fi
case $1 in
#SSH; Standart Option; use following argument as remote command
ssh|'')
if [ -z $sshport ];then
#Use standart port; -t for remote tty, eg. for sudo
ssh -ti $key $address $2
else
#Use different port
ssh -p $sshport -ti $key $address $2
fi
;;
#SFTP
ftp|sftp)
#Options for the sftp command
options=''
if [ ! -z $sshport ];then
#Set different port
options='-P '$sshport
fi
case $2 in
#If there is no next argument start SFTP client console
'')
sftp $options -i $key $address
;;
#If there is a next argument use it as command
'string')
echo $3 > $batch
sftp $options -i $key -b $batch $address
rm $batch
;;
#If there is a next argument use this as SFTP batch file name
'batch')
sftp $options -i $key -b $3 $address
;;
esac
;;
#Explicit no X11Forwarding
'ssh-no-x')
#Check if should use different SSH port
if [ -z $sshport ]; then
ssh -xti $key $address $2
else
#Using different SSH port
ssh -p $sshport -txi $key $address $2
fi
;;
#Mount a remote directory via sshfs
'sshfs')
#Check if a remote path is given
if [ -z $2 ]; then
echo "A Path is required!"
exit 0
fi
#Store remote path
path=$2
#Set Remote Path to Remote Home if Path is empty
if [ -z $path ]; then
path=`./netcup ssh 'echo $HOME'`
fi
#Append Remote path to $sshfspath
if [ ${path:0:1} == '/' ]; then
sshfspath=$sshfspath$path
else
#Add a ´/´ to the begin of $path if not present
sshfspath=$sshfspath'/'$path
fi
#Check for unmount
if [ ! -z $3 ]; then
if [ $3 == '-u' ]; then
#Unmount
fusermount $sshfspath -u
exit 0
fi
fi
#Create target directory if not exist
if [ ! -d $sshfspath ]; then
mkdir -p $sshfspath
fi
#Adds Identity File to options
if [ -z $sshfsoptions ]; then
#No sshfs options present
sshfsoptions='IdentityFile='$key
else
#sshfs options present
sshfsoptions='IdentityFile='$key','$sshfsoptions
fi
#Check if should use different SSH Port
if [ -z $sshport ];then
#normal
sshfs $address':'$path $sshfspath -o $sshfsoptions
else
#use different port
sshfs $address':'$path $sshfspath -p $sshport -o $sshfsoptions
fi
;;
"help")
echo './FILE : Open a SSH Connection to $server with user '$user
echo './FILE ssh \"[CMD]\" : Executing the Command [CMD] on '$server
echo './FILE ftp : Open a FTP Connection to '$server' with '$user
echo './FILE ftp string \"[STRING]\" : Use [STRING] as FTP Command(s)'
echo './FILE ftp batch \"[BATCH]\" : Use a Batch File [BATCH]'
echo './FILE ssh-no-x : Disable X11 Forwarding for SSH Connection (if enabled in ssh_conf)'
echo './FILE sshfs \"[PATH]\" : Mount the Remote [PATH] to '$sshfspath'/[PATH]'
echo './FILE sshfs \"[PATH]\" -u : Unmount Remote [PATH] from '$sshfspath'/[PATH]'
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment