Copiare del testo dalla clipboard locale in una sessione ssh e viceversa
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
- Copy Shell Prompt Output To Linux / UNIX X Clipboard Directly
- A better way to use xclip (clipboard from the command line) | Made by Nathan
- xorg - Copy input to clipboard over SSH? - Unix and Linux - Stack Exchange
- Steven Occhipinti: Copy and Paste over SSH with Xclip
- XSel by Conrad Parker
- console - copy and paste from terminal with keyboard in linux (ubuntu) - Stack Overflow
- X11 forwarding request failed on channel 0 Error and Solution