是否可以在 bash shell 脚本中使用 pastebin(可能是通过他们的“API”功能)?如何发送http-post?如何取回网址?
11 回答
由于 pastebin.com 关闭了他们的公共 api,我正在寻找替代方案。
斯普鲁格很棒。用法:
<command> | curl -F 'sprunge=<-' http://sprunge.us
或者,当我使用它时:
alias paste="curl -F 'sprunge=<-' http://sprunge.us"
<command> | paste
文件说您POST
需要向
http://pastebin.com/api_public.php
唯一的强制参数是paste_code
,字符串类型是您要制作的粘贴。
成功pastebin
后将返回一个新的 URL。
您可以使用命令轻松地从您的 bash shell 中执行此操作curl
。
curl
使用该-d
选项将POST
数据发送到指定的 URL。
演示:
此演示将使用代码创建一个新粘贴:
printf("Hello..I am Codaddict");
从你的外壳:
$ curl -d 'paste_code=printf("Hello..I am Codaddict");' 'http://pastebin.com/api_public.php'
http://pastebin.com/598VLDZp
$
现在,如果您看到 URL http://pastebin.com/598VLDZp,您将看到我的粘贴 :)
或者,您可以使用wget
使用选项--post-data
发送POST
值的命令来执行此操作。
我试过这个命令它工作正常:
wget --post-data 'paste_code=printf("Hello..I am Codaddict");' 'http://pastebin.com/api_public.php'
将以下内容放入您的.bashrc
:
sprunge() {
if [[ $1 ]]; then
curl -F 'sprunge=<-' "http://sprunge.us" <"$1"
else
curl -F 'sprunge=<-' "http://sprunge.us"
fi
}
...然后你可以运行:
sprunge filename # post file to sprunge
...或者...
some_command | sprunge # pipe output to sprunge
https://paste.c-net.org/的 API 比所有这些都简单。只需“发布”到它。
从网站:
Upload text using curl:
$ curl -s --data 'Hello World!' 'https://paste.c-net.org/'
Upload text using wget:
$ wget --quiet -O- --post-data='Hello World!' 'https://paste.c-net.org/'
Upload a file using curl:
$ curl --upload-file @'/tmp/file' 'https://paste.c-net.org/'
Upload a file using wget:
$ wget --quiet -O- --post-file='/tmp/file' 'https://paste.c-net.org/'
Upload the output of a command or script using curl:
$ ls / | curl --upload-file - 'https://paste.c-net.org/'
$ ./bin/hello_world | curl -s --data-binary @- 'https://paste.c-net.org/'
您也可以简单地使用 netcat。与 termbin 不同,如果您的脚本生成输出的时间超过 5 秒,paste.c-net.org 不会超时。
$ { sleep 10; ls /; } | nc termbin.com 9999
$ { sleep 10; ls /; } | nc paste.c-net.org 9999
https://paste.c-net.org/ExampleOne
自 codaddict 发布以来,用于发布到 pastebin 的 API 已更改。
详细信息可在此链接中找到:https ://pastebin.com/api
例子:
curl -d 'api_paste_code=printf("Hello..\n I am Codaddict");' \
-d 'api_dev_key=<get_your_own>' \
-d 'api_option=paste' 'http://pastebin.com/api/api_post.php'
到目前为止,共有三个基本字段:
api_dev_key
-> 您需要在 pastebin.com 上创建一个登录名才能获得该登录名
api_option
-> 发布格式
api_paste_code
-> 您要发布的文本
以 Vishal 的回答为基础,pastebin 现在已升级为仅使用 HTTPS:
curl -d 'api_paste_code=printf("Hello World");' \
-d 'api_dev_key=<your_key>' \
-d 'api_option=paste' 'https://pastebin.com/api/api_post.php'
您不必指定-X POST
参数
可以在此处找到更多详细信息: https ://pastebin.com/doc_api#1
我发现 Sprunge 当前已关闭,但dpaste.com 有一个简单的 API。
从 STDIN 发帖
curl -s -F "content=<-" http://dpaste.com/api/v2/
从一个文件foo.txt
cat foo.txt | curl -s -F "content=<-" http://dpaste.com/api/v2/
发布一个字符串
curl -s -F "content=string" http://dpaste.com/api/v2/
响应将是粘贴的纯文本 URL。
注意:/
URL 中的尾随http://dpaste.com/api/v2/
似乎是必要的
另外两个答案(大约从 2014 年开始)指向http://sprunge.us,它旨在像这样使用......
curl --form 'sprunge=@yourfile.txt' sprunge.us
但是,截至 2018 年,srunge.us 有过载的趋势,并为每个请求返回 500 Internal Server Error。对于至少 300 KB 但不超过 2.8 MB 的文件,我在http://ix.io上使用非常相似的服务很幸运:
curl --form 'f:1=@yourfile.txt' ix.io
对于至少 2.8 MB(也许更高,我不知道)的文件,我发现了更高级的https://transfer.sh。它推荐了一个稍微不同且更简单的命令行,并且需要 https
(没有它就无法工作):
curl --upload-file yourfile.txt https://transfer.sh
发布到 pastebin 的最简单方法
echo 'your message' | sed '1s/^/api_paste_code=/g' | sed 's/$/\%0A/g' | curl -d @- -d 'api_dev_key=<your_api_key>' -d 'api_option=paste' 'http://pastebin.com/api/api_post.php'
只需更改<your_api_key>
零件并将任何您想要的东西用管道输送到其中。
sed
调用将参数添加api_paste_code
到消息的开头并在每行的末尾添加一个换行符,以便它可以处理多行输入。@-
告诉 curl 从标准输入读取。
一个可以粘贴的 Bash 函数
为了便于重用,将其设为 bash 函数(将其复制并粘贴到终端并API_KEY
适当地设置字段:
pastebin () {
API_KEY='<your_api_key>'
if [ -z $1 ]
then
cat - | sed '1s/^/api_paste_code=/g' | sed 's/$/\%0A/g' | curl -d @- -d 'api_dev_key='"$API_KEY"'' -d 'api_option=paste' 'http://pastebin.com/api/api_post.php'
else
echo "$1" | sed '1s/^/api_paste_code=/g' | sed 's/$/\%0A/g' | curl -d @- -d 'api_dev_key='"$API_KEY"'' -d 'api_option=paste' 'http://pastebin.com/api/api_post.php'
fi
printf '\n'
}
您可以使用以下任一方式运行它:
pastebin 'your message'
或者如果您需要将文件通过管道传输到其中:
cat your_file.txt | pastebin
基于此页面上的另一个答案,我编写了以下脚本,该脚本从 STDIN 读取(或假设输出它通过管道传输到其中)。
此版本允许使用 URI 转义的任意数据(按jq
)。
#!/bin/bash
api_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
curl -d "api_paste_code=$(jq -sRr @uri)" \
-d "api_dev_key=$api_key" \
-d 'api_option=paste' 'https://pastebin.com/api/api_post.php'
echo # By default, there's no newline