zoukankan      html  css  js  c++  java
  • 远程唤醒

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

    由各种百度,博客发现可以通过幻包远程进行唤醒:其电脑设置步骤此处省略,百度搜索远程唤醒即可找到,当然网上也有很多工具可以远程唤醒,由于楼主是学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;
             };
        }
    }

  • 相关阅读:
    Spring整合JMS(四)——事务管理
    centos7.3部署django用uwsgi和nginx[亲测可用]
    ImportError: Couldn't import Django.或者提示Django 模块不存在
    ModuleNotFoundError: No module named '_sqlite3'
    django-admin:command not found的解决办法
    2个版本并存的python使用新的版本安装django的方法
    uwsgi的使用
    plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
    cento7.3下玩转sphinx
    一些常用的centos命令,记忆下,属于常用的
  • 原文地址:https://www.cnblogs.com/gfjin/p/8079395.html
Copyright © 2011-2022 走看看