|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: CoreBuilder.Extensions |
| 4 | + * FILE: WhoAmIExtension.cs |
| 5 | + * PURPOSE: Extension for WhoAmI to return individual parameters like "ip" or "hostname". Specially designed for scripting. |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Linq; |
| 11 | +using System.Net.NetworkInformation; |
| 12 | +using Weaver.Interfaces; |
| 13 | +using Weaver.Messages; |
| 14 | + |
| 15 | +namespace CoreBuilder.Extensions |
| 16 | +{ |
| 17 | + /// <inheritdoc /> |
| 18 | + /// <summary> |
| 19 | + /// Extension for WhoAmI command to fetch individual parameters |
| 20 | + /// </summary> |
| 21 | + public sealed class WhoAmIExtension : ICommandExtension |
| 22 | + { |
| 23 | + /// <inheritdoc /> |
| 24 | + public string Name => "Who"; |
| 25 | + |
| 26 | + /// <inheritdoc /> |
| 27 | + public string Description => "Returns specific information of WhoAmI command (e.g., ip, hostname, username), you can join them or return a single one."; |
| 28 | + |
| 29 | + /// <inheritdoc /> |
| 30 | + public string Namespace => "System"; |
| 31 | + |
| 32 | + /// <inheritdoc /> |
| 33 | + public CommandResult Invoke(ICommand command, string[] args, Func<string[], CommandResult> executor) |
| 34 | + { |
| 35 | + if (args.Length == 0) |
| 36 | + { |
| 37 | + return CommandResult.Fail("No parameter specified. Example: whoami().who(ip,hostname) or whoami().who(ip)"); |
| 38 | + } |
| 39 | + |
| 40 | + try |
| 41 | + { |
| 42 | + var hostname = Environment.MachineName; |
| 43 | + var username = Environment.UserName; |
| 44 | + var domain = Environment.UserDomainName; |
| 45 | + |
| 46 | + var ips = NetworkInterface |
| 47 | + .GetAllNetworkInterfaces() |
| 48 | + .Where(n => n.OperationalStatus == OperationalStatus.Up) |
| 49 | + .SelectMany(n => n.GetIPProperties().UnicastAddresses) |
| 50 | + .Where(a => a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) |
| 51 | + .Select(a => a.Address.ToString()) |
| 52 | + .Distinct() |
| 53 | + .ToList(); |
| 54 | + |
| 55 | + string ipsJoined = ips.Any() ? string.Join(", ", ips) : "None"; |
| 56 | + |
| 57 | + // Build a list of lines for requested fields |
| 58 | + var lines = args.Select(arg => arg.ToLowerInvariant()) |
| 59 | + .Select(field => |
| 60 | + { |
| 61 | + return field switch |
| 62 | + { |
| 63 | + "hostname" => $"Hostname: {hostname}", |
| 64 | + "username" => $"Username: {username}", |
| 65 | + "domain" => $"Domain: {domain}", |
| 66 | + "ip" => $"IP: {ipsJoined}", |
| 67 | + "os" => $"OS: {Environment.OSVersion}", |
| 68 | + "64bitos" => $"64-bit OS: {Environment.Is64BitOperatingSystem}", |
| 69 | + "64bitprocess" => $"64-bit Process: {Environment.Is64BitProcess}", |
| 70 | + "processorcount" => $"Processor Count: {Environment.ProcessorCount}", |
| 71 | + "clrversion" => $"CLR Version: {Environment.Version}", |
| 72 | + _ => $"Unknown parameter: {field}" |
| 73 | + }; |
| 74 | + }).ToArray(); |
| 75 | + |
| 76 | + // Join lines into a single message |
| 77 | + string message = string.Join(Environment.NewLine, lines); |
| 78 | + |
| 79 | + return CommandResult.Ok(message: message); |
| 80 | + } |
| 81 | + catch (Exception ex) |
| 82 | + { |
| 83 | + return CommandResult.Fail($"WhoExtension failed: {ex.Message}"); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments