Author Topic: Get MAC address of one or more computers on LAN  (Read 864 times)

jep

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 421
    • View Profile
Get MAC address of one or more computers on LAN
« on: 2012.09.10, 19:59:58 »
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.

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

DougB

  • Sr. Member
  • ****
  • Posts: 407
    • View Profile
Re: Get MAC address of one or more computers on LAN
« Reply #1 on: 2012.09.10, 23:26:50 »
Interesting approach. However, I use the SAMBA server, not PEER, so my NET commands are different. I cannot seem to find one that will produce a similar output to NET VIEW. FINDSMB.CMD seems to be close, but I am not sure if the output will work for what you are using it for. The output looks like:

Code: [Select]
                                *=DMB
                                +=LMB
IP ADDR         NETBIOS NAME     WORKGROUP/OS/VERSION
-------------------------------------------------------------------------------
192.168..100    IREBBS7        * DOMAIN/Win 95/unknown                       
192.168..101    IREBBS7        * DOMAIN/Win 95/unknown                       
192.168..108    IREBBS7        * DOMAIN/Win 95/unknown                       
192.168..110    IREBBS7        * DOMAIN/Win 95/unknown                       

The NETBIOS NAME (IREBBS7) is the name of the local machine. The IP ADDR entries are all of the machines that are online (or have been online recently), including the local machine. Also, note that the double dot in the address really should have a zero in the middle (192.168.0.100). FINDSMB.CMD is also not in the path, so a full path needs to be supplied.

There is also a "-D" parameter, that gives more information, including the MAC address, however, the MAC address is always listed as all zeros (apparently it doesn't work in eCS).

Any thoughts?