Skip to content

Instantly share code, notes, and snippets.

@mrWinston
Last active October 21, 2015 14:13
Show Gist options
  • Save mrWinston/f0d3569af9b8f0f130ec to your computer and use it in GitHub Desktop.
Save mrWinston/f0d3569af9b8f0f130ec to your computer and use it in GitHub Desktop.
Load a file via scp and open in your favourite editor. afterward, write the file back over scp to the server. #bash #scp #ssh #utility
#!/bin/bash
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://www.wtfpl.net/ for more details.
#some constants
local_meta_postfix=".ssh_meta"
local_meta_prefix="/tmp"
#colors for output
RED='\033[0;31m'
NC='\033[0m' # No Color
#loads the file from ssh and stores it. a metafile is created in /tmp/ to store the
#origin of the file.
function loadFile {
# first parameter: $1 := file to be copied
remote_url=$1
#second parameter : $2 := file name of new file
local_url=$2
#third parameter : $3 := editor-command to be used, default is nano
if [ ! "$3"];then
editor="nano"
else
editor=$3
fi
# first, download the file to the local url and create a metafile, storing
# its location on the server.
scp $remote_url $local_url
error_code="$?"
if [ $error_code != 0 ] # check return code of scp for errors
then
echo -e "${RED}Something went wrong during scp-ing, check console ${NC}"
exit
fi
# create the meta-file
local_name=$(basename $2) # filename of local downloaded file
local_realpath=$(realpath $2) # only the path of the file
local_folder=$(dirname $local_realpath) # only folder
local_meta_file="$local_meta_prefix$local_folder/.$local_name$local_meta_postfix" # full path of the meta-file
local_meta_folder=$(dirname $local_meta_file) # only the folder the meta-file lies in
mkdir -p $local_meta_folder # create directory for meta file in /tmp/
echo $remote_url > $local_meta_file #store the remote file url in the meta-file
echo "Stored remote File in $local_realpath"
$editor $local_realpath # open local file in editor
}
#save the file with the given name back to the remote server if a metadata file is found
function saveFile {
#first parameter: fileName.
local_file=$1
local_name=$(basename $1)
local_realpath=$(realpath $1)
local_folder=$(dirname $local_realpath)
local_meta_file="$local_meta_prefix$local_folder/.$local_name$local_meta_postfix"
remote_url=$(cat $local_meta_file)
if [ ! "$remote_url"] # if file is not found or empty, print error message
then
echo "metafile $local_meta_file not found"
else
scp $local_file $remote_url
echo "saved file on remote server: $remote_url"
fi
}
if [ $1 == "-l" ]
then
loadFile $2 $3 $4
elif [ $1 == "-s" ]
then
saveFile $2
else
echo -e "usage:\t sshFileAccess -l <user@remotelocation:filepath> <location/to/store/file> <editor-to-be-used> # load file"
echo -e ' \t sshFileAccess -s <location/to/store/file> # store file again'
exit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment