Support Questions

Find answers, ask questions, and share your expertise

how to print the hosts and IP address for each host By API

avatar

the following API give only the hosts from the amabri server

curl -sH "X-Requested-By: ambari" -u admin:admin -i http://localhost:8080/api/v1/hosts

is it possible to change the API in way that we can get also the IP address for each host?

the following example print only the hosts :

curl -sH "X-Requested-By: ambari" -u admin:admin -i http://localhost:8080/api/v1/hosts | grep host_name | sed s'/"//g' | awk '{print $NF}'

master01

master02

master03

worker01

worker02

expected results

master01 192.9.200.12

master02 192.9.200.13

master03 192.9.200.14

worker01 192.9.200.15

worker02 192.9.200.16

Michael-Bronson
1 ACCEPTED SOLUTION

avatar
Master Mentor

@uri ben-ari

You can try something like following:

# curl -sH "X-Requested-By: ambari" -u admin:admin -i http://localhost:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip | grep -A 1 host_name | sed s'/"//g' | awk '{print $NF}' 

.

Example Output:

# curl -sH "X-Requested-By: ambari" -u admin:admin -i http://amb25101.example.com:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip | grep -A 1 host_name | sed s'/"//g' | awk '{print $NF}' 


http://amb25101.example.com:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip,
[
--
amb25101.example.com,
172.10.116.149
--
amb25102.example.com,
172.10.116.148
--
amb25103.example.com,
172.10.116.151
--
amb25104.example.com,
172.10.116.150

.

View solution in original post

3 REPLIES 3

avatar
Master Mentor

@uri ben-ari

You can try something like following:

# curl -sH "X-Requested-By: ambari" -u admin:admin -i http://localhost:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip | grep -A 1 host_name | sed s'/"//g' | awk '{print $NF}' 

.

Example Output:

# curl -sH "X-Requested-By: ambari" -u admin:admin -i http://amb25101.example.com:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip | grep -A 1 host_name | sed s'/"//g' | awk '{print $NF}' 


http://amb25101.example.com:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip,
[
--
amb25101.example.com,
172.10.116.149
--
amb25102.example.com,
172.10.116.148
--
amb25103.example.com,
172.10.116.151
--
amb25104.example.com,
172.10.116.150

.

avatar

is it possible to print as the following order

amb25101.example.com 172.10.116.149

amb25102.example.com 172.10.116.15

Michael-Bronson

avatar
Master Mentor

@uri ben-ari

Please try something like following:

#  curl -sH "X-Requested-By: ambari" -u admin:admin -i http://localhost:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip | grep -A 1 host_name | awk '{print $NF}' > /tmp/hosts_details.txt;  sed -e '1,2d' -e s'/--//g'  -e 's/\n//g' -e 's/"//g' -e '/^$/d' /tmp/hosts_details.txt | awk 'NR%2{printf "%s ",$0;next;}1

.

Example Output:

#  curl -sH "X-Requested-By: ambari" -u admin:admin -i http://amb25101.example.com:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip | grep -A 1 host_name | awk '{print $NF}' > /tmp/hosts_details.txt;  sed -e '1,2d' -e s'/--//g'  -e 's/\n//g' -e 's/"//g' -e '/^$/d' /tmp/hosts_details.txt | awk 'NR%2{printf "%s ",$0;next;}1'

amb25101.example.com 172.10.116.149
amb25102.example.com 172.10.116.148
amb25103.example.com 172.10.116.151
amb25104.example.com 172.10.116.150

.