Hi!
I m newbie for scripting. My requirement is to create a host file as below from the output of aws api.
Hostname PrivateIP abc x.y.x.z
cde a.b.c.c
and so on.
I have the following script,
#!/bin/bash
export AWS_ACCESS_KEY=abc
export AWS_SECRET_KEY=cde
export EC2_URL=https://url.com
source ~/.bashrc
ec2-describe-instances --filter "group-name=mygroup" > /tmp/info
name=$(cat /tmp/info| grep 'Hostname' | cut -f 5)
ip=$(cat /tmp/info| grep 'INSTANCE' | cut -f 18)
echo -e $name"\t"$ip
currenltly the output is in one line.
I m sure i need a for loop. But not sure how to do it.
Please advice.
RudiC
June 30, 2014, 12:20pm
2
You echo just one line (which may have multiple names and ips). Please post the output of ec2-describe-instances
.
RESERVATION r-a64da0e5 322802824935 mygroup
INSTANCE i-ca4bef89 ami-896c96fe ec2-52-72-117-279.eu-west-1.compute.amazonaws.com ip-10-42-41-91.eu-west-1.compute.internal running mykey 0 t1.micro 2014-06-19T10:54:01+0000 eu-west-1b aki-52a34525 monitoring-disabled 52.72.117.279 10.42.41.91 ebs paravirtual xenUPcZb1403098656835 sg-05241d61 default false
BLOCKDEVICE /dev/sda1 vol-7e2e2772 2014-06-18T13:37:40.000Z true
TAG instance i-ca4bef89 Name linux-1
TAG instance i-ca4bef89 Hostname linux-1
RESERVATION r-a64da0e5 322802824935 mygroup
INSTANCE i-ca4bef89 ami-896c96fe ec2-51-72-117-279.eu-west-1.compute.amazonaws.com ip-10-41-41-91.eu-west-1.compute.internal running mykey 0 t1.micro 2014-06-19T10:54:01+0000 eu-west-1b aki-52a34525 monitoring-disabled 51.72.117.279 10.41.41.91 ebs paravirtual xenUPcZb1403098656835 sg-05241d61 default false
BLOCKDEVICE /dev/sda1 vol-7e2e2772 2014-06-18T13:37:40.000Z true
TAG instance i-ca4bef89 Name linux-2
TAG instance i-ca4bef89 Hostname linux-2
RudiC
June 30, 2014, 12:42pm
4
There's no "Hostname" string in that sample.
Sorry. that was incorrect. I have edited it now. The output is as above.
RudiC
June 30, 2014, 1:07pm
6
Not sure what
export AWS_ACCESS_KEY=abc
export AWS_SECRET_KEY=cde
are for and where they are used. Not sure either, why you are referencing field 18 ("default") in your script. I had to use 15 (or 14) to get an IP number.
Try
printf "\t%s\t%s\n" Hostname privateIP
while read IN X X HO TMPNM X X X X X X X X X TMPIP REST
do [ "$IN" = "INSTANCE" ] && IP=$TMPIP
[ "$HO" = "Hostname" ] && { NM=$TMPNM; printf "\t%s\t%s\n" $NM $IP; }
done < /tmp/info
or pipe the command output immediately to that while loop...