· 5 years ago · Sep 25, 2020, 08:08 AM
1#!/bin/bash
2
3# TODO
4# change api_paste_private
5# 0 = Public
6# 1 = Unlisted
7# 2 = Private
8#
9# change api_paste_expire_date
10# N = Never
11# 10M = 10 Minutes
12# 1H = 1 Hour
13# 1D = 1 Day
14# 1W = 1 Week
15# 2W = 2 Weeks
16# 1M = 1 Month
17
18function howto {
19 echo "Usage: [ -f file ] [ -p name ] [ -t format ] -a -h -l
20 -a -> anonymous mode
21 -h -> help
22 -l -> list"
23 exit
24}
25
26file=""
27pname=""
28format=""
29anonymous=""
30username=""
31password=""
32api_key=""
33list=""
34
35declare -A extensions_name
36
37extensions_name=( ["py"]="python" ["php"]="php" ["sh"]="bash" ["c"]="c" ["cpp"]="cpp")
38
39while getopts "f:p:t:lah" arg; do
40 case $arg in
41 f)
42 file=$OPTARG
43 ;;
44 p)
45 pname=$OPTARG
46 ;;
47 t)
48 format=$OPTARG
49 ;;
50 l)
51 list="ok"
52 ;;
53 a)
54 anonymous="ok"
55 ;;
56 h)
57 howto
58 ;;
59 *)
60 howto
61 ;;
62 esac
63done
64
65conf_filename="$HOME/.pastbin.cnf"
66
67if [ -f $conf_filename ]; then
68 echo "load config file"
69 source $conf_filename
70else
71 echo "Please create configuration file: $conf_filename"
72 exit
73fi
74
75if [[ ! -n "$username" || ! -n "$password" || ! -n "$api_key" ]]; then
76 echo "Invalid configuration file"
77 exit
78fi
79
80if [[ ! -n "$anonymous" ]]; then
81 echo "get user api key"
82 uk=$(curl -d api_dev_key="$api_key" -d api_user_name="$username" -d api_user_password="$password" https://pastebin.com/api/api_login.php)
83fi
84
85if [[ -n "$list" ]];
86then
87 curl -s -d api_dev_key="$api_key" -d api_user_key="$uk" -d api_user_name="$username" -d api_user_password="$password" -d api_option="list" https://pastebin.com/api/api_post.php
88else
89 # if [ -z "${s}" ] || [ -z "${p}" ];
90 if [[ ! -n "$file" ]]; then
91 howto
92 exit
93 fi
94 if [[ ! -n "$pname" ]]; then
95 pname=$(basename $file)
96 fi
97
98 extension=${pname##*.}
99
100 for key in ${!extensions_name[@]}; do
101 if [[ "${key}" == "${extension}" ]]; then
102 format=${extensions_name[${key}]}
103 fi
104 done
105
106 if [[ ! -n "$format" ]]; then
107 echo "Can't get file format. Please specify it with -t option"
108 exit
109 fi
110
111 if [[ ! -f $file ]]; then
112 echo "$file: This file does not exists."
113 exit
114 fi
115 pc=$(cat $file)
116
117 if [[ ! -n "$pc" ]]; then
118 exit
119 fi
120
121 res=$(curl -s -d api_dev_key="$api_key" -d api_user_key="$uk" -d api_user_name="$username" -d api_user_password="$password" -d api_option="paste" -d api_paste_code="$pc" -d api_paste_name="$pname" -d api_paste_format="$format" https://pastebin.com/api/api_post.php)
122 echo ""
123 if [ "$res" == "Bad API request, invalid api_paste_format" ]; then
124 echo "The -t option is invalid"
125 else
126 echo $res
127 fi
128fi
129
130echo ""
131# END