Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a group of EC2 instances in a security group. I am looking for a way to obtain a list of their public DNS address's from command line. I have a script that uses RSYNC to make sure a set of files is updated across these servers. They are in an elastic load balancer and the amount of servers in the group changes enough that I do not want to have to maintain the list by hand. I want to avoid using AWK or SED to pull information out of ec2-describes-instances, I'd prefer to use the API directly. Anybody have any great suggestions? :)

share|improve this question
Why not put a cronjob on the instances themselves to pull down whatever files they need? Thats going to be a lot more stable than relying on one box to push updates to many. You could also use a user data script to pull in the files at boot time. – bobmagoo May 22 '14 at 15:10

The command line output format is pretty static, and you can control when it might changed because the toolkit is your hands. I keep a copy of the api tools in a versioned repository along with whatever software I've written that uses it, this way they always work together at a given version.

If you do decide to go this way, here is a simple grab to get the public DNS address of everything running. Obviously you could request this for a specific group too.

ec2-describe-instances | grep 'INSTANCE' | cut  -f 4
share|improve this answer
This doesn't really work because I want to grab only instances from a particular security group. It splits this information onto separate lines. – geekbri Apr 27 '11 at 22:18
up vote 3 down vote accepted

Incase anybody is searching for a solution, I ended up using Amazon's PHP SDK. It allows you to filter results by many different criteria. I simply did a describe instance request, filtering by a specific security group (where security-group-name is the one you want to search for!)

 // Get the response from a call to the DescribeInstances operation.
 $response = $ec2->describe_instances(array('Filter' => array(array('Name' => 'group-name
', 'Value' => 'security-group-name'))));
share|improve this answer
If this solved your question, please mark it as such. – Bart De Vos Jun 4 '11 at 19:51
ec2din -F instance-state-name=running | grep -e TAG -e INSTANCE | sed -e 's/TAG//' | awk '{print $4}'

You can also add filters for your desired security group adding -F group-name=your-group

share|improve this answer
Is there an important difference between your answer and Caleb's? They look remarkably similar. If there is, you should probably edit your answer to describe why yours is better. – Ladadadada Dec 4 '13 at 10:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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