성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - WOL(Wake On Lan) 구현</h1> <p> 예전에 구현했던,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 라즈베리 파이를 이용해 원격 컴퓨터의 전원 스위치 제어 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/11726'>https://www.sysnet.pe.kr/2/0/11726</a> Synology NAS(DS216+II)에 FTDI 장치 연결 후 C#(.NET Core)으로 DTR 제어 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/11734'>https://www.sysnet.pe.kr/2/0/11734</a> </pre> <br /> 전원 스위치를 이용해 원격으로 잘 제어하던 PC를 교체했더니,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 2020년 작업 PC ^^ ; <a target='tab' href='https://www.sysnet.pe.kr/0/0/522'>https://www.sysnet.pe.kr/0/0/522</a> </pre> <br /> 다시 저 피복 벗기고 하는 식의 작업을 새 PC에 하는 것이 귀찮아졌습니다. 그래도 꽤 괜찮게 써먹었던 기능이라서 없으면 무척 아쉬울 것 같아 차선책으로 WoL(Wake on Lan) 기능으로 넘어갔는데요, 검색해 보면 아래의 글이 꽤 적절하게 잘 설명하고 있기 때문에,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Wake On Lan(WOL) : 원격으로 컴퓨터 켜기 설정 및 사용 ; <a target='tab' href='https://neoray.org/281'>https://neoray.org/281</a> </pre> <br /> 자세한 것은 넘어가고, 곧바로 소스 코드 구현을 보겠습니다. 검색해 보면 아주 많은 소스 코드를 볼 수 있지만, 사실 프로토콜 자체가 매우 쉬우므로 아래와 같이 간단하게 구현할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text.RegularExpressions; namespace wol { class Program { const int WOL_PACKET_LEN = 102; // Wake-on-Lan (WoL) in C# // <a target='tab' href='https://www.fluxbytes.com/csharp/wake-lan-wol-c/'>https://www.fluxbytes.com/csharp/wake-lan-wol-c/</a> static void Main(string[] args) { <span style='color: blue; font-weight: bold'>byte[] wolBuffer = GetWolPacket(args[0]);</span> UdpClient udp = new UdpClient(); udp.EnableBroadcast = true; IPAddress ipAddress = IPAddress.Parse(<span style='color: blue; font-weight: bold'>"255.255.255.255"</span>); <span style='color: blue; font-weight: bold'>udp.Send</span>(wolBuffer, wolBuffer.Length, ipAddress.ToString(), 7); udp.Send(wolBuffer, wolBuffer.Length, ipAddress.ToString(), 9); } private static byte[] GetWolPacket(string macAddress) { byte[] datagram = new byte[WOL_PACKET_LEN]; byte[] macBuffer = StringToBytes(macAddress); MemoryStream ms = new MemoryStream(datagram); BinaryWriter bw = new BinaryWriter(ms); <span style='color: blue; font-weight: bold'>// 6바이트의 0xff를 선두에 채우고,</span> for (int i = 0; i < 6; i++) { bw.Write((byte)0xff); } <span style='color: blue; font-weight: bold'>// 이후 WoL로 깨울 PC가 소유한 Network Adapter의 MAC 주소를 16번 반복</span> for (int i = 0; i < 16; i++) { bw.Write(macBuffer, 0, macBuffer.Length); } return datagram; } private static byte[] StringToBytes(string macAddress) { macAddress = Regex.Replace(macAddress, "[-|:]", ""); // Remove any semicolons or minus characters present in our MAC address byte[] buffer = new byte[macAddress.Length / 2]; for (int i = 0; i < macAddress.Length; i += 2) { string digit = macAddress.Substring(i, 2); buffer[i / 2] = byte.Parse(digit, NumberStyles.HexNumber); } return buffer; } } } </pre> <br /> 여기서 재미있는 것은 Port인데요, 여러 소스 코드들을 보면 <a target='tab' href='https://www.fluxbytes.com/csharp/wake-lan-wol-c/'>0번</a>, <a target='tab' href='https://docs.microsoft.com/en-us/archive/blogs/knom/wake-on-lan-client-with-c'>3번</a>, <a target='tab' href='https://gallery.technet.microsoft.com/scriptcenter/Wake-on-LAN-for-Hyper-V-21578819'>7번</a>, 9번 등을 사용해 다소 혼란스러울 수 있는데 아래의 문서를 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Wake-on-LAN ; <a target='tab' href='https://en.wikipedia.org/wiki/Wake-on-LAN#Magic_packet'>https://en.wikipedia.org/wiki/Wake-on-LAN#Magic_packet</a> </pre> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > although it is typically sent as a UDP datagram to port 0, 7 or 9, or directly over Ethernet as EtherType 0x0842. </pre> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> The internet with local broadcasting - <span style='color: blue; font-weight: bold'>some routers permit a packet received from the internet to be broadcast to the entire LAN</span> [26]; the default TCP or UDP ports preconfigured to relay WOL requests are <span style='color: blue; font-weight: bold'>usually ports 7 (Echo Protocol) and/or 9 (Discard Protocol)</span>. This proxy setting must be enabled in the router, and port forwarding rules may need to be configured in its embedded firewall in order to accept magic packets coming from the internet side to these restricted port numbers, and to allow rebroadcasting them on the local network (normally to the same ports and the same TCP or UDP protocol). Such routers may also be configurable to use different port numbers for this proxying service. </div><br /> <br /> Router의 영향을 고려해 7번 또는 9번을 사용하는 것이 좋아 보입니다. (이 글의 소스 코드에서는 2개 모두 사용했습니다.) 또한, 255.255.255.255 자체의 Broadcasting에 대한 제약을 따져 봤을 때,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > UDP 브로드캐스트 주소 255.255.255.255와 192.168.0.255의 차이점과 이를 고려한 C# UDP 서버/클라이언트 예제 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/11368'>https://www.sysnet.pe.kr/2/0/11368</a> </pre> <br /> 모든 어댑터를 통해 보낼 수 있도록 Send 부분을 다음과 같이 변경해 주면 더 좋을 것입니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > foreach (IPAddress ipAddress in GetDirectedBroadcastAddresses()) { udp.Send(wolBuffer, wolBuffer.Length, ipAddress.ToString(), port); } private static IPAddress[] GetDirectedBroadcastAddresses() { List<IPAddress> list = new List<IPAddress>(); foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (item.NetworkInterfaceType == NetworkInterfaceType.Loopback) { continue; } if (item.OperationalStatus != OperationalStatus.Up) { continue; } UnicastIPAddressInformationCollection unicasts = item.GetIPProperties().UnicastAddresses; foreach (UnicastIPAddressInformation unicast in unicasts) { IPAddress ipAddress = unicast.Address; if (ipAddress.AddressFamily != AddressFamily.InterNetwork) { continue; } byte[] addressBytes = ipAddress.GetAddressBytes(); byte[] subnetBytes = unicast.IPv4Mask.GetAddressBytes(); if (addressBytes.Length != subnetBytes.Length) { continue; } byte[] broadcastAddress = new byte[addressBytes.Length]; for (int i = 0; i < broadcastAddress.Length; i++) { broadcastAddress[i] = (byte)(addressBytes[i] | (subnetBytes[i] ^ 255)); } list.Add(new IPAddress(broadcastAddress)); } } return list.ToArray(); } </pre> <br /> Github에도 소스 코드 및 빌드된 바이너리를 올려두었습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > stjeong / Utilities / wol ; <a target='tab' href='https://github.com/stjeong/Utilities/tree/master/wol'>https://github.com/stjeong/Utilities/tree/master/wol</a> Utilities.zip - wol.exe ; <a target='tab' href='https://github.com/stjeong/Utilities/releases/'>https://github.com/stjeong/Utilities/releases/</a> </pre> <br /> 참고로, 컴퓨터를 끄는 것은 "<a target='tab' href='https://docs.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page'>작업 스케줄러</a>"에 "<a target='tab' href='https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/shutdown'>shutdown</a> /h" 명령어를 원하는 시간에 실행하도록 등록하면 됩니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> 직접 실습을 해보니까, 제가 테스트한 4대의 컴퓨터 중에 구형 컴퓨터 2대(각각 <a target='tab' href='https://www.sysnet.pe.kr/0/0/280'>11년</a>, <a target='tab' href='https://www.sysnet.pe.kr/0/0/442'>8년</a>)는 BIOS 설정과 윈도우의 Adapter 속성창에서 WoL 관련 설정을 했지만 동작하지 않았습니다. 반면 <a target='tab' href='https://www.sysnet.pe.kr/0/0/499'>4년</a> 된 컴퓨터와 <a target='tab' href='https://www.sysnet.pe.kr/0/0/522'>새로 구매한 컴퓨터</a>는 정상 동작했습니다. (그러고 보니, 저 4대의 컴퓨터가 모두 ASUS 보드군요. ^^)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 검색하다가 낚인 것이 있어 ^^ 공유해 봅니다. 아래의 글에 대한 제목만 보면 Hyper-V의 가상 머신(VM)을 WoL 방식으로 깨우는 호스트 측의 기능이 있을 것 같은데요,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Wake on LAN for Hyper-V Guests ; <a target='tab' href='https://deploymentpros.wordpress.com/2016/11/28/wake-on-lan-for-hyper-v-guests/'>https://deploymentpros.wordpress.com/2016/11/28/wake-on-lan-for-hyper-v-guests/</a> PowerShell Script (psHyper-V_WoL.ps1) ; <a target='tab' href='https://gallery.technet.microsoft.com/scriptcenter/Wake-on-LAN-for-Hyper-V-21578819'>https://gallery.technet.microsoft.com/scriptcenter/Wake-on-LAN-for-Hyper-V-21578819</a> </pre> <br /> 실상은 Hyper-V 호스트가 VM에 대한 WoL을 지원하는 것은 아닙니다. 단지, 위의 스크립트가 UDP 소켓을 만들어 Receive로 대기하고 있다가 VM이 소유한 네트워크 어댑터의 WoL 신호가 들어오면 <a target='tab' href='https://docs.microsoft.com/en-us/powershell/module/hyper-v/start-vm?view=win10-ps'>Start-VM 명령어</a>를 이용하는 역할을 하는 것입니다.<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1016
(왼쪽의 숫자를 입력해야 합니다.)