v1ral_ITS

amazing cat files output directly to pastebin [ yourS! ]]

Apr 19th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.15 KB | None | 0 0
  1. #    v1ral_ITS
  2. #*  .bashrc OR .zshrc Alias for awesome terminal cat output anyfile to pastebin
  3. ##   https://pastebin.com/u/v1ral_ITS
  4.  
  5. #    .*rc function alias
  6. #*   needed default pastebin script and default pastebin.conf are lsted after
  7.  
  8. #//+ ITS_ function to easily check the,
  9. #// operating systems presense of any needed package.
  10.  
  11. #/#/ Define any tools needed here seperated only by a "\:space:"\
  12. needed_tool=' pastebin xsel echo cat printf '
  13.  
  14. #//* Begin System Tool Search
  15. require_tools ()
  16. {
  17.     local NOT_AVAIL=""
  18.     for TOOL in $needed_tool; do
  19.         if [ "`which $TOOL 2> /dev/null`" == "" ];
  20. then NOT_AVAIL="$NOT_AVAIL $TOOL"; fi
  21.     done
  22.     if [[ "$NOT_AVAIL" != "" ]]; then
  23.         echo "ERROR: The following required tool(s)
  24. cannot be found: $NOT_AVAIL"
  25.         exit 3
  26.     fi
  27. }
  28.  
  29. # Check file system tools before we start
  30. require_tools
  31.  
  32.  
  33. pbin_this() {
  34.     file_this=$*
  35.     (\cat $file_this | pastebin) | xsel -i --clipboard
  36.    
  37.     printf '\033[033mPASTEBINS_NEW_URL: \033[m'
  38.     echo `xsel --clipboard`
  39. }
  40.  
  41.  
  42. ################################
  43. # needed pastebin shell script #
  44. ################################
  45. #!/bin/bash
  46.  
  47. # bash pastebin.com client
  48. # https://github.com/v1ral-ITS/pastebin
  49.  
  50. # Path to config file
  51. config_file="$HOME/bin/pastebin.conf"
  52.  
  53. # Config file overwrites following default settings
  54.  
  55. # This is my API Developer Key (you can change it here or use config file)
  56. api_dev_key=""
  57. # Autologin
  58. logintopastebin="1"
  59.  
  60. # Default paste title, if -n argument is not defined
  61.  
  62.  
  63. # Default paste highlighting, if -f argument is not defined
  64. api_paste_format="bash"
  65.  
  66. # Default private settings, if -p argument is not defined (0=public, 1=unlisted, 2=private)
  67.  
  68.  
  69. # Default expire date, if -e argument is not defined
  70. api_paste_expire_date="N"
  71.  
  72. # Pastebin.com API URL
  73. api_url="http://pastebin.com/api"
  74.  
  75. usage()
  76. {
  77. cat << EOF
  78. Usage: $0 [options]
  79. Post text from stdin to pastebin.com.
  80.  
  81. Options:
  82.     -l              Log into pastebin.com
  83.     -u              Your pastebin.com username
  84.     -p              Your pastebin.com password
  85.     -n name             Paste title
  86.     -f format           Syntax highlightig format (you can find list
  87.                     of formats at http://pastebin.com/api#5)
  88.     -e expire           Paste expire time:
  89.                       n (or never) - never
  90.                       10M          - 10 minutes
  91.                       1H           - 1 hour
  92.                       1D           - 1 day
  93.                       1M           - 1 month
  94.     -r private          Private settings: public (or 0), unlisted (or 1)
  95.                     or private (or 2, need to login)
  96.     -h              Show this message
  97.     -o              Print sample config
  98.     -c /path/to/pastebin.conf   Set path to config file
  99. EOF
  100. }
  101.  
  102. # command_exists() tells if a given command exists.
  103. function command_exists() {
  104.     command -v "$1" >/dev/null 2>&1
  105. }
  106.  
  107. sample_config()
  108. {
  109. cat << EOF
  110. # Change to your API Developer Key
  111. api_dev_key=""
  112.  
  113. # Change to your pastebin.com username, if you want to log into your account
  114. api_user_name="v1ral_ITS"
  115.  
  116. # Change to your pastebin.com password
  117. api_user_password="**********"
  118.  
  119. # Set to 1 if you want to log into your account automatically
  120. # (you may login with -l, -u, -p arguments also)
  121. logintopastebin="1"
  122.  
  123. # Default paste title, if -n argument is not defined
  124. #api_paste_name=""
  125.  
  126. # Default paste highlighting, if -f argument is not defined
  127. # (see http://pastebin.com/api#5 for more)
  128. #api_paste_format="bash"
  129.  
  130. # Default private settings, if -p argument is not defined
  131. # (0=public, 1=unlisted, 2=private)
  132. #api_paste_private="0"
  133.  
  134. # Default expire date, if -e argument is not defined
  135. #api_paste_expire_date="N"
  136. EOF
  137. }
  138.  
  139. auth_user()
  140. {
  141.     if [ -z "$api_user_name" ] || [ -z "$api_user_password" ]
  142.     then
  143.         echo "Username and/or password is undefined."
  144.         echo "Please adjust 'api_user_name' and 'api_user_password' in the configuration file '$config_file'"
  145.         exit 1
  146.     fi
  147.  
  148.     server_reply="$(curl --silent --data "api_dev_key=$api_dev_key" --data-urlencode "api_user_name=$api_user_name" --data-urlencode "api_user_password=$api_user_password" "$api_url/api_login.php")"
  149.  
  150.     if [[ $server_reply == Bad* ]]
  151.     then
  152.         echo "Username: $api_user_name"
  153.         echo "$server_reply"
  154.         exit 1
  155.     else
  156.         api_user_key="$server_reply"
  157.     fi
  158. }
  159.  
  160. if ! command_exists curl; then
  161.     echo "'curl' is needed. Please install 'curl'. More details can be found at https://curl.haxx.se/"
  162.     exit 1
  163. fi
  164.  
  165. # Load configuration file if it exists.
  166. if [ -f "$config_file" ]; then
  167.     # shellcheck source=/dev/null
  168.     source "$config_file"
  169. fi
  170.  
  171. while getopts "hu:p:ln:f:c:e:r:o" OPTION ; do
  172.   case $OPTION in
  173.     h) usage; exit ;;
  174.     o) sample_config; exit ;;
  175.     u) api_user_name="$OPTARG"; logintopastebin=1 ;;
  176.     p) api_user_password="$OPTARG" ;;
  177.     l) logintopastebin=1 ;;
  178.     n) api_paste_name="$OPTARG" ;;
  179.     f) api_paste_format="$OPTARG" ;;
  180.     e) case $OPTARG in
  181.         n|never) api_paste_expire_date=N ;;
  182.         10M) api_paste_expire_date=10M ;;
  183.         1H) api_paste_expire_date=1H ;;
  184.         1D) api_paste_expire_date=1D ;;
  185.         1M) api_paste_expire_date=1M ;;
  186.         1W) api_paste_expire_date=1W ;;
  187.         2W) api_paste_expire_date=2W ;;
  188.         *) echo "Invalid expire date"; exit 1 ;;
  189.        esac ;;
  190.     r) case $OPTARG in
  191.           public|0) api_paste_private=0 ;;
  192.           unlisted|1) api_paste_private=1 ;;
  193.           private|2)
  194.             if [ $logintopastebin -ne 1 ]
  195.             then
  196.               echo "You need to log in (option -l or -u) if you want to post private pastes."
  197.               exit 1
  198.             else
  199.               api_paste_private=2
  200.             fi
  201.           ;;
  202.           *) echo "Invalid private value"; exit 1 ;;
  203.        esac
  204.     ;;
  205.     c) if [ -f "$OPTARG" ];
  206.        then
  207.          # shellcheck source=/dev/null
  208.          source "$OPTARG"
  209.        else
  210.          echo "$OPTARG is not exists"
  211.          exit 1
  212.        fi
  213.     ;;
  214.   esac
  215. done
  216.  
  217. [ $logintopastebin -ne 0 ] && auth_user
  218. api_paste_code=$( cat - )
  219. curl -0 --show-error \
  220.     --data "api_dev_key=$api_dev_key" \
  221.     --data "api_option=paste" \
  222.     --data "api_paste_code=$api_paste_code" \
  223.     --data "api_paste_format=$api_paste_format" \
  224.     --data "api_paste_private=$api_paste_private" \
  225.     --data "api_paste_expire_date=$api_paste_expire_date" \
  226.     --data "api_user_key=$api_user_key" \
  227.     --data-urlencode "api_paste_name=$api_paste_name" \
  228.     --data-urlencode "api_paste_code=$api_paste_code" \
  229.         "$api_url/api_post.php"
  230.  
  231. echo
  232. exit $?
Add Comment
Please, Sign In to add comment