-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect
executable file
·172 lines (161 loc) · 6.26 KB
/
connect
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env node
'use strict';
var aws = require('aws-sdk');
var HttpsProxyAgent = require('https-proxy-agent');
var argv = require('yargs').argv;
var Table = require('cli-table');
var fs = require('fs');
// default values
let profileToUse = 'default';
let regionToUse = 'ap-southeast-2';
let publicAddress = false;
if (argv.hasOwnProperty('profile')) {
profileToUse = argv.profile;
}
else if (typeof process.env.AWS_PROFILE !== 'undefined') {
console.log(`AWS_PROFILE is set to ${process.env.AWS_PROFILE}, so we will use it.`);
profileToUse = process.env.AWS_PROFILE;
}
var credentials = new aws.SharedIniFileCredentials({ profile: profileToUse });
aws.config.credentials = credentials;
if (typeof process.env.https_proxy !== 'undefined') {
aws.config.update({
httpOptions: {
agent: new HttpsProxyAgent(process.env.https_proxy)
}
});
}
if (argv.hasOwnProperty('region')) {
regionToUse = argv.region;
}
var ec2 = new aws.EC2({ region: regionToUse });
if (argv.hasOwnProperty('public')) {
publicAddress = true;
}
var constructed = [];
var table = new Table({
head: ['','Name','ID','Type','LaunchedAt','IP','State'],
colWidths: [5,40,21,12,28,20,20]
});
function usage() {
console.log("");
console.log("Usage");
console.log("---------------------------------------------------------------------------------------------------------------------");
console.log("akon [--public] [--profile profileName] [--region awsRegion] arg1 [arg2]...[argN]");
console.log("public: List Public IP addresses of instances, defaults to unset(will list private IP addresses, if available)");
console.log("profileName: AWS profile to use, defaults to 'default' profile");
console.log("awsRegion: AWS region to use, defaults to 'ap-southeast-2'");
console.log("arg1: Search string in a dashed naming convention");
console.log("argN: Search string in a dashed naming convention");
console.log("For example , group-environment-*-application-single can be found with " + process.argv[1] + " group environment \"*\" application single");
}
if (process.argv.length < 4) {
usage();
process.exit(1);
}
var finalString = "";
var fileName = argv._[0];
for (var i = 1; i < argv._.length; i++) {
if (finalString === "") {
finalString = argv._[i];
}
else {
finalString = finalString + "-" + argv._[i];
}
}
finalString = finalString + "*";
var params = {
DryRun: false,
Filters : [
{
Name: 'tag:Name',
Values : [
finalString
]
}
]
};
ec2.describeInstances(params, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
var counter = 0;
var constructCounter = 0;
if (data.Reservations.length > 0) {
for (var i = 0; i < data.Reservations.length; i++) {
for (var j = 0; j < data.Reservations[i].Instances.length; j++) {
for (var k = 0; k < data.Reservations[i].Instances[j].Tags.length; k++) {
if (data.Reservations[i].Instances[j].Tags[k].Key == "Name") {
if (data.Reservations[i].Instances[j].State.Name == 'running') {
constructed[++constructCounter] = data.Reservations[i].Instances[j];
// if public flag is set, list public addresses
if (publicAddress === true) {
if (typeof data.Reservations[i].Instances[j].PublicIpAddress === 'undefined') {
table.push([
++counter,
data.Reservations[i].Instances[j].Tags[k].Value,
data.Reservations[i].Instances[j].InstanceId,
data.Reservations[i].Instances[j].InstanceType,
data.Reservations[i].Instances[j].LaunchTime,
'n/a',
data.Reservations[i].Instances[j].State.Name
]);
if (typeof constructed[constructCounter].PrivateIpAddress !== 'undefined') {
delete constructed[constructCounter].PrivateIpAddress;
}
}
else {
table.push([
++counter,
data.Reservations[i].Instances[j].Tags[k].Value,
data.Reservations[i].Instances[j].InstanceId,
data.Reservations[i].Instances[j].InstanceType,
data.Reservations[i].Instances[j].LaunchTime,
data.Reservations[i].Instances[j].PublicIpAddress,
data.Reservations[i].Instances[j].State.Name
]);
// override private ip to be public ip since the akon script uses private address
constructed[constructCounter].PrivateIpAddress = data.Reservations[i].Instances[j].PublicIpAddress;
}
}
else { // else list private addresses
if (typeof data.Reservations[i].Instances[j].PrivateIpAddress === 'undefined') {
table.push([
++counter,
data.Reservations[i].Instances[j].Tags[k].Value,
data.Reservations[i].Instances[j].InstanceId,
data.Reservations[i].Instances[j].InstanceType,
data.Reservations[i].Instances[j].LaunchTime,
'n/a',
data.Reservations[i].Instances[j].State.Name
]);
}
else {
table.push([
++counter,
data.Reservations[i].Instances[j].Tags[k].Value,
data.Reservations[i].Instances[j].InstanceId,
data.Reservations[i].Instances[j].InstanceType,
data.Reservations[i].Instances[j].LaunchTime,
data.Reservations[i].Instances[j].PrivateIpAddress,
data.Reservations[i].Instances[j].State.Name
]);
}
}
}
}
}
}
}
fs.writeFile(fileName, JSON.stringify(constructed), function(err) {
if (err) return console.log(err);
process.exit();
});
console.log(table.toString());
}
else {
console.log("No instances found");
}
}
});