logo

sshpaste

Simple paste script to a server having SSH+HTTP daemons git clone https://hacktivis.me/git/sshpaste.git

sshpaste (2865B)


  1. #!/bin/sh
  2. # Copyright 2018 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
  3. # Distributed under the terms of the ISC license
  4. datetime="$(date -u +%FT%T)"
  5. target_ssh="cloudsdale.hacktivis.me:pastes/${datetime}"
  6. target_www="https://cloudsdale.hacktivis.me/pastes/${datetime}"
  7. die() {
  8. if [ -n "$*" ]
  9. then echo "${*}, exiting…" >&2
  10. else echo "Failed, exiting…" >&2
  11. fi
  12. if [ -e "${temporary_file}" ]
  13. then
  14. rm -f "${temporary_file}"
  15. fi
  16. exit
  17. }
  18. sshpaste_file() {
  19. [ -s "$1" ] || die "$1 is empty"
  20. target_name=$(basename "$1")
  21. chmod +r "$1"
  22. file -i "$1" 2>/dev/null | grep 'text/' && target_name="${target_name}.txt"
  23. if scp -q "$1" "${target_ssh}-'${target_name}'"
  24. then echo "${target_www}-${target_name}"
  25. else die "Failed uploading $1"
  26. fi
  27. }
  28. sshpaste_xclip() {
  29. temporary_file="$(mktemp -t sshpaste_xclip.XXXXXXXX)"
  30. xclip -o > "${temporary_file}" || die "xclip output"
  31. sshpaste_file "${temporary_file}"
  32. rm -f "${temporary_file}"
  33. }
  34. sshpaste_command() {
  35. temporary_file="$(mktemp -t sshpaste_command.XXXXXXXX)"
  36. sh -x -c "$*" > "${temporary_file}" 2>&1
  37. #echo "${PS4}$*" > "${temporary_file}"
  38. #sh -c "$*" >> "${temporary_file}" 2>&1
  39. sshpaste_file "${temporary_file}"
  40. rm -f "${temporary_file}"
  41. }
  42. sshpaste_stdin() {
  43. temporary_file="$(mktemp -t sshpaste_stdin.XXXXXXXX)"
  44. tty 2&1 >/dev/null && echo "Reading from stdin, press EOF(^D) when you're done"
  45. cat - > "${temporary_file}" || die "reading from stdin"
  46. sshpaste_file "${temporary_file}"
  47. rm -fr ${temporary_file}
  48. }
  49. sshpaste_usage() {
  50. cat >&2 <<EOF
  51. Usage: ${1} [options] [file(s)]
  52. Options:
  53. -c COMMAND paste COMMAND and the output of COMMAND
  54. Can be used only once, not compatible with -x
  55. -h show this help
  56. -x read input from clipboard (requires xclip)
  57. Can be used only once, not compatible with -c
  58. When no options or files are given, ${1} reads from stdin, it assumes plain text is given and so puts a .txt extension
  59. EOF
  60. }
  61. flag_x=
  62. flag_c=
  63. temporary_file=
  64. while getopts "xhc:" opt
  65. do
  66. case $opt in
  67. 'c')
  68. [ ! -z $flag_x ] && die "Cannot have both -x and -c"
  69. [ ! -z $flag_c ] && die "Cannot have two -c, use ; if you want multiple commands"
  70. flag_c=1
  71. opt_command="${OPTARG}"
  72. ;;
  73. 'h')
  74. sshpaste_usage $0
  75. exit 0
  76. ;;
  77. 'x')
  78. [ ! -z $flag_c ] && die "Cannot have both -x and -c"
  79. flag_x=1
  80. ;;
  81. '?')
  82. sshpaste_usage $0
  83. exit 2
  84. ;;
  85. ':')
  86. echo "Option -${OPTARG} requires an argument, exiting…" >&2
  87. exit 1
  88. ;;
  89. esac
  90. done
  91. if [ ! -z "${flag_x}" ]
  92. then
  93. sshpaste_xclip
  94. elif [ ! -z "${opt_command}" ]
  95. then
  96. sshpaste_command ${opt_command}
  97. elif [ "$#" -eq 0 ]
  98. then
  99. sshpaste_stdin
  100. else
  101. # try to upload the rest of the arguments if they are files
  102. shift $(($OPTIND - 1))
  103. for file in "$@"
  104. do test -n "$file"
  105. if [ "$file" == '-' ]
  106. then sshpaste_stdin
  107. else sshpaste_file "$file"
  108. fi
  109. done
  110. fi