0

by the following api we can get the info about master1/2 machines

curl -sH "X-Requested-By: ambari" -u "admin"":""admin" -i http://192.23.39.2:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip | egrep "master1|master3"  | grep http  
      "href" : "http://192.23.39.2:8080/api/v1/hosts/master1.usa14.com",
      "href" : "http://192.23.39.2:8080/api/v1/hosts/master3.usa14.com",

now I update the api to get only the IP's of master1/3

curl -sH "X-Requested-By: ambari" -u "admin"":""admin" -i http://192.23.39.2:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip | egrep "master1|master3"  | grep http | sed s'/\/\// /g' | sed s'/:/ /g' | awk '{print $3}'

192.23.39.2
192.23.39.2

but my api isn't so elegant

any other suggestion how to capture the ip's ?

2

You can do this by simply by using awk string manipulation functions

awk -F'/[/]?' '$NF ~ /^(master1|master3).*/{ n=split($2,arr,":"); print arr[1] }'
0

using grep :

  grep 'master[1,3]' | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"
0

Done by using sed command and it worked fine

command

 sed -e "s/.*\:\///g" -e "s/:.*//g" -e "s/\///g" filename

output

192.23.39.2
192.23.39.2
0

Done by using python too

import re
p=re.compile(r'[0-9]*\.[0-9]*\.[0-9]*')
k=open('filename','r')
for i in k:
    h=re.search(p,i)
    print h.group()

output

192.23.39
192.23.39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.