如何从shell脚本中使用pastebin?

发布于 2024-09-29 16:14:41 字数 136 浏览 4 评论 0原文

是否可以在bash shell脚本中使用pastebin(可能通过其"API"功能)?如何发送http-post?如何取回 URL?

Is it possible to use pastebin (may be via their "API" functionality) inside bash shell scripts? How do I send http-post? How do I get back the URL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

江挽川 2024-10-06 16:14:41

由于pastebin.com 关闭了他们的公共API,我一直在寻找替代方案。

Sprunge 很棒。用法:

<command> | curl -F 'sprunge=<-' http://sprunge.us

或者,正如我使用的那样:

alias paste="curl -F 'sprunge=<-' http://sprunge.us"
<command> | paste

As pastebin.com closed their public api, I was looking for alternatives.

Sprunge is great. Usage:

<command> | curl -F 'sprunge=<-' http://sprunge.us

or, as I use it:

alias paste="curl -F 'sprunge=<-' http://sprunge.us"
<command> | paste
墨小墨 2024-10-06 16:14:41

文档 表示您需要提交一个 POST 请求

http://pastebin.com/api_public.php

,并且是唯一强制的参数是paste_code,字符串类型是您要制作的粘贴。

成功后,将返回新的 pastebin URL。

您可以使用命令 curl 在 bash shell 中轻松完成此操作。

curl 使用-d 选项将POST 数据发送到指定的URL。

演示:

此演示将使用以下代码创建一个新粘贴:

printf("Hello..I am Codaddict");

从您的 shell:

$ 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'

The documentation says that you need to submit a POST request to

http://pastebin.com/api_public.php

and the only mandatory parameter is paste_code, of type string is the paste that you want to make.

On success a new pastebin URL will be returned.

You can easily do this from your bash shell using the command curl.

curl uses the -d option to send the POST data to the specified URL.

Demo:

This demo will create a new paste with the code:

printf("Hello..I am Codaddict");

From your shell:

$ curl -d 'paste_code=printf("Hello..I am Codaddict");' 'http://pastebin.com/api_public.php'
http://pastebin.com/598VLDZp
$

Now if you see the URL http://pastebin.com/598VLDZp, you'll see my paste :)

Alternatively you can do it using the wget command which uses the option --post-data to sent POST values.

I've tried this command it works fine:

wget --post-data 'paste_code=printf("Hello..I am Codaddict");' 'http://pastebin.com/api_public.php'
℉服软 2024-10-06 16:14:41

将以下内容放入您的 .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

Put the following in your .bashrc:

sprunge() {
  if [[ $1 ]]; then
    curl -F 'sprunge=<-' "http://sprunge.us" <"$1"
  else
    curl -F 'sprunge=<-' "http://sprunge.us"
  fi
}

...and then you can run:

sprunge filename # post file to sprunge

...or...

some_command | sprunge # pipe output to sprunge
无风消散 2024-10-06 16:14:41

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

https://paste.c-net.org/ has a simpler API than all of them. Simply "POST" to it.

From the website:

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/'

You can also simply use netcat. Unlike termbin, paste.c-net.org won't time out if your script takes more than 5 seconds to produce its output.

$ { sleep 10; ls /; } | nc termbin.com 9999
$ { sleep 10; ls /; } | nc paste.c-net.org 9999
https://paste.c-net.org/ExampleOne
ゞ记忆︶ㄣ 2024-10-06 16:14:41

自 codaddict 发布以来,用于发布到 Pastebin 的 API 已更改。
详细信息可以通过以下链接找到:https://pastebin.com/api

示例:
<代码>
curl -d 'api_paste_code=printf("你好..\n 我是 Codaddict");' \
-d 'api_dev_key=' \
-d 'api_option=paste' 'http://pastebin.com/api/api_post.php'

目前有三个基本字段:
api_dev_key ->您需要在pastebin.com 上创建一个登录才能获得该信息
api_option ->发帖格式
api_paste_code ->您要发布的文字

The API for posting to pastebin has changed, since posted by codaddict.
Details can be found at this link: https://pastebin.com/api

Example:

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'

There are three essential fields as of now:
api_dev_key -> You need to create a login on pastebin.com in order to get that
api_option -> Format in which to post
api_paste_code -> Text you want to post

何必那么矫情 2024-10-06 16:14:41

另外两个答案(大约 2014 年)指向 http://sprunge.us,它的设计目的是这样使用的...

curl --form '[email protected]' sprunge.us

但是,截至 2018 年,sprunge.us 有过载的趋势,并对每个请求返回 500 内部服务器错误。对于至少 300 KB 但不超过 2.8 MB 的文件,我在 http:// /ix.io

curl --form 'f:[email protected]' ix.io

对于至少 2.8 MB(也许更高,我不知道)的文件,我发现了更精致的 https://transfer.sh。它建议使用稍微不同且更简单的命令行,并且需要 https(没有它就无法工作):

curl --upload-file yourfile.txt https://transfer.sh

Two other answers (from circa 2014) point to http://sprunge.us, which is designed to be used like this...

curl --form '[email protected]' sprunge.us

However, as of 2018, sprunge.us has a tendency to be overloaded and return 500 Internal Server Error to every request. For files up to at least 300 KB but not as high as 2.8 MB, I have had good luck with the very similar service at http://ix.io:

curl --form 'f:[email protected]' ix.io

For files up to at least 2.8 MB (and maybe higher, I don't know), I've found the more highly polished https://transfer.sh. It recommends a slightly different and simpler command line, and requires https (it won't work without it):

curl --upload-file yourfile.txt https://transfer.sh
誰ツ都不明白 2024-10-06 16:14:41

我发现 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/ 中的尾随 / 似乎是必要的

I have found that Sprunge is currently down, but dpaste.com has a simple API.

To post from STDIN

curl -s -F "content=<-" http://dpaste.com/api/v2/

from a file foo.txt

cat foo.txt | curl -s -F "content=<-" http://dpaste.com/api/v2/

to post a string

curl -s -F "content=string" http://dpaste.com/api/v2/

The response will be a plain text URL to the paste.


Nb: the trailing / in the URL http://dpaste.com/api/v2/ seems necessary

薆情海 2024-10-06 16:14:41

发布到 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'

只需更改 部分并将您想要的任何内容通过管道传输到其中。

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

Easiest way to post to 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'

Just change the <your_api_key> part and pipe whatever you want into it.

The sed invocations add the api_paste_code parameter to beginning of the message and add a newline at the end of each line so it can handle multiline input. The @- tells curl to read from stdin.

A Bash Function You Can Paste

For easy reuse, make it a bash function (copy and paste this into your terminal and set the API_KEY field appropriately:

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'
}

You can run it with either:

pastebin 'your message'

or if you need to pipe a file into it:

cat your_file.txt | pastebin

在风中等你 2024-10-06 16:14:41

为了构建 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

To built upon Vishal's answer, pastebin has upgraded to only use HTTPS now:

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'

You don't have to specify the -X POST parameter

Additional details can be found here:
https://pastebin.com/doc_api#1

陪我终i 2024-10-06 16:14:41

基于本页上的另一个答案,我编写了以下脚本,该脚本从 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

Based on another answer on this page, I wrote the following script which reads from STDIN (or assumes output it piped into it).

This version allows for arbitrary data which is URI escaped (by 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
何其悲哀 2024-10-06 16:14:41

我写这篇文章有点晚了,但我创建了一个小工具来帮助解决这个问题。

https://pasteshell.com/

请随意查看并告诉我您的想法。

谢谢,

I am a bit late to this post, but I created a little tool to help with this.

https://pasteshell.com/

Feel free to check it out and let me know what you think.

Thanks,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文