Copiare del testo dalla clipboard locale in una sessione ssh e viceversa

From RVM Wiki
Jump to navigation Jump to search
  • Per copiare del testo da una sessione ssh agli appunti locali, si può usare
xclip
  • Installare xclip
ssh mymachine
sudo apt-get install xclip xauth
  • Testare
echo PIPPO | xclip
  • Xclip di default copia nel buffer XA_PRIMARY, quindi incollare in locale con
MIDDLE_CLICK
  • Se si vuole copiare nel buffer XA_CLIPBOARD, usare:
 echo PIPPO_CLIPBOARD | xclip -selection clipboard
  • In questo modo si può incollare con
CTRL-V
SHIFT-INS
Edit...Paste
  • Per comodità, è possibile definire le seguenti functions in .bashrc
cat >> ~/.bashrc <<'EOFile'

# ------------------------------------------------
# A shortcut function that simplifies usage of xclip.
# - Accepts input from either stdin (pipe), or params.
# ------------------------------------------------
cb() {
	local _scs_col="\e[0;32m"
	local _wrn_col="\e[1;31m"
	local _trn_col="\e[0;33m"
	
	# Check that xclip is installed.
	if ! type xclip > /dev/null 2>&1
	then
		echo -e "$_wrn_col""You must have the 'xclip' program installed.\e[0m"
		sudo apt-get -y install xclip
	# Check user is not root (root doesn't have access to user xorg server)
	elif [[ "$USER" == "root" ]]
	then
		echo -e "$_wrn_col""Must be regular user (not root) to copy a file to the clipboard.\e[0m"
  	else
		# If no tty, data should be available on stdin
		if ! [[ "$( tty )" == /dev/* ]]
		then
			input="$(< /dev/stdin)"
		# Else, fetch input from params
		else
			input="$*"
		fi
		if [ -z "$input" ]; then  # If no input, print usage message.
			echo "Copies a string to the clipboard."
			echo "Usage: cb <string>"
			echo "   echo <string> | cb"
		else
			# Copy input to clipboard
			echo -n "$input" | xclip -selection c
			# Truncate text for status
			if [ ${#input} -gt 80 ]
			then
				input="$(echo $input | cut -c1-80)$_trn_col...\e[0m"
			fi
			# Print status.
			echo -e "$_scs_col""Copied to clipboard:\e[0m $input"
		fi
  	fi
}
# Aliases / functions leveraging the cb() function
# ------------------------------------------------
# Copy contents of a file
function cbfile() { cat "$1" | cb; } 
# Copy current working directory
alias cbpwd="pwd | cb"  
# Copy most recent command in bash history
alias cbhist="tail -n 1 $HISTFILE | cb"
EOFile
  • Per copiare una stringa:
echo PIPPO | cb
  • Per copiare un file:
cbfile filename
  • Per copiare l'ultimo comando:
cbhist

Utilizzo via SSH

  • Assicurarsi di attivare il forwarding X11 nella connessione SSH sulla macchina locale:
echo ForwardX11 yes >> ~/.ssh/config
  • Sulla macchina remota, verificare che ci sia attivo il forwarding X11:
sudoedit /etc/ssh/sshd_config
X11Forwarding yes
X11UseLocalhost no
sudo invoke-rc.d ssh restart 
  • Installare xauth sulla macchina remota
sudo apt-get install xauth
  • Installare xclip sulla macchina remota:
sudo apt-get install xclip
  • Lo si può utilizzare come se fosse locale
  • Abilitare anche i comandi cb

Riferimenti