【问题标题】:Make SOAP call in bash without curl or wget在没有 curl 或 wget 的 bash 中进行 SOAP 调用
【发布时间】:2018-11-14 19:08:35
【问题描述】:

我正在尝试编写一个 bash 脚本来执行一个 SOAP 请求(并获得响应)。我能做到的

result=$(wget ${url} --post-data="${data}" --header='Content-Type: text/xml;charset=UTF-8'--timeout=${timeout} -qO- --delete-after)

result=$(curl --silent --header 'Content-Type: text/xml;charset=UTF-8' --data "${data}" "${url}" --connect-timeout "${timeout}")

其中“数据”是 XML 负载。

但是,我需要从中执行脚本的服务器没有 curl 或 wget,并且可能没有任何其他此类扩展功能。

那么有没有办法在不使用它们的情况下实现这一点? 一个相关的 stackexhange 问题表明这可能是可能的,尽管他们的要求略有不同。 https://unix.stackexchange.com/questions/83926/how-to-download-a-file-using-just-bash-and-nothing-else-no-curl-wget-perl-et

谢谢

编辑: 根据 Nic3500 的评论,我花了一整天的时间阅读 nc/netcat 并努力寻找我需要的文档。但我已经得到它发送以下内容:

POST <remote_URI> HTTP/1.1\r\n
User-Agent: Wget/1.14 (linux-gnu)\r\n
Accept: */*\r\n
Host: <IP Address>\r\n
Connection: Keep-Alive\r\n
Content-Type: text/xml;charset=UTF-8--timeout=10\r\n
Content-Length: <Content Length>\r\n
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="Core" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <ns1:Dispatch>
      <param0 xsi:type="xsd:string">some data</param0>
      [more params here]
      <param21 xsi:type="xsd:int">1</param21>
    </ns1:Dispatch>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

使用

echo ${head} | nc <IP Address> 80

现在据我所知,它与 wget 方法发送的内容精确匹配(该方法有效,但不能从我需要的服务器执行)基于获取它以转储它发送的数据。 但是很明显有些事情不太对劲,因为我得到的只是一个 400 Bad 请求。

【问题讨论】:

  • 您是否在服务器上安装了更多“进化”的脚本语言?即 perl、python、php、...?这些具有比纯 bash 更好的扩展。
  • @Nic3500 遗憾的是没有 perl python 或 php。
  • 他们喜欢把你的生活变成地狱我看到的 ;-) 看看这个也许 stackoverflow.com/questions/26850271/… netcatnc 可能会有所帮助。

标签: bash soap


【解决方案1】:

事实证明,我对编辑的尝试并不遥远,只需要在 echo 命令上使用 -e 开关:

POST <remote_URI> HTTP/1.1\r\n
User-Agent: Wget/1.14 (linux-gnu)\r\n
Accept: */*\r\n
Host: <IP Address>\r\n
Connection: Keep-Alive\r\n
Content-Type: text/xml;charset=UTF-8--timeout=10\r\n
Content-Length: <Content Length>\r\n
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="Core" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <ns1:Dispatch>
      <param0 xsi:type="xsd:string">some data</param0>
      [more params here]
      <param21 xsi:type="xsd:int">1</param21>
    </ns1:Dispatch>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

通过将其分配给数据变量,然后运行:

result=$(echo -e "${data}" | nc -w ${timeout} ${url} 80)

解决了我遇到的问题,因为 -e 告诉 echo 解释换行符,而我使用 nc 遇到的所有问题实际上都归结为某种形状或形式的错误行尾。供参考 - 如上所述 - HTTP 数据需要 CRLF 行结尾。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-10
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多