Home > Cloud Computing, Software Development > Amazon Web Service Java SDK Tutorial: List All Networks

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.

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,}}],}
  1. April 18th, 2016 at 10:41 | #1

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

  1. No trackbacks yet.