Hello,
here's some code to get the MAC address for one or more computers on the LAN (but not the one running the code?!).
The function I've written behave differently depending on how you use it.
If you specify a computer name like rxGetMacAddr( 'ACOMPUTER' ) it will return information about that specific computer with one or more NICs.
If you leave out the computer name like rxGetMacAddr() it will return information about as many computers it can find on the LAN.
Remove the line stem = 'host.' to return information from rxGetMacAddr( 'ACOMPUTER' ) as a string and not as a stem.
Then you can PARSE VALUE rxGetMacAddr( 'ACOMPUTER' ) WITH name' 'macaddr' 'ipaddr'0D0A0D0A'x .
The code included in the example below behave like this:
host.1.name computername, like ACOMPUTER
host.1.macaddr mac address for computer ACOMPUTER in hex format, like 00:50:BA:DA:DD:A4
host.1.ipaddr ip address for computer ACOMPUTER in standard ip format, like 192.168.0.1
host.2.name computername, like COMPUTER2
... etc.
This example also contain code to send "Wake on Lan in REXX". Please see the original unmodified code (magic.cmd) in the original thread started by Andy P.
//Jan-Erik
here's some code to get the MAC address for one or more computers on the LAN (but not the one running the code?!).
The function I've written behave differently depending on how you use it.
If you specify a computer name like rxGetMacAddr( 'ACOMPUTER' ) it will return information about that specific computer with one or more NICs.
If you leave out the computer name like rxGetMacAddr() it will return information about as many computers it can find on the LAN.
Remove the line stem = 'host.' to return information from rxGetMacAddr( 'ACOMPUTER' ) as a string and not as a stem.
Then you can PARSE VALUE rxGetMacAddr( 'ACOMPUTER' ) WITH name' 'macaddr' 'ipaddr'0D0A0D0A'x .
The code included in the example below behave like this:
host.1.name computername, like ACOMPUTER
host.1.macaddr mac address for computer ACOMPUTER in hex format, like 00:50:BA:DA:DD:A4
host.1.ipaddr ip address for computer ACOMPUTER in standard ip format, like 192.168.0.1
host.2.name computername, like COMPUTER2
... etc.
This example also contain code to send "Wake on Lan in REXX". Please see the original unmodified code (magic.cmd) in the original thread started by Andy P.
Code Select
/*
* Filename: macaddr.cmd
* Author: JAN-ERIK LÄRKA
* Created: Mon Sep 10 2012
* Purpose: Retrieve MAC Address
* Changes:
*/
stem = 'host.'
count = rxGetMacAddr()
DO i = 1 TO count
/*magic.cmd*/
/*Wake on Lan in REXX*/
/*MAC = '11:22:33:44:55:66';*/
MAC = host.i.macaddr
SAY MAC;
A = X2C(SUBSTR(MAC,1,2));
B = X2C(SUBSTR(MAC,4,2));
C = X2C(SUBSTR(MAC,7,2));
D = X2C(SUBSTR(MAC,10,2));
E = X2C(SUBSTR(MAC,13,2));
F = X2C(SUBSTR(MAC,16,2));
X = A||B||C||D||E||F;
call RxFuncAdd 'SockLoadFuncs', 'rxsock', 'SockLoadFuncs';
call SockLoadFuncs( '*' );
SCT = SockSocket( 'AF_INET', 'SOCK_DGRAM', 'IPPROTO_UDP' );
RC = SockSetSockOpt( SCT, 'SOL_SOCKET', 'SO_BROADCAST', '1' );
RC = SockIOCtl( SCT, 'FIONBIO', '0' );
adr.!family = 'AF_INET';
/*adr.!addr = '255.255.255.255';*/
adr.!addr = host.i.ipaddr
adr.!port = 9;
S = COPIES( D2C(255) ,6 );
T = COPIES( X ,16);
G = COPIES( D2C(0) ,6 );
RC = SockConnect( SCT, "adr.!" );
STG = S||T||G;
SAY STG;
RC = SockSend( SCT, STG );
call SockSoClose SCT;
END
RETURN 0
rxGetMacAddr: PROCEDURE EXPOSE (stem) /*Optional parameter <COMPUTERNAME>*/
isStem = ( RIGHT( stem, 1 ) = '.' )
stemval = STRIP( stem, 'T', '.' )
retval = 0
i = 1
/* No parameters given, list all computers */
IF ARG() = 0 THEN
DO
DO WHILE QUEUED() > 0; PARSE PULL; END;
/* Computers on network */
'@NET VIEW 2>&1|RXQUEUE'
DO WHILE QUEUED() > 0
PARSE UPPER VALUE LINEIN( 'QUEUE:' ) WITH '\\'computer.i' '.
/* Add computer to list */
IF LENGTH( computer.i ) > 0 THEN i = i + 1
END
computer.0 = i - 1
END
ELSE DO
/* Search for a specific computer */
computer.0 = 1
computer.1 = TRANSLATE( ARG(1) )
END
j = 0
DO WHILE QUEUED() > 0; PARSE PULL; END;
DO i = 1 TO computer.0
'@PING '||TRANSLATE( computer.i )||' 1 1 2>&1|RXQUEUE'
DO WHILE QUEUED() > 0; PARSE PULL; END;
'@ARP '||TRANSLATE( computer.i )||' 2>&1|RXQUEUE'
DO WHILE QUEUED() > 0
PARSE UPPER VALUE SUBWORD( LINEIN( 'QUEUE:' ), 2 ) WITH machex +17 ipnum .
IF LENGTH( machex ) = 17 & LENGTH( ipnum ) > 7 THEN
DO
j = j + 1
IF isStem THEN
INTERPRET stemval'.j.name = computer.i;'||stemval'.j.macaddr = TRANSLATE( machex, "0", " " );'||stemval'.j.ipaddr = ipnum;'||stemval'.0 = j; retval = j'
ELSE
retval = retval||computer.i||' '||TRANSLATE( machex, "0", " " )||' '||ipnum||D2C(13)||D2C(10)
END
END
END
RETURN retval
//Jan-Erik