我正在用C#构建一个Wake On Lan程序我已经找到了很多WOL的代码,但是到目前为止我找不到任何代码。我相信这是因为我无法启用IP定向广播(客户的安全策略不会启用此功能以防止DOS攻击)。
我正在寻找一种通过以太网将魔术数据包直接发送到请求的mac地址的方法 - 现在它似乎是通过UDP发送到255.255.255.255
(我不明白为什么它需要发送到255.255.255.255而不是发送给mac本身)
这是我现在的代码(不记得我在哪里找到它)。
public static bool WakeOnLan(string MacAddress)
{
try
{
MacAddress = MacAddress.Replace("-", "");
MacAddress = MacAddress.Replace(":", "");
if (MacAddress.Length != 12)
{
return false;
}
byte[] mac = new byte[6];
for (int k = 0; k < 6; k++)
{
mac[k] = Byte.Parse(MacAddress.Substring(k * 2, 2), System.Globalization.NumberStyles.HexNumber);
}
// WOL packet is sent over UDP 255.255.255.0:40000.
System.Net.Sockets.UdpClient client = new System.Net.Sockets.UdpClient();
client.Connect(System.Net.IPAddress.Broadcast, 4000);
byte[] packet = new byte[17 * 6];
for (int i = 0; i < 6; i++)
packet[i] = 0xFF;
for (int i = 1; i <= 16; i++)
for (int j = 0; j < 6; j++)
packet[i * 6 + j] = mac[j];
client.Send(packet, packet.Length);
return true;
}
catch
{
return false;
}
}
非常感谢任何帮助。
感谢名单
答案 0 :(得分:1)
WoL帧被发送到广播MAC地址Building BankingSkeleton 1.0-SNAPSHOT
------------------------------------------------------------------------
>>> jetty-maven-plugin:9.2.4.v20141103:run (default-cli) @ BankExample >>>
--- maven-dependency-plugin:2.6:copy (default) @ BankExample ---
--- maven-resources-plugin:2.5:resources (default-resources) @ BankExample ---
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
Copying 0 resource
--- maven-compiler-plugin:3.1:compile (default-compile) @ BankExample ---
Nothing to compile - all classes are up to date
--- maven-resources-plugin:2.5:testResources (default-testResources) @ BankExample ---
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory /Users/declanbarnes/Downloads/BankingSkeleton 2/src/test/resources
--- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ BankExample ---
Nothing to compile - all classes are up to date
<<< jetty-maven-plugin:9.2.4.v20141103:run (default-cli) @ BankExample <<<
--- jetty-maven-plugin:9.2.4.v20141103:run (default-cli) @ BankExample ---
2016-12-20 15:08:09.448:INFO::main: Logging initialized @4282ms
Configuring Jetty for project: BankingSkeleton
webAppSourceDirectory not set. Trying src/main/webapp
Reload Mechanic: automatic
Classes = /Users/declanbarnes/Downloads/BankingSkeleton 2/target/classes
Context path = /
Tmp directory = /Users/declanbarnes/Downloads/BankingSkeleton 2/target/tmp
Web defaults = org/eclipse/jetty/webapp/webdefault.xml
Web overrides = none
web.xml file = file:/Users/declanbarnes/Downloads/BankingSkeleton%202/src/main/webapp/WEB-INF/web.xml
Webapp directory = /Users/declanbarnes/Downloads/BankingSkeleton 2/src/main/webapp
2016-12-20 15:08:09.575:INFO:oejs.Server:main: jetty-9.2.4.v20141103
2016-12-20 15:08:40.428:INFO:oejsh.ContextHandler:main: Started o.e.j.m.p.JettyWebAppContext@9a7a808{/,file:/Users/declanbarnes/Downloads/BankingSkeleton%202/src/main/webapp/,AVAILABLE}{file:/Users/declanbarnes/Downloads/BankingSkeleton%202/src/main/webapp/}
2016-12-20 15:08:40.429:WARN:oejsh.RequestLogHandler:main: !RequestLog
2016-12-20 15:08:40.468:INFO:oejs.ServerConnector:main: Started ServerConnector@5fba7249{HTTP/1.1}{127.0.0.1:8080}
2016-12-20 15:08:40.469:INFO:oejs.Server:main: Started @35306ms
Started Jetty Server
Starting scanner at interval of 3 seconds.
Dec 20, 2016 3:08:56 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.7 2014-03-12 18:11:31...
Dec 20, 2016 3:08:57 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Dec 20, 2016 3:08:57 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
Dec 20, 2016 3:08:57 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Dec 20, 2016 3:08:57 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Dec 20, 2016 3:08:57 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Dec 20, 2016 3:08:57 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
Dec 20, 2016 3:08:57 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
Dec 20, 2016 3:08:57 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/bank]
Dec 20, 2016 3:08:57 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, autocommit=true, release_mode=auto}
Dec 20, 2016 3:08:58 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Dec 20, 2016 3:08:58 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Dec 20, 2016 3:08:58 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
2016-12-20 15:08:58.518:WARN:oejs.ServletHandler:qtp1532800776-19:
javax.servlet.ServletException: A MultiException has 2 exceptions. They are:|1. javax.persistence.PersistenceException: [PersistenceUnit: test] Unable to build EntityManagerFactory|2. java.lang.IllegalStateException: Unable to perform operation: create on com.mycompany.mavenproject1.AccountResource|
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:392)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:800)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1125)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1059)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:497)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:248)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:620)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:540)
at java.lang.Thread.run(Thread.java:745)
。为此,您必须将IP数据包发送到网络或有限的广播地址。广播不会跨越路由器,因为这是巨大的安全漏洞。
必须从不同网络发送WoL的实现通过在LAN上放置WoL服务器来执行此操作,并将命令发送到WoL服务器,然后将在LAN上发送WoL帧。
修改强>
如果您尝试在同一LAN上使用源和目标进行WoL,则可以使用LAN或有限广播,因为帧不会尝试通过路由器。
你真的不应该使用UDP。这可以通过以太网帧完成。只需将框架发送到ffff:ffff:ffff
即可。只需要IP地址就可以将数据包从一个网络传输到另一个网络。 LAN上的数据在第2层中传送,例如,以太网,帧。
您可以使用ffff:ffff:ffff
的EtherType,然后在帧有效负载中放入0x0842
,然后立即重复16次目标MAC地址。这就是&#34; Magic Packet&#34;因为它实际上是一个框架,而不是一个包。