aboutsummaryrefslogtreecommitdiff
path: root/xclip-ssh
blob: 46c94b3a4d25475b4314ec680ab4ea411522edf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env bash
#
# Intersystem communication via clipboard over SSH!
# Transmits data using the clipboard via SSH session.
#
# Description:
#
# xclip-ssh does what the same says - xclip over SSH.
# Works even with a remote host in a .onion service!
# And even between virtual machines!
#
# Works around the following PoC:
#
# ssh -X <server>      # log in the remote system
# DISPLAY=:0 xclip -o  # extracts remote's clipboard
#
# Usage:
#
# xclip-ssh send    <server> [stdin|data]
# xclip-ssh receive <server> [stdout|xclip]
#
# Examples:
#
# cat /path/to/file | xclip-ssh send server
# xclip-ssh send server "some text"
# xclip-ssh receive server stdout > saved.txt

# Parameters
BASENAME="`basename $0`"
ACTION="$1"
SERVER="$2"
STORE="$3"

# Display usage
function xclip_ssh_usage {
  echo "usage: $BASENAME <send|receive> <server> [data]"
  exit 1
}

# Send data
function xclip_ssh_send {
  if [ -z "$STORE" ]; then
    STORE="xclip"
  fi

  if [ "$STORE" == "xclip" ]; then
    CONTENT="`xclip -o`"
  elif [ "$STORE" == "stdin" ]; then
    mapfile -t CONTENT
    #printf '%s\n' "${CONTENT[@]}"
  else
    shift 2
    CONTENT="$*"
  fi

  # See https://askubuntu.com/questions/804095/how-do-i-disable-the-message-of-the-day-motd-on-ubuntu-14-04
  ssh -q -X $SERVER bash <<EOF
  ##### BEGIN REMOTE SCRIPT #####
  if ! which xclip &> /dev/null; then
    echo "$BASENAME: missing xclip in the remote server"
  else
    echo ${CONTENT[@]} | DISPLAY=:0 xclip
  fi
  ##### END REMOTE SCRIPT #######
EOF
}

# Receive data helper
function xclip_ssh_helper {
  ssh -q -X $SERVER bash <<EOF
  ##### BEGIN REMOTE SCRIPT #####
  if ! which xclip &> /dev/null; then
    echo "$BASENAME: missing xclip in the remote server"
  else
    DISPLAY=:0 xclip -o
  fi
  ##### END REMOTE SCRIPT #######
EOF
}

# Receive data
function xclip_ssh_receive {
  if [ -z "$STORE" ]; then
    STORE="xclip"
  fi

  if [ "$STORE" == "stdout" ]; then
    xclip_ssh_helper
    echo ""
  else
    xclip_ssh_helper | xclip -i
  fi
}

# Check
if [ -z "$2" ]; then
  xclip_ssh_usage
elif [ "$ACTION" != "send" ] && [ "$ACTION" != "receive" ]; then
  xclip_ssh_usage
elif ! which xclip &> /dev/null; then
  echo "$BASENAME: please install xclip"
  exit 1
fi

# Dispatch
xclip_ssh_$ACTION $*