如何将bash输出用作变量

发布于 2025-02-11 20:15:37 字数 585 浏览 1 评论 0原文

我正在创建一个作业,该作业首先启动API检查带有某些参数的子网中的下一个IP是什么。

然后,我想在输出(即IP)上运行“ ping”检查(即IP),然后在同一输出上telnet到端口22 80 3389,

如何将所有curl输出插入到一个变量中,以便我可以继续脚本在指出IP确实“可用”之前,请运行Ping和Telnet检查 - 我在过去的两天中尝试了许多失败的语法:)谢谢。

因此:

#!/bin/bash
curl --stderr -i -H "Accept: application/json" -X POST -d "query=ip" -d "string=$1" -u 'username:password' https://device42.xxxxxxxxx/api/1.0/search/ --insecure | awk '{print "Avaliable",$9,$10,$11}'
[[ -z "$1" ]] && echo "Please Enter IP" ||

API返回此ATM:

available: Yes ip: 10.120.34.11

I am creating a job that would first initiate an API checking what is the next available ip in a subnet with some parameters.

Then i want to run "ping" check on the output (that is an IP), then also telnet to ports 22 80 3389 on the same output,

how can i insert all the CURL output in to a variable so i can continue the script running ping and telnet checks before giving an indication that the ip is really "Available" - i have tried many failed syntaxes in the last 2 days :) thank you.

so:

#!/bin/bash
curl --stderr -i -H "Accept: application/json" -X POST -d "query=ip" -d "string=$1" -u 'username:password' https://device42.xxxxxxxxx/api/1.0/search/ --insecure | awk '{print "Avaliable",$9,$10,$11}'
[[ -z "$1" ]] && echo "Please Enter IP" ||

the api returns this atm:

available: Yes ip: 10.120.34.11

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

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

发布评论

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

评论(1

倒带 2025-02-18 20:15:37

curl -stderr -i无效。您可能想要curl -stderr -i

尝试在任何外壳中使用命令输出替换`

#!/bin/bash
var=`curl --stderr - -i -H "Accept: application/json" -X POST -d "query=ip" -d "string=$1" -u 'username:password' https://device42.xxxxxxxxx/api/1.0/search/ --insecure | awk '{print "Avaliable",$9,$10,$11}'`
[[ -z "$var" ]] && echo "Please Enter IP" ||

或其他命令输出替代语法

#!/bin/bash
var=$(curl --stderr - -i -H "Accept: application/json" -X POST -d "query=ip" -d "string=$1" -u 'username:password' https://device42.xxxxxxxxx/api/1.0/search/ --insecure | awk '{print "Avaliable",$9,$10,$11}')
[[ -z "$var" ]] && echo "Please Enter IP" ||

curl --stderr -i is invalid. You might want curl --stderr - -i.

Try substitution ` with command output in any shell:

#!/bin/bash
var=`curl --stderr - -i -H "Accept: application/json" -X POST -d "query=ip" -d "string=$1" -u 'username:password' https://device42.xxxxxxxxx/api/1.0/search/ --insecure | awk '{print "Avaliable",$9,$10,$11}'`
[[ -z "$var" ]] && echo "Please Enter IP" ||

Or with another command output substitution syntax

#!/bin/bash
var=$(curl --stderr - -i -H "Accept: application/json" -X POST -d "query=ip" -d "string=$1" -u 'username:password' https://device42.xxxxxxxxx/api/1.0/search/ --insecure | awk '{print "Avaliable",$9,$10,$11}')
[[ -z "$var" ]] && echo "Please Enter IP" ||
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文