Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Send Magic Packet
Message
From
07/01/2011 11:11:29
Michael Curtis
Dahl Chase Diagnostic Services
Bangor, Maine, United States
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Send Magic Packet
Miscellaneous
Thread ID:
01495151
Message ID:
01495151
Views:
165
I'm trying to write an app that will let me turn on specific PCs in the middle of the night, do what ever maintenance I want to them, then shut them back down. I'd like to do it using only Visual Foxpro if it's possible. I know how to shut them off, and I found some code written by a French guy but that required an IP address but we use DHCP, so IP isn't knowable when the PC is off.

This is the code I found, but I haven't managed to make it work even with the port and IP address.
*! * Subject: Starting a remote machine
*! * * Author: C. Chenavier
*! * * Version: 1.00 - 31/05/2005
*! *
*! * The Wake-up frame is a particular data flow formed
*! * Of at least 16 times repetition of the Ethernet address
*! * Remote machine, preceded by a synchronization stream
* "* 6-byte value of 255.
*! *
*! * If the Ethernet address of the remote computer is 01:02:03:04:05:06 (6 bytes)
*! * Then the LAN controller of this machine is waiting for the following sequence:
*! * FFFFFFFFFFFF010203040506010203040506010203040506010203040506
*! * 010203040506010203040506010203040506010203040506010203040506
*! * 010203040506010203040506010203040506010203040506010203040506
*! * 010203040506010203040506
*! *
*! * * Example of use:
*! *
*! * = M.lOK Wakeup ("000b6ab505a6", "192168192255")



FUNCTION WakeUp

LPARAMETERS cMacAddress, nPort, cIP 

LOCAL I, lOK, cMagic, cBuff, nSocketHandle, cPort

#DEFINE AF_INET 2
#DEFINE SOCK_DGRAM 2
#DEFINE IPPROTO_UDP 17
#DEFINE SOCKET_ERROR -1
#DEFINE SOL_SOCKET      0xFFFF
#DEFINE SO_BROADCAST 0x20

M.cMacAddress = CHRTRAN(M.cMacAddress, ":- ", '')
M.cMagic = REPLICATE("FF",6)+REPLICATE(M.cMacAddress,16)
M.cBuff = ''
FOR I = 1 TO LEN(M.cMagic) STEP 2
    M.cBuff = M.cBuff + CHR(HexaToDeci(SUBSTR(M.cMagic, I, 2)))
ENDFOR

DECLARE INTEGER WSAStartup IN WS2_32.DLL ;
        INTEGER wVersionRequested, STRING lpWSAData

DECLARE INTEGER socket IN WS2_32.DLL ;
        INTEGER af, INTEGER type, INTEGER protocol

DECLARE INTEGER htons IN WS2_32.DLL ;
        INTEGER hostshort

DECLARE INTEGER inet_addr IN WS2_32.DLL ;
         STRING cp

DECLARE INTEGER setsockopt IN WS2_32.DLL ;
        INTEGER s, INTEGER level, INTEGER optname, STRING @ optval, INTEGER optlen

DECLARE INTEGER sendto IN WS2_32.DLL;
        INTEGER s, STRING @ buf, INTEGER buflen, INTEGER flags, STRING @ sockaddr, INTEGER addrlen

DECLARE INTEGER closesocket IN WS2_32.DLL ;
        INTEGER s

DECLARE INTEGER WSACleanup IN WS2_32.DLL

IF WSAStartup(0x202, REPLICATE(CHR(0),512)) = 0 && initialisation Winsock
   M.nSocketHandle = Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) && création du socket UDP
   IF M.nSocketHandle <> SOCKET_ERROR
      M.cPort = WordToStr(htons(IIF(EMPTY(M.nPort), 9, M.nPort)))
      M.cIP = dWordToStr(inet_addr(M.cIP))
      M.cRemoteAddr = WordToStr(AF_INET) + M.cPort + M.cIP + REPLICATE(CHR(0),8)
      M.cOptval = REPLICATE(CHR(255),4)
      M.nResult = setsockopt(M.nSocketHandle, SOL_SOCKET, SO_BROADCAST, @cOptval, LEN(M.cOptval)) && mode broadcast
      IF M.nResult <> SOCKET_ERROR
         M.nResult = sendto(M.nSocketHandle, @cBuff, LEN(M.cBuff), 0, @cRemoteAddr, LEN(cRemoteAddr)) && envoi de la trame
         M.lOK = (M.nResult <> SOCKET_ERROR)
      ENDIF
      =closesocket(M.nSocketHandle)
   ENDIF
   WSACleanup()
ENDIF

CLEAR DLLS WSAStartup, socket, htons, inet_addr ;
           setsockopt, sendto, closesocket, WSACleanup


RETURN M.lOK



*---------------------------------------------------------------- dWordToStr

FUNCTION dWordToStr

LPARAMETER nValeur

LOCAL nB0, nB1, nB2, nB3

IF M.nValeur < 0
   M.nValeur = 2^32 + M.nValeur
ENDIF
M.nB3 = BITRSHIFT(M.nValeur, 24)
M.nB2 = BITRSHIFT(M.nValeur - M.nB3*2^24, 16)
M.nB1 = BITRSHIFT(M.nValeur - M.nB3*2^24 - M.nB2*2^16, 8)
M.nB0 = MOD(M.nValeur, 2^8)

RETURN CHR(M.nB0)+CHR(M.nB1)+CHR(M.nB2)+CHR(M.nB3)


*---------------------------------------------------------------- WordToStr

FUNCTION WordToStr

LPARAMETER nValeur

RETURN CHR(MOD(nValeur,256)) + CHR(INT(nValeur/256))


*---------------------------------------------------------------- HexaToDeci

FUNCTION HexaToDeci

LPARAMETERS cHexa

RETURN EVALUATE("0x"+ALLTRIM(M.cHexa))
Next
Reply
Map
View

Click here to load this message in the networking platform