Skip to content

Instantly share code, notes, and snippets.

@ningsuhen
Last active March 7, 2020 01:00
Show Gist options
  • Save ningsuhen/7933b206b92fc57364b2 to your computer and use it in GitHub Desktop.
Save ningsuhen/7933b206b92fc57364b2 to your computer and use it in GitHub Desktop.
A small script which will enable copying into local clipboard from remote ssh instance. Based on RemoteCopy.
######################## For SSH Remote Copy #########################
export LC_SETUP_RC='command -v rclip >/dev/null 2>&1 || { echo "executing"; mkdir -p /usr/local/bin; if [ ! -f /usr/local/bin/rclip ];then wget https://raw.githubusercontent.com/justone/remotecopy/master/remotecopy -P /usr/local/bin/; ln -s /usr/local/bin/remotecopy /usr/local/bin/rclip; chmod +x /usr/local/bin/remotecopy; fi; if [[ \":\$PATH:\" == *\"/usr/local/bin:\"* ]]; then export PATH=/usr/local/bin:$PATH; fi } > /var/log/rclip.log 2>&1 || echo "Some error occured in setting up rclip. check /var/log/rclip.log"'
ssh_function() {
count="`ps -eaf | grep remotecopyserver | grep -v grep | wc -l`";
if [ "$count" -eq "0" ]; then
mkdir -p $HOME/bin;
if [ ! -f $HOME/bin/remotecopyserver ]; then
wget https://raw.githubusercontent.com/justone/remotecopy/master/remotecopyserver -P $HOME/bin;
chmod +x $HOME/bin/remotecopyserver;
fi;
nohup $HOME/bin/remotecopyserver &
fi;
ssh_cmd=`which ssh`
PARAMS=""
for PARAM in "$@"
do
PARAMS="${PARAMS} \"${PARAM}\""
done
bash -c "ssh ${PARAMS} -R 12345:localhost:12345 -t 'echo \$LC_SETUP_RC | sudo bash; bash -l'"
}
alias ssho=`which ssh`
alias ssh=ssh_function
alias ssh2=ssh_function
vssh_function() {
ssh_config=`vagrant ssh-config`;
if [ "$?" -eq "1" ]; then
echo "Problem with Vagrant config. run 'vagrant ssh-config' to debug"
return 1
fi
PORT=`echo "$ssh_config" | grep Port | grep -o "[0-9]\+"`;
ID_FILE=`echo "$ssh_config" | grep IdentityFile | awk '{print $2}'`
ssh2 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -i $ID_FILE vagrant@localhost -p $PORT "$@"
}
alias vssh=vssh_function

#Installation Just add the code snippet of add_to_bash_profile to ~/.bash_profile or ~/.bashrc depending on your OS.

##Command Line Usage

rclip "Some String to be copied to your local Clipboard"
Input secret:

Press Cmd+V (copy) or Ctrl+V and press enter.

##Vim Usage Whatever you can pipe to an external command can be copied over to your local clipboard. e.g.

:w !rclip

followed by Cmd+V will copy the whole file contents to your clipboard

:'<,'> !rclip

will copy selected portion (Visual mode) to your clipboard.

#Credits & Implementation details http://endot.org/2011/12/04/remotecopy-copy-from-remote-terminals-into-your-local-clipboard/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment