Advertisement
v1ral_ITS

pbin [TERMINAL POST TO PERSONAL PASTEBIN]

Jul 10th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.91 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # bash pastebin.com client
  4. # https://github.com/mefuckin/pastebin-shell
  5.  
  6. # Path to config file
  7. config_file="$HOME/.config/pastebin.conf"
  8.  
  9. # Config file overwrites following default settings
  10.  
  11. # This is my API Developer Key (you can change it here or use config file)
  12. api_dev_key="97d4dbcc849cf72fe005154e40904f71"
  13.  
  14. # Autologin
  15. logintopastebin="0"
  16.  
  17. # Default paste title, if -n argument is not defined
  18. api_paste_name=""
  19.  
  20. # Default paste highlighting, if -f argument is not defined
  21. api_paste_format="text"
  22.  
  23. # Default private settings, if -p argument is not defined (0=public, 1=unlisted, 2=private)
  24. api_paste_private="0"
  25.  
  26. # Default expire date, if -e argument is not defined
  27. api_paste_expire_date="1H"
  28.  
  29. # Pastebin.com API URL
  30. api_url="http://pastebin.com/api"
  31.  
  32. usage()
  33. {
  34. cat << EOF
  35. Usage: $0 [options]
  36. Post text from stdin to pastebin.com.
  37.  
  38. Options:
  39.     -l              Log into pastebin.com
  40.     -u              Your pastebin.com username
  41.     -p              Your pastebin.com password
  42.     -n name             Paste title
  43.     -f format           Syntax highlightig format (you can find list
  44.                     of formats at http://pastebin.com/api#5)
  45.     -e expire           Paste expire time:
  46.                       n (or never) - never
  47.                       10M          - 10 minutes
  48.                       1H           - 1 hour
  49.                       1D           - 1 day
  50.                       1M           - 1 month
  51.     -r private          Private settings: public (or 0), unlisted (or 1)
  52.                     or private (or 2, need to login)
  53.     -h              Show this message
  54.     -o              Print sample config
  55.     -c /path/to/pastebin.conf   Set path to config file
  56. EOF
  57. }
  58.  
  59. # command_exists() tells if a given command exists.
  60. function command_exists() {
  61.     command -v "$1" >/dev/null 2>&1
  62. }
  63.  
  64. sample_config()
  65. {
  66. cat << EOF
  67. # Change to your API Developer Key
  68. #api_dev_key=""
  69.  
  70. # Change to your pastebin.com username, if you want to log into your account
  71. #api_user_name=""
  72.  
  73. # Change to your pastebin.com password
  74. #api_user_password=""
  75.  
  76. # Set to 1 if you want to log into your account automatically
  77. # (you may login with -l, -u, -p arguments also)
  78. #logintopastebin="0"
  79.  
  80. # Default paste title, if -n argument is not defined
  81. #api_paste_name=""
  82.  
  83. # Default paste highlighting, if -f argument is not defined
  84. # (see http://pastebin.com/api#5 for more)
  85. #api_paste_format="text"
  86.  
  87. # Default private settings, if -p argument is not defined
  88. # (0=public, 1=unlisted, 2=private)
  89. #api_paste_private="0"
  90.  
  91. # Default expire date, if -e argument is not defined
  92. #api_paste_expire_date="N"
  93. EOF
  94. }
  95.  
  96. auth_user()
  97. {
  98.     if [ -z "$api_user_name" ] || [ -z "$api_user_password" ]
  99.     then
  100.         echo "Username and/or password is undefined."
  101.         echo "Please adjust 'api_user_name' and 'api_user_password' in the configuration file '$config_file'"
  102.         exit 1
  103.     fi
  104.  
  105.     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")"
  106.  
  107.     if [[ $server_reply == Bad* ]]
  108.     then
  109.         echo "Username: $api_user_name"
  110.         echo "$server_reply"
  111.         exit 1
  112.     else
  113.         api_user_key="$server_reply"
  114.     fi
  115. }
  116.  
  117. if ! command_exists curl; then
  118.     echo "'curl' is needed. Please install 'curl'. More details can be found at https://curl.haxx.se/"
  119.     exit 1
  120. fi
  121.  
  122. # Load configuration file if it exists.
  123. if [ -f "$config_file" ]; then
  124.     # shellcheck source=/dev/null
  125.     source "$config_file"
  126. fi
  127.  
  128. while getopts "hu:p:ln:f:c:e:r:o" OPTION ; do
  129.   case $OPTION in
  130.     h) usage; exit ;;
  131.     o) sample_config; exit ;;
  132.     u) api_user_name="$OPTARG"; logintopastebin=1 ;;
  133.     p) api_user_password="$OPTARG" ;;
  134.     l) logintopastebin=1 ;;
  135.     n) api_paste_name="$OPTARG" ;;
  136.     f) api_paste_format="$OPTARG" ;;
  137.     e) case $OPTARG in
  138.         n|never) api_paste_expire_date=N ;;
  139.         10M) api_paste_expire_date=10M ;;
  140.         1H) api_paste_expire_date=1H ;;
  141.         1D) api_paste_expire_date=1D ;;
  142.         1M) api_paste_expire_date=1M ;;
  143.         1W) api_paste_expire_date=1W ;;
  144.         2W) api_paste_expire_date=2W ;;
  145.         *) echo "Invalid expire date"; exit 1 ;;
  146.        esac ;;
  147.     r) case $OPTARG in
  148.           public|0) api_paste_private=0 ;;
  149.           unlisted|1) api_paste_private=1 ;;
  150.           private|2)
  151.             if [ $logintopastebin -ne 1 ]
  152.             then
  153.               echo "You need to log in (option -l or -u) if you want to post private pastes."
  154.               exit 1
  155.             else
  156.               api_paste_private=2
  157.             fi
  158.           ;;
  159.           *) echo "Invalid private value"; exit 1 ;;
  160.        esac
  161.     ;;
  162.     c) if [ -f "$OPTARG" ];
  163.        then
  164.          # shellcheck source=/dev/null
  165.          source "$OPTARG"
  166.        else
  167.          echo "$OPTARG is not exists"
  168.          exit 1
  169.        fi
  170.     ;;
  171.   esac
  172. done
  173.  
  174. [ $logintopastebin -ne 0 ] && auth_user
  175. api_paste_code=$( cat - )
  176. curl -0 --show-error \
  177.     --data "api_dev_key=$api_dev_key" \
  178.     --data "api_option=paste" \
  179.     --data "api_paste_code=$api_paste_code" \
  180.     --data "api_paste_format=$api_paste_format" \
  181.     --data "api_paste_private=$api_paste_private" \
  182.     --data "api_paste_expire_date=$api_paste_expire_date" \
  183.     --data "api_user_key=$api_user_key" \
  184.     --data-urlencode "api_paste_name=$api_paste_name" \
  185.     --data-urlencode "api_paste_code=$api_paste_code" \
  186.         "$api_url/api_post.php"
  187.  
  188. echo
  189. exit $?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement