Amazon Web Service Java SDK Tutorial: List All Networks

In my previous posts, I showed samples on virtual machine creation, virtual machine instances listing, storage volume listing. This sample shows how to list all the networks that you have.

With the information about your networks, you can get all the private and public IP addresses.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

To run the following sample, you can check out the previous post for the pom.xml file and how to get AWS credentials from AWS console.

package org.doublecloud.awssample;
 
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.DescribeNetworkInterfacesResult;
import com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult;
import com.amazonaws.services.ec2.model.NetworkInterface;
import com.amazonaws.services.ec2.model.SecurityGroup;
 
public class AwsEc2ListNetworks
{
  public static void main(String[] args) throws Exception
  {
    AWSCredentials credentials = new PropertiesCredentials(AwsEc2ListNetworks.class.getResourceAsStream("/AwsCredentials.properties"));
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);
 
    DescribeNetworkInterfacesResult netResult = ec2.describeNetworkInterfaces();
 
    for (NetworkInterface net : netResult.getNetworkInterfaces())
    {
      System.out.println(net);
    }
  }
}

This is the output for my run of the sample, part of which has been modified to hide some details. Please note that although it looks like JSON format, but really it’s not. I wish Amazon could have made it a valid JSON format.

{NetworkInterfaceId: eni-a7f41b82,SubnetId: subnet-d3d915f9,VpcId: vpc-bc266cd8,AvailabilityZone: us-east-1d,Description: ,OwnerId: 307588169762,RequesterManaged: false,Status: in-use,MacAddress: 12:55:eb:79:cc:33,PrivateIpAddress: 172.31.56.121,PrivateDnsName: ip-172-31-56-121.ec2.internal,SourceDestCheck: true,Groups: [{GroupName: default,GroupId: sg-71475808}],Attachment: {AttachmentId: eni-attach-2bafc0d5,InstanceId: i-ac61a42f,InstanceOwnerId: 588169762307,DeviceIndex: 0,Status: attached,AttachTime: Mon Feb 29 16:27:22 PST 2016,DeleteOnTermination: true},Association: {PublicIp: 52.90.195.128,PublicDnsName: ec2-52-90-195-128.compute-1.amazonaws.com,IpOwnerId: amazon,},TagSet: [],PrivateIpAddresses: [{PrivateIpAddress: 172.31.56.121,PrivateDnsName: ip-172-31-56-121.ec2.internal,Primary: true,Association: {PublicIp: 52.90.195.128,PublicDnsName: ec2-52-90-195-128.compute-1.amazonaws.com,IpOwnerId: amazon,}}],}
This entry was posted in Cloud Computing, Software Development and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted April 18, 2016 at 10:41 am | Permalink

    Quite Brilliant – Thank you very much for sharing – I shall be handing this link out to many of my colleagues to take a look.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.