annotate ServerMonitor/Helpers.cs @ 5:b6fe203af9d5

Private key passwords and validation
author Brad Greco <brad@bgreco.net>
date Thu, 28 Feb 2019 21:19:32 -0500
parents 3142e52cbe69
children 052aa62cb42a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
1 using Renci.SshNet;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
2 using Renci.SshNet.Common;
b6fe203af9d5 Private key passwords and validation
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
3 using ServerMonitorApp.Properties;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.Collections.Generic;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using System.ComponentModel;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
7 using System.Drawing;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8 using System.Linq;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
9 using System.Net.NetworkInformation;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 using System.Text;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 using System.Windows.Forms;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13 namespace ServerMonitorApp
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 static class Helpers
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 public static void FormatImageButton(Button button)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
18 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
19 button.Image = new Bitmap(button.Image, button.Height - 8, button.Height - 8);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
20 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 public static string GetAllMessages(this Exception ex)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
24 return "\t" + (ex.InnerException == null ? ex.Message : ex.Message + Environment.NewLine + ex.InnerException.GetAllMessages());
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27 public static string GetDisplayName(Type type)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
28 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29 return type?.GetAttribute<DisplayNameAttribute>()?.DisplayName ?? type?.Name ?? "null";
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
30 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
32 public static bool IsNullOrEmpty(this string aString)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
34 return aString == null || aString.Trim() == string.Empty;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
35 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
36
2
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
37 public static string ConvertNewlines(this string aString)
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
38 {
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
39 return aString.Replace("\r\n", "\n").Replace('\r', '\n');
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
40 }
453ecc1ed9ea Disk space check
Brad Greco <brad@bgreco.net>
parents: 0
diff changeset
41
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
42 public static T GetAttribute<T>(this Type type) where T : Attribute
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
43 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
44 return type.GetCustomAttributes(typeof(T), false).SingleOrDefault() as T;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
45 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
46
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
47 public static Image GetIcon(this CheckStatus checkStatus)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
48 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
49 switch (checkStatus)
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
50 {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
51 case CheckStatus.Error: return Resources.error;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
52 case CheckStatus.Warning: return Resources.warning;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
53 case CheckStatus.Information: return Resources.info;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
54 case CheckStatus.Success: return Resources.pass;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
55 case CheckStatus.Running: return Resources.run;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
56 case CheckStatus.Disabled: return Resources.disable;
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57 default: return null;
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61 public static bool In(this Enum value, params Enum[] values) {
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 return values.Contains(value);
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63 }
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
64
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
65 // https://stackoverflow.com/a/8345173
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
66 /// <summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
67 /// Indicates whether any network connection is available.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
68 /// Filter connections below a specified speed, as well as virtual network cards.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
69 /// </summary>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
70 /// <returns>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
71 /// <c>true</c> if a network connection is available; otherwise, <c>false</c>.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
72 /// </returns>
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
73 public static bool IsNetworkAvailable()
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
74 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
75 if (!NetworkInterface.GetIsNetworkAvailable())
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
76 return false;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
77
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
78 foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
79 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
80 // discard because of standard reasons
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
81 if ((ni.OperationalStatus != OperationalStatus.Up) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
82 (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
83 (ni.NetworkInterfaceType == NetworkInterfaceType.Tunnel))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
84 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
85
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
86 // discard virtual cards (virtual box, virtual pc, etc.)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
87 if ((ni.Description.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0) ||
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
88 (ni.Name.IndexOf("virtual", StringComparison.OrdinalIgnoreCase) >= 0))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
89 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
90
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
91 // discard "Microsoft Loopback Adapter", it will not show as NetworkInterfaceType.Loopback but as Ethernet Card.
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
92 if (ni.Description.Equals("Microsoft Loopback Adapter", StringComparison.OrdinalIgnoreCase))
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
93 continue;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
94
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
95 return true;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
96 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
97 return false;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents: 2
diff changeset
98 }
0
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
99 }
3e1a2131f897 Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
Brad Greco <brad@bgreco.net>
parents:
diff changeset
100 }