Link to home
Start Free TrialLog in
Avatar of BlueAlien
BlueAlien

asked on

Getting network MAC address

I have a problem.

I need to get the network MAC address under Win2000. I have a function which returns that MAC, but only if the computer is connected to a network. If the computer is offline it returns the address of LocalIP. The problem is, that I would need the network MAC address even if the computer is offline. Does somebody know how to get it?

Thanks.
Avatar of weaze1
weaze1

The Media Access Control address, is unique to the NIC (or other network device) If you can get it once, thats all you need. It does not change on or offline.
Avatar of BlueAlien

ASKER

I know that, but I need to get the MAC in offline mode. If the computer is online it`s no problem, but I can`t get it in offline mode.
Hi BlueAlien

Try   ipconfig/all this will give you MAC address irrespective of Machine being online or offline

Vj
sorry this is not possible to get a MAC address while the other pc is unpluged and if you wanna see the Mac address o any online pf you should try this (C:\nbtstat -a IP) replace word ip with the one ip which you have i have tested it on win98 win2k and winxp
sorry this is not possible to get a MAC address while the other pc is unpluged and if you wanna see the Mac address o any online pf you should try this (C:\nbtstat -a IP) replace word ip with the one ip which you have i have tested it on win98 win2k and winxp
sorry this is not possible to get a MAC address while the other pc is unpluged and if you wanna see the Mac address o any online pf you should try this (C:\nbtstat -a IP) replace word ip with the one ip which you have i have tested it on win98 win2k and winxp
I forgot to mention that I need to get the Mac address in a program, so I would prefer a source or some advice I could use in that program.
If the computer is not connected to a network, you can not get the mac from the network.  You have to go to the machine that you need the MAC of and type ipconfig -all
at the dos prompt.  
And by the way, it maybe listed as physical address, which is the same thing.  
Some network card like 3com have their mac address printed on it. On new model you can see a little sticker with EA: 000475b1a004

The EA number is the mac address.

If the computer is down it is impossible to see the mac address because the mac address table flush entries who didn't be access for a couple of minute.

try to start the computer with the network card you want to know the mac address, ping it and use the command arp -a to know what is the mac address associated with the ip you ping.

If you can start the computer , put the network card in an other computer.

 
 
What exactly are you trying to do ?

The only way you can get the MAC of a node whilst it's off , is if it's printed on the hardware ( and it usualy is )

As every one else says , if you bring it up , then at the command prompt type "ipconfig /all"

If you want it remotly , and it's on the same network ( as in netmask range ) , then just ping the IP , then do a "arp -a" that'll give you the MAC.

If you want it remotly , and the machines not on , then the best way would be to use a sniffer , with the filter set to only log the traffic from the desired IP. That'll give you the MAC in the packets.


You don't say how big the network is , or if you have access to the network switches.....
He's hacking...
Whether the computer is connected to the network or not, running 'ipconfig /all' will display the MAC address. If you want to do this programmatically the simplest way would be to use a WMI script.

http://www.microsoft.com/scripting

However, if the computer is not connected to the network, obviously, you will need physical access to the machine without some sort of NSA devised TEMPEST device which allows input =)
*Emebedded
I know that the MAC adrress is printed on the network card, but I need to get it programmatically. The situation is:

I`m writing a program which decides from the MAC adrress whether it is registred. The computer is always turned on, but it doesn`t has to be connected to a network. If it is connected, I can get the MAC adrress, but if the computer is disconnected from the network I can`t. The problem is, that if the connection is lost (for some reason) the program cannot be run because it cannot get the MAC adrress of the computer. So i would need a function which returns the MAC adrress of the local computer whether it is connected to a network or not.

I don`t know if it is possible, but I think it is.

These are the functions I use (GetMACAdrress returns the adrress):

const
  MODEM=1;
  LAN=  2;
  PROXY=4;
  BUSY= 8;

type
  THandle = integer;
  TAdapterStatus = record
    adapter_address: array [0..5] of char;
    filler: array [1..4 * SizeOf(char) + 19 * SizeOf(Word) + 3 * SizeOf(longword)] of
    Byte;
  end;

  THostInfo = record
    username: PWideChar;
    logon_domain: PWideChar;
    oth_domains: PWideChar;
    logon_server: PWideChar;
  end;

function GetMACAddress: string;
var
 AdapterList: TLanaEnum;
 NCB: TNCB;
begin
 FillChar(NCB, SizeOf(NCB), 0);
 NCB.ncb_command := Char(NCBENUM);
 NCB.ncb_buffer := @AdapterList;
 NCB.ncb_length := SizeOf(AdapterList);
 Netbios(@NCB);
 if Byte(AdapterList.length) > 0 then
   Result := GetAdapterInfo(AdapterList.lana[0])
 else
   Result := 'mac not found';
end;

function AdapterToString(Adapter: TAdapterStatus): string;
begin
  with Adapter do Result :=
      Format('%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x',
      [Integer(adapter_address[0]), Integer(adapter_address[1]),
      Integer(adapter_address[2]), Integer(adapter_address[3]),
      Integer(adapter_address[4]), Integer(adapter_address[5])]);
end;

function GetAdapterInfo(Lana: Char): String;
var
 Adapter: TAdapterStatus;
 NCB: TNCB;
begin
 FillChar(NCB, SizeOf(NCB), 0);
 NCB.ncb_command := Char(NCBRESET);
 NCB.ncb_lana_num := Lana;
 if Netbios(@NCB) <> Char(NRC_GOODRET) then
 begin
   Result := 'mac not found';
   Exit;
 end;

 FillChar(NCB, SizeOf(NCB), 0);
 NCB.ncb_command := Char(NCBASTAT);
 NCB.ncb_lana_num := Lana;
 NCB.ncb_callname := '*';

 FillChar(Adapter, SizeOf(Adapter), 0);
 NCB.ncb_buffer := @Adapter;
 NCB.ncb_length := SizeOf(Adapter);
 if Netbios(@NCB) <> Char(NRC_GOODRET) then
 begin
   Result := 'mac not found';
   Exit;
 end;
 Result :=
   IntToHex(Byte(Adapter.adapter_address[0]), 2) + '-' +
   IntToHex(Byte(Adapter.adapter_address[1]), 2) + '-' +
   IntToHex(Byte(Adapter.adapter_address[2]), 2) + '-' +
   IntToHex(Byte(Adapter.adapter_address[3]), 2) + '-' +
   IntToHex(Byte(Adapter.adapter_address[4]), 2) + '-' +
   IntToHex(Byte(Adapter.adapter_address[5]), 2);
end;

function GetMACAddress: string;
var
 AdapterList: TLanaEnum;
 NCB: TNCB;
begin
 FillChar(NCB, SizeOf(NCB), 0);
 NCB.ncb_command := Char(NCBENUM);
 NCB.ncb_buffer := @AdapterList;
 NCB.ncb_length := SizeOf(AdapterList);
 Netbios(@NCB);
 if Byte(AdapterList.length) > 0 then
   Result := GetAdapterInfo(AdapterList.lana[0])
 else
   Result := 'mac not found';
end;

function GetConnectionKind(var strKind: string): Boolean;
var
  flags: longword;
begin
  strKind := '';
  Result := InternetGetConnectedState(@flags, 0);
  if Result then
  begin
    if (flags and MODEM) = MODEM then strKind := 'Modem';
    if (flags and LAN) = LAN then strKind := 'LAN';
    if (flags and PROXY) = PROXY then strKind := 'Proxy';
    if (flags and BUSY) = BUSY then strKind := 'Modem Busy';
  end;
end;

function LocalIP: string;
type
  TaPInAddr = array[0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  phe: PHostEnt;
  pptr: PaPInAddr;
  Buffer: array[0..63] of Char;
  I: Integer;
  GInitData: TWSAData;
begin
  WSAStartup($101, GInitData);
  Result := '';
  GetHostName(Buffer, SizeOf(Buffer));
  phe := GetHostByName(buffer);
  if phe = nil then Exit;
  pPtr := PaPInAddr(phe^.h_addr_list);
  I    := 0;
  while pPtr^[I] <> nil do
  begin
    Result := inet_ntoa(pptr^[I]^);
    Inc(I);
  end;
  WSACleanup;
end;

function IPAddrToName(IPAddr: string): string;
var
  SockAddrIn: TSockAddrIn;
  HostEnt: PHostEnt;
  WSAData: TWSAData;
begin
  WSAStartup($101, WSAData);
  SockAddrIn.sin_addr.s_addr := inet_addr(PChar(IPAddr));
  HostEnt := gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
  if HostEnt <> nil then
    Result := StrPas(Hostent^.h_name)
  else
    Result := '';
end;

***********************************
*** To jatcan: I`m not hacking. ***
***********************************
OK-sounds like it though...

So, if you are PCA and the other PC is PCB:

PCA is connected to the network, but PCB is not, how, in the name of god do you expect to get the mac address if PCB is not connected to the network?

Magic?

IT HAS TO HAVE A NETWORK CONNECTION in order for you to get it's mac from a command originating from  PCA. Period. Unless you're program is gonna do magic, in which case can I beta test it for you? 'cause I really got to see this in action...

I am not trying to be smart here, but in order to get a mac from PCB via PCA there absolutely has to be a connection to the network. In other words the PCB has to be turned on and have a network cable plugged into it. It DOES NOT have to be logged into, only turned on. If it is on but there is no network cable plugged into it, then you have to walk over to PCB, sit down in front of it, log onto it, and run either winipcfg(windows9X) or ipconfig(NT), whether you run these programs or similar functions to the ones used by these programs is besides the point.

Only thing I suggest is that you write a function that will return the last known mac for PCB if it is NOT connected to the network, other than that it will be connected to the network and you have no problems with that?

Maybe I simply don't understand exactly what you are trying to do, I am not a programmer so maybe I'll just keep quiet and watch what happens with this thread.

Cheers.



I beleive I understand now.

I just disabled my network connection and ran ipconfig /all

this is the output:

C:\>ipconfig /all

Windows 2000 IP Configuration

        Host Name . . . . . . . . . . . . : mypc
        Primary DNS Suffix  . . . . . . . :
        Node Type . . . . . . . . . . . . : Broadcast
        IP Routing Enabled. . . . . . . . : No
        WINS Proxy Enabled. . . . . . . . : No

I little bit different than if the connection is enabled. I DO see you dilemna now.
I also searcjh the registry for the mac address with the conection enabled and disabled and did not find it, although I am sure it is there somewhere when the connection is enabled...
so I am now in watching/learning mode,

Cheers

J
ASKER CERTIFIED SOLUTION
Avatar of syswiz
syswiz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That loopback would be good if there was no network. But in my case the computer is mostly connected to the network. I would just need to get that MAC adrress while disconnected.
Just a comments on this.  There are really could be two mac addresses.  Most cards today support something call "Set Station Address" which some ethernet transports need (Older, like DECNET).  The MAC Address that is "set" depends on the address for the machines.  Please note that MOST transports do NOT do this, but some older ones doe.  Make sure that anything you do takes this into account.
I will ask my wife if her group can thhink of a way to do this(they're all a bunch of programmers), you will do it programmatically from PC-B or from a server when a network request is made? Such as dhcp?

Cheers.
Ok ...

1. You say the target computer is 'always turned on'.  That implies that it is not a laptop or other 'mobile' device.  Further, the assumption is that it is then 'booted' and otherwise operational.

2. You imply that it may sometimes 'not be connected' to the network.  I take this to mean that a network cable has been disconnected from the (Primary) NIC or walljack.

What is not explained is why someone would (apparently) frequently disconnect the patch cord from the PC or the walljack.  This is not a normal thing for a user to do.
But, let's assume it does happen, just for the record.


Solution ...

Admittedly, it is impractical, if not outright insensible, but you wanted a way to get that (Primary) MAC address when someone committed the unconscionable act of pulling the (Primary) plug, so here it is.


1. Install a second NIC in every PC, using a completely different Class Address from the 'Primary' network.

2. Install a metal shroud over the back of the 2nd NIC, screwed to the PC frame with Torx screws or some of the (Ugly Name) perverse screws with the stud in the middle of the opening as IBM and a few others delight in using (Ugly Name, again).  (Can you believe that they actually pay people to think up stuff like that?)  Odds are the user ain't gonna have one of those tools in their desk drawer or pocketbook.

3. Use a red patch cord for this 2nd NIC, and either ...
a. install a similar obstructionist cover over the walljack to prevent easy removal of the patch cord, or
b. really get nasty.  Remove the wallplate cover, pop the jack out of the housing, run the patch cord through the wallplate opening, plug it into the now recessed jack and then replace the wallplate using the (Ugly Name) screws mentioned above.

I have not yet accounted for the ruthless souls who could then resort to cutting the red patch cord with scissors, etc, but should such be a concern, you may wish to investigate some patch cords clad with Titanium or a similar resistant material.  That should at least slow'em down.


4.  Develop a software program that will use this alternate network to communicate with any PC which 'went offline' through the Primary network.

By your definition of the structure, you cannot be using DHCP or you would not know what IPaddress belonged to which specific machine.  Therefore, you would know both the Primary and Secondary network addresses assigned to a particular PC.

Develop a simple background program that will only respond to a command from the Secondary network controller, and will execute the 'winipcfg' or 'ipconfig' routine on the local PC, and report the results back to the inquiring source.

Knowing what the Primary network IPaddress should be for that queried machine, the program can then parse the response, and extract the MAC address of the Primary NIC.

Viola!  (So, contrary to popular opinion, you can indeed remotely get the Primary MAC address even if the PC is 'offline'!).

Hey, I already told you it was a stupid idea, but that don't mean it couldn't be sold to the Gov'ment!






BlueAlien:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.