コード例 #1
0
ファイル: Program.cs プロジェクト: blaubart69/wol
        static int Main(string[] args)
        {
            try
            {
                Opts opts;
                if (!Opts.ParseOpts(args, out opts))
                {
                    return(99);
                }

                Stats stats = new Stats();

                IEnumerable <byte[]> MACs =
                    Parse.MACs(Misc.ConcatFilecontentAndOneValue(opts.MAC, opts.FilenameMacAddresses),
                               onParseError: (string MAC, string message) =>
                {
                    ++stats.errors;
                    Console.Error.WriteLine($"could not parse MAC [{MAC}]. [{message}]");
                });

                IEnumerable <IPAddress> broadcastIPsFromCidrs =
                    Parse.CIDRs(Misc.ConcatFilecontentAndOneValue(opts.cidr, opts.FilenameCIDRs),
                                onParseError: (string CIDR, string message) =>
                {
                    ++stats.errors;
                    Console.Error.WriteLine($"could not parse CIDR [{CIDR}]. [{message}]");
                });

                IEnumerable <IPAddress> broadcastIPsFromFile =
                    Parse.IPs(Misc.ConcatFilecontentAndOneValue(opts.broadcastIP, opts.FilenameBroadcastIPs),
                              onParseError: (string IP, string message) =>
                {
                    ++stats.errors;
                    Console.Error.WriteLine($"could not parse IP [{IP}]. [{message}]");
                });

                IReadOnlyList <IPAddress> broadcastEndpoints = broadcastIPsFromCidrs.Concat(broadcastIPsFromFile).ToList();

                if (opts.verbose)
                {
                    foreach (var ip in broadcastEndpoints)
                    {
                        Console.WriteLine($"broadcast IP: {ip}");
                    }
                    Console.WriteLine($"sending each MAC to {broadcastEndpoints.Count} IPs");
                }

                CreateSockets(broadcastEndpoints, out UdpClient v4Socket, out UdpClient v6Socket, opts.verbose);

                DateTime start = DateTime.Now;
                stats.numberMACs =
                    wol.SendEachMacToAllNets(MACs, broadcastEndpoints, WOL_UDP_PORT, v4Socket, v6Socket,
                                             onSendSuccessfull: (in IPAddress target) => { ++stats.sentPackets; },
                                             onSendError:       (in IPAddress target, in string error) => { ++stats.errors; Console.Error.WriteLine($"error sending to {target}\t[{error}]"); });
コード例 #2
0
        public static bool ParseOpts(string[] args, out Opts opts)
        {
            opts = null;
            bool showhelp = false;

            Opts tmpOpts = new Opts()
            {
            };
            var cmdOpts = new BeeOptsBuilder()
                          .Add('m', "mac", OPTTYPE.VALUE, "mac address for ", o => tmpOpts.MAC = o)
                          .Add(null, "macfile", OPTTYPE.VALUE, "input file with MAC addresses", o => tmpOpts.FilenameMacAddresses = o)
                          .Add('s', "broadcast", OPTTYPE.VALUE, "IP address to send to", o => tmpOpts.broadcastIP = o)
                          .Add(null, "broadcastfile", OPTTYPE.VALUE, "input file with subnet broadcast IPs", o => tmpOpts.FilenameBroadcastIPs = o)
                          .Add('c', "cidr", OPTTYPE.VALUE, "CIDR of subnet", o => tmpOpts.cidr = o)
                          .Add(null, "cidrfile", OPTTYPE.VALUE, "input file with CIDRs", o => tmpOpts.FilenameCIDRs = o)
                          .Add('v', "verbose", OPTTYPE.BOOL, "show some output", o => tmpOpts.verbose = true)
                          .Add('h', "help", OPTTYPE.BOOL, "show help", o => showhelp = true)
                          .GetOpts();

            var parsedArgs = BeeOpts.Parse(args, cmdOpts, (string unknownOpt) => Console.Error.WriteLine($"unknow option [{unknownOpt}]"));

            if (String.IsNullOrEmpty(tmpOpts.MAC) && String.IsNullOrEmpty(tmpOpts.FilenameMacAddresses))
            {
                Console.Error.WriteLine("no MAC of file with MACs given");
                showhelp = true;
            }
            if (String.IsNullOrEmpty(tmpOpts.broadcastIP) && String.IsNullOrEmpty(tmpOpts.FilenameBroadcastIPs) &&
                String.IsNullOrEmpty(tmpOpts.cidr) && String.IsNullOrEmpty(tmpOpts.FilenameCIDRs))
            {
                tmpOpts.broadcastIP = "255.255.255.255";
                if (tmpOpts.verbose)
                {
                    Console.WriteLine($"setting broadcast IP to {tmpOpts.broadcastIP}");
                }
            }

            if (showhelp)
            {
                Console.WriteLine(
                    "\nusage: wol.exe [OPTIONS]"
                    + "\n\nOptions:");
                BeeOpts.PrintOptions(cmdOpts);
                return(false);
            }

            opts = tmpOpts;
            return(true);
        }