-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.cs
290 lines (283 loc) · 12.1 KB
/
main.cs
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Tea;
using Tea.Utils;
namespace AliyunAkTools
{
public partial class main : Form
{
public string AK_ID = "";
public string AK_SK = "";
public string fCloudAssistantStatus;
public main( )
{
InitializeComponent();
}
/**
创建client
*/
public static AlibabaCloud.SDK.Ecs20140526.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
// 您的AccessKey ID
AccessKeyId = accessKeyId,
// 您的AccessKey Secret
AccessKeySecret = accessKeySecret,
};
// 访问的域名
config.Endpoint = "ecs.aliyuncs.com";
return new AlibabaCloud.SDK.Ecs20140526.Client(config);
}
private void ConnectBtn_Click(object sender, EventArgs e)
{
if (this.srv_lvw.Items.Count > 1)
{
this.srv_lvw.Items.Clear();
}
AK_ID = AKID.Text;
AK_SK = AKSK.Text;
LogInfo("Tring use: " + AK_ID + "----" + AK_SK + " login.");
AlibabaCloud.SDK.Ecs20140526.Client client = CreateClient(AK_ID, AK_SK);
AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest describeRegionsRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest();
//请求SDK,屎一样的SDK
try
{
AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsResponse response = client.DescribeRegions(describeRegionsRequest);
//解析为JSON
String JsonData = AlibabaCloud.TeaUtil.Common.ToJSONString(response.Body.ToMap());
//解析JSON为数组
JObject Region = JObject.Parse(JsonData);
JArray items = (JArray)Region["Regions"]["Region"];
//获取地区数量
int length = items.Count;
List<string> regionIds = new List<string>();
for (int i = 0; i < items.Count; i++)
{
regionIds.Add(items[i]["RegionId"].ToString());
}
LogSuccess("Login Success! Get Available Aera Success, Area Count:" + length.ToString());
string[] _regionIds = regionIds.ToArray();
Thread t = new Thread(( ) => getAccountServer(AK_ID, AK_SK, _regionIds));
t.Start();
}
catch
{
LogError("Connect Failed, Check Your AK/AKSK");
}
}
public void getAccountServer(String AK_ID, String AK_SK, String[] regionIds)
{
int count = 0;
AlibabaCloud.SDK.Ecs20140526.Client client = CreateClient(AK_ID, AK_SK);
foreach (var regionId in regionIds)
{
AlibabaCloud.SDK.Ecs20140526.Models.DescribeInstancesRequest describeInstancesRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeInstancesRequest
{
RegionId = regionId
};
try
{
AlibabaCloud.SDK.Ecs20140526.Models.DescribeInstancesResponse resp = client.DescribeInstances(describeInstancesRequest);
List<AlibabaCloud.SDK.Ecs20140526.Models.DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances.DescribeInstancesResponseBodyInstancesInstance> instances = resp.Body.Instances.Instance;
foreach (var instance in instances)
{
string cloudAssistantStatus = DescribeCloudAssistantStatus(client, instance.InstanceId, instance.RegionId);
switch ((string)cloudAssistantStatus){
case "false":
cloudAssistantStatus = "×";
break;
case "true":
cloudAssistantStatus = "√";
break;
default:
cloudAssistantStatus = "Unknow";
break;
}
addItemToListView(count,
instance.InstanceId,
instance.RegionId,
instance.HostName,
instance.OSType + "(" + instance.OSName + ")",
instance.Status,
string.Join(",", instance.VpcAttributes.PrivateIpAddress.IpAddress.ToArray()),
string.Join(",", instance.PublicIpAddress.IpAddress.ToArray()),
string.Join(",", instance.SecurityGroupIds.SecurityGroupId.ToArray()),
"CPU:" + instance.Cpu + " Mem:" + instance.Memory + " MB",
instance.InstanceType,
instance.CreationTime,
instance.ExpiredTime,
cloudAssistantStatus
);
count++;
LogSuccess(string.Join(",", instance.PublicIpAddress.IpAddress.ToArray()) + "--CPU:" + instance.Cpu + " Mem:" + instance.Memory + " MB");
}
}
catch
{
LogError(regionId + " Region Search Failed,API Not Support.");
}
}
}
public delegate void LogAppendDelegate(Color color, string text);
//ID,实例ID,地区,主机名,主机系统,状态,私网IP,公网IP,安全组,实例配置,创建时间,到期时间
private void addItemToListView(
int count,
string InstanceId,
string regionId,
string HostName,
string OSType,
string Status,
string InnerIpAddress,
string PublicIpAddress,
string SecurityGroupId,
string srvOptins,
string InstanceType,
string CreationTime,
string ExpiredTime,
string cloudAssistantStatus
)
{
ListViewItem lvi = new ListViewItem(count.ToString());
lvi.SubItems.Add(InstanceId);
lvi.SubItems.Add(regionId);
lvi.SubItems.Add(HostName);
lvi.SubItems.Add(OSType);
lvi.SubItems.Add(Status);
lvi.SubItems.Add(InnerIpAddress);
lvi.SubItems.Add(PublicIpAddress);
lvi.SubItems.Add(SecurityGroupId);
lvi.SubItems.Add(srvOptins);
lvi.SubItems.Add(InstanceType);
lvi.SubItems.Add(CreationTime);
lvi.SubItems.Add(ExpiredTime);
lvi.SubItems.Add(cloudAssistantStatus);
this.srv_lvw.Invoke(new DelegateAddItem(addItem), lvi);
}
public void LogAppend(Color color, string text)
{
if (this.txt_log.Text.Length > 10000)
{
this.txt_log.Clear();
}
this.txt_log.SelectionColor = color;
this.txt_log.HideSelection = false;
this.txt_log.AppendText(text + Environment.NewLine);
}
/// <summary>
/// 显示错误日志
/// </summary>
/// <param name="text"></param>
public void LogError(string text)
{
LogAppendDelegate la = new LogAppendDelegate(LogAppend);
this.txt_log.Invoke(la, Color.Red, "[" + DateTime.Now + "] " + text);
}
/// <summary>
/// 显示警告信息
/// </summary>
/// <param name="text"></param>
public void LogWarning(string text)
{
LogAppendDelegate la = new LogAppendDelegate(LogAppend);
this.txt_log.Invoke(la, Color.Violet, "[" + DateTime.Now + "] " + text);
}
/// <summary>
/// 显示一般信息
/// </summary>
/// <param name="text"></param>
public void LogMessage(string text)
{
LogAppendDelegate la = new LogAppendDelegate(LogAppend);
this.txt_log.Invoke(la, Color.Black, "[" + DateTime.Now + "] " + text);
}
/// <summary>
/// 显示正确信息
/// </summary>
/// <param name="text"></param>
public void LogInfo(string text)
{
LogAppendDelegate la = new LogAppendDelegate(LogAppend);
this.txt_log.Invoke(la, Color.BlueViolet, "[" + DateTime.Now + "] " + text);
}
public void LogSuccess(string text)
{
LogAppendDelegate la = new LogAppendDelegate(LogAppend);
this.txt_log.Invoke(la, Color.Green, "[" + DateTime.Now + "] " + text);
}
delegate void DelegateAddItem(ListViewItem lvi);
private void addItem(ListViewItem lvi)
{
this.srv_lvw.Items.Add(lvi);
}
private void Form1_Load(object sender, EventArgs e)
{
LogMessage("Welcome");
}
private void ExecToolStripMenuItem_Click(object sender, EventArgs e)
{
functions func = new functions();
func.InstanceId = this.srv_lvw.SelectedItems[0].SubItems[1].Text;
func.RegionId = this.srv_lvw.SelectedItems[0].SubItems[2].Text;
func.AK_ID = AK_ID;
func.AK_SK = AK_SK;
func.SrvName = this.srv_lvw.SelectedItems[0].SubItems[3].Text;
func.PublicIP = this.srv_lvw.SelectedItems[0].SubItems[7].Text;
func.OS = this.srv_lvw.SelectedItems[0].SubItems[4].Text;
switch (this.srv_lvw.SelectedItems[0].SubItems[13].Text)
{
case "×":
fCloudAssistantStatus = "false";
break;
case "√":
fCloudAssistantStatus = "true";
break;
default:
fCloudAssistantStatus = "Unknow";
break;
}
func.cloudAssistantStatus = fCloudAssistantStatus;
func.StartPosition = FormStartPosition.CenterScreen;
func.ShowDialog(this);
}
public static string DescribeCloudAssistantStatus(AlibabaCloud.SDK.Ecs20140526.Client client, string instanceId, string regionId)
{
AlibabaCloud.SDK.Ecs20140526.Models.DescribeCloudAssistantStatusRequest req = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeCloudAssistantStatusRequest();
// 所在的地域ID
req.RegionId = regionId;
// 实例ID列表
req.InstanceId = new List<string>
{
instanceId
};
AlibabaCloud.SDK.Ecs20140526.Models.DescribeCloudAssistantStatusResponse resp = client.DescribeCloudAssistantStatus(req);
AlibabaCloud.SDK.Ecs20140526.Models.DescribeCloudAssistantStatusResponseBody.DescribeCloudAssistantStatusResponseBodyInstanceCloudAssistantStatusSet.DescribeCloudAssistantStatusResponseBodyInstanceCloudAssistantStatusSetInstanceCloudAssistantStatus status = resp.Body.InstanceCloudAssistantStatusSet.InstanceCloudAssistantStatus[0];
return status.CloudAssistantStatus;
}
private void CopyLineToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.srv_lvw.SelectedItems.Count == 0)
{
return;
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 1; i <= 12; i++)
{
sb.Append("----");
sb.Append(this.srv_lvw.SelectedItems[0].SubItems[i].Text);
}
Clipboard.SetText(sb.Remove(0, 4).ToString());
MessageBox.Show("复制成功!");
}
}
}