-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEC2Enum.java
98 lines (86 loc) · 3.01 KB
/
EC2Enum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.internal.StaticCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
public class EC2Enum
{
static
{
//disable apache's logging in amazon AWS SDK
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
}
public static void main(String[] args)
{
ClientConfiguration cc = new ClientConfiguration();
//Possible to set a proxy to get out.
//cc.setProxyHost("ipProxy");
//cc.setProxyPort(3128);
StringBuilder output = new StringBuilder();
//get keys some way
String accessKey = "AWS access key"
String secretKey = "AWS secret key"
String accountName = "Account name" //name associated to keys only used for output
//basic check
if (accessKey.length() != 0 && secretKey.length() != 0)
{
try
{
//looking in all AWS EC2 regions (us-east-1 us-west-2 us-west-1 eu-west-1 eu-central-1 ap-southeast-1 etc.)
for(Regions regionRef : Regions.values())
{
BasicAWSCredentials creds = new BasicAWSCredentials(a.getAccessKey(), a.getSecretKey());
try
{
//Building ec2 client for current region.
AmazonEC2Client ec2cli = Region.getRegion(regionRef).createClient(AmazonEC2Client.class, new StaticCredentialsProvider(creds), cc);
//ec2 instances are in sommething called "reservations"
for (Reservation r : ec2cli.describeInstances().getReservations())
{
//looping for each instance
for (Instance i : r.getInstances())
{
String ipStr = i.getPublicIpAddress();
//check if instance is running and has a public ip.
if (ipStr != null && !"null".equals(ipStr) && "running".equals(i.getState().getName()))
{
//append host to output, formatted for redis
output.append("sadd AWS-");
output.append(accountName.replace(' ', '_'));
output.append('(');
output.append(regionRef.getName().replace(' ', '_'));
output.append(") ");
output.append(ipStr);
output.append('\n');
}
}
}
}
//catch "unauthorized" on some regions (us-gov and north korea?)
catch (AmazonServiceException e)
{
continue;
}
}
}
catch (AmazonServiceException e)
{
//append error to output, commented in redis
output.append("#ERROR ");
output.append(a.getAccountName());
output.append(' ');
output.append(e.getErrorMessage());
output.append('\n');
}
}
System.out.print(output.toString().trim());
}
}