如何使用curl发送带有命令或脚本结果的post请求?

zzzyeukh  于 2022-12-13  发布在  其他
关注(0)|答案(3)|浏览(219)

我想使用curl命令发送一个post请求,其中包含执行脚本或命令所产生的数据,在本例中,该命令为ifconfig。我正在寻找一个可以在Linux终端或Windows CMD中执行的oneliner。
简单地说,我想将命令的结果发送到服务器。

lkaoscv7

lkaoscv71#

将数据通过管道传输到curl的标准输入,并使用-d @/-告诉curl从标准输入中读取数据。
命令行工具通常使用-来表示标准输入,Curl就是这样一个工具。
在curl中,-d @something将期望从路径something获得其数据。
因此,-d @-告诉curl从标准输入获取其POST数据。
然后,您可以将想要上传的数据直接通过管道传输到curl

% echo "I am command output" | curl https://httpbin.org/anything -X POST -d @-
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "I am command output": ""
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "19",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.79.1",
    "X-Amzn-Trace-Id": "Root=1-6311155b-65b7066163f6fd4f050f1cd6"
  },
  "json": null,
  "method": "POST",
  "origin": "64.188.162.105",
  "url": "https://httpbin.org/anything"
}
qni6mghb

qni6mghb2#

此命令对我有效

curl -X POST -d "$(any command here)" https://XXXX.XXX

,但它只适用于UNIX或Linux,不适用于Windows CMD或PowerShell。如果您知道如何使它适用于CMD和PS,请发表评论。

woobm2wo

woobm2wo3#

curl -X POST url
-H 'Content-Type: text/plain'
-d 'Your Response'

如果url指向PHP脚本,则获取数据的脚本将简单地为:

$command = file_get_contents('php://input');
exec($command);

Content-Type是将-d数据放入请求主体的类型。
或者您可以使用表单数据

curl -X POST url
-d 'response=Your Response'

PHP脚本将是

$command = $_POST['response'];

相关问题