远程唤醒

生活工作中经常通过远程控制某台电脑,但是有些时候,我们想远程某个电脑却发现,电脑被断电或者关机了,此时必须的跑去开个机;

由各种百度,博客发现可以通过幻包远程进行唤醒:其电脑设置步骤此处省略,百度搜索远程唤醒即可找到,当然网上也有很多工具可以远程唤醒,由于楼主是学C#故发一下

远程唤醒的C#代码实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Gaofajin.Shell
{
  
    public class RemoteOpenComputer
    {
       public  string OpenComputer(string Mac)
        {
            string msg = "(同网段)执行代码成功!数据包:";
            try
            {
                byte[] packet = GetPacket(Mac);
                UdpClient udpClient = new UdpClient();
                udpClient.Connect(IPAddress.Broadcast,30000);
                udpClient.Send(packet,packet.Length);
                foreach (byte bt in packet)
                {
                    msg += bt.ToString();
                }
           
            }catch(Exception ex)
            {
                msg=ex.Message;
            }

            return msg;
        }

       public  string OpenComputer(string broadcastip,string mac)
        {
            string msg = "(不同网段)执行代码成功!数据包为:";
            try
            {
                byte[] ip = IPToBytes(broadcastip);
            byte[] packet = GetPacket(mac);
            IPAddress iPAddress = new IPAddress(ip);
            UdpClient udpClient = new UdpClient();
            udpClient.Connect(iPAddress,30000);
            udpClient.Send(packet,packet.Length);
            foreach(byte bt in packet)
                {
                    msg += bt.ToString();
                }
            }
            catch(Exception ex)
            {
                msg=ex.Message;
            }
            return msg;
        }

        Func<string,byte[]> IPToBytes = ip =>
               {
                   byte[] bts = new byte[4];
                   for(int i = 0;i<ip.Split('.').Length;i++)
                   {
                       bts[i]=byte.Parse(ip.Split('.')[i]);
                   }
                   return bts;
               };
        Func<string,byte[]> GetPacket=mac=>
        {
            byte[] bmac =MacTobytes(mac);
            byte[] packet = new byte[17*6];
            for(int i = 0;i<6;i++)
            {
                packet[i]=0xFF;
            }
            for(int j = 1;j<=16;j++)
            {
                for(int k = 0;k<6;k++)
                {
                    packet[j*6+k]=bmac[k];
                }
            }
            return packet;
        };
       static Func<string,byte[]> MacTobytes = mac =>
         {
             mac=(mac.Contains('-')) ? mac.Trim('-') : mac;
             if(mac.Length!=12)
             {
                 throw new Exception("不是有效的mac地址!");
             }
             byte[] bt = new byte[6];
             for(int i = 0;i<6;i++)
             {
                 bt[i]=Convert.ToByte(mac.Substring(i*2,2),16);
                 Console.Write(bt[i].ToString());
             }
             return bt;
         };
    }
}

posted @ 2017-12-21 11:45  淺笑嫣然  阅读(579)  评论(0编辑  收藏  举报