Skip to content

Instantly share code, notes, and snippets.

@Zaaphod
Created September 10, 2021 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zaaphod/d1b7bf7a29aa653e3b0b61a836a03f62 to your computer and use it in GitHub Desktop.
Save Zaaphod/d1b7bf7a29aa653e3b0b61a836a03f62 to your computer and use it in GitHub Desktop.
Program Windows_Mac;
{$Mode FPC}
Uses Windows,CRT,classes,sysutils,Process;
{=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=}
Function Get_IPv4(Connection:String):String;
Var
Strin_pos,dot_count,Last_dot:Byte;
Sequence_Good:Boolean;
IPv4:String;
Begin
Sequence_Good:=True;
Last_Dot:=0;
Dot_Count:=0;
for Strin_pos := 1 to length(Connection) do
Begin
If ((Connection[Strin_pos] < '0') Or (Connection[Strin_pos] > '9')) and (Connection[Strin_pos] <> '.') Then
Begin
Break
End;
If (Connection[Strin_pos] = '.') Then
Begin
If (Strin_pos = 1) Or (Strin_pos = Length(Connection)) or (Strin_pos = Last_dot+1) Then
Begin
Sequence_Good:=False;
Break;
end;
Inc(Dot_count);
Last_dot:=Strin_pos;
End;
IPv4:=IPv4+Connection[Strin_pos];
End;
If (Dot_Count=3) then
Get_IPv4:=IPv4
Else
Get_IPv4:='Not Foud';
End;
{=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=}
Function GetMac(getmactest: Boolean):String;
Const
BUF_SIZE = 2048; // Buffer size for reading the output in chunks
Var
AProcess : TProcess;
OutputStream : TStream;
BytesRead,StringCount : longint;
Buffer : array[1..BUF_SIZE] of byte;
MacAddress : string;
NetAdapterName,NetMac,NetIP : array[1..255] of String;
//NetAdapterMac : array[1..255] of String; //Only used in one place below for diagnostics
NetworkNumber : Byte;
StartCount : Byte;
FoundWinIpConfig : Boolean;
Begin
AProcess := TProcess.Create(nil);
//Comment the below file check out for Windows 98. IPConfig is in C:\Windows but this check fails
If FileExists ('c:\windows\system32\IPConfig.exe') then
Begin
AProcess.Executable := 'IPConfig';
AProcess.Parameters.Add('/All');
AProcess.Options := [poUsePipes];
AProcess.Execute;
OutputStream := TMemoryStream.Create;
repeat
BytesRead := AProcess.Output.Read(Buffer, BUF_SIZE);
OutputStream.Write(Buffer, BytesRead)
until BytesRead = 0;
AProcess.Free;
with TStringList.Create do
begin
OutputStream.Position := 0; // Required to make sure all data is copied from the start
LoadFromStream(OutputStream);
networknumber:=1;
If count>0 Then
Begin
Foundwinipconfig := False;
If getmactest Then
Textcolor(4);
For StringCount:=0 to Count-1 do
begin
StartCount:=stringcount;
If Foundwinipconfig And (length(strings[Stringcount])>2) then
Break;
If Not(Foundwinipconfig) And (Pos('IP Configuration',strings[stringcount])>0) then
Begin
Foundwinipconfig := True;
If getmactest Then
Textcolor(2);
End;
If GetmacTest Then
Begin
Writeln(Stringcount+1,Strings[Stringcount]);
End;
End;
If Foundwinipconfig Then
Begin
For stringcount:=Startcount to count-1 do
Begin
If getmactest Then
Begin
Textcolor(8);
Writeln(Stringcount+1,' ',Strings[Stringcount]);
End;
// check for something in first column, only network adapter are in this position
If (length(strings[Stringcount])>2) AND (strings[Stringcount][1]<>' ') And (strings[Stringcount][1]<>' ') then
Begin
netadaptername[networknumber]:=strings[Stringcount];
If getmactest Then
Begin
Textcolor(10);
Write(' ^ #',NetworkNumber);
Textcolor(11);
write(' Network Adapter Found: ');
Textcolor(14);
Write(netadaptername[networknumber]);
Textcolor(10);
Writeln(' ^');
//Readkey; // Uncomment to pause for each network
End;
End;
// look for Physical Address and record Mac Address
If Pos('Physical Address',strings[stringcount])>0 then
Begin
netmac[networknumber]:= copy(strings[Stringcount],length(strings[Stringcount])-16,17);
//writeln(netadaptername[networknumber]);
If getmactest Then
Begin
//netadaptermac[networknumber]:= strings[Stringcount]; {Only used in writeln below. If Used, uncomment in VAR as well}
//writeln(netadaptername[networknumber]);
Textcolor(10);
Write(' ^ #',NetworkNumber);
Textcolor(11);
Write(' Mac Address Found: ');
Textcolor(13);
Write(' ',netmac[networknumber]);
Textcolor(10);
Writeln(' ^');
End;
If NetworkNumber<254 then
inc(networknumber)
Else
Begin
writeln('Insanely, This system has over 255 networks!!!');
Break
End;
End;
// look for Physical Address and record Mac Address
If Pos('IPv4',strings[stringcount])>0 then
Begin
netIP[networknumber]:= Get_IPv4(copy(strings[Stringcount],length(strings[Stringcount])-22,15));
//writeln(netadaptername[networknumber]);
If getmactest Then
Begin
//netadaptermac[networknumber]:= strings[Stringcount]; {Only used in writeln below. If Used, uncomment in VAR as well}
//writeln(netadaptername[networknumber]);
Textcolor(10);
Write(' ^ #',NetworkNumber);
Textcolor(11);
Write(' IPv4 Address Found: ');
Textcolor(13);
Write(' ',netIP[networknumber]);
Textcolor(10);
Writeln(' ^');
End;
End;
//If getmactest then //Uncomment for line by line evaluation
//REadkey;
End;
Dec(Networknumber);
If getmactest Then
Begin
Textcolor(13);
writeln('--- Number of lines = ', Count, '---- Length: ',length(text),' Number of Networks: ',Networknumber);
End;
If getmactest then
Begin
Writeln;
for stringcount:=1 to networknumber do
Begin
Textcolor(10);
Write('#',stringcount,' ');
Textcolor(11);
Write(NetAdapterName[stringcount]);
If NetMac[stringcount] <> '' Then
Begin
Textcolor(13);
Write(' MAC: ',NetMac[stringcount]);
End;
If NetIP[stringcount] <> '' Then
Begin
Textcolor(14);
Write(' IPv4: ',NetIP[stringcount]);
End;
Writeln;
End;
End;
MacAddress:='Not Found';
If Networknumber=0 then
MacAddress:='No Addresses'
Else
Begin
for stringcount:=1 to networknumber do
Begin
If getmactest then
Writeln(NetAdapterName[stringcount]);
If Pos('ETHERNET ADAPTER ETH',Upcase(NetAdapterName[stringcount]))>0 then
Begin
If getmactest then
Begin
Textcolor(10);
Writeln('1st Ethernet adapter with a name that starts with ETH selected');
End;
MacAddress:=netmac[stringcount];
Break;
end;
End;
If MacAddress='Not Found' Then
Begin
If getmactest then
Writeln('No Ethernet found');
for stringcount:=1 to networknumber do
Begin
If getmactest then
Begin
Textcolor(2);
Writeln('Any adapter selected');
End;
MacAddress:=netmac[stringcount];
Break;
End;
End;
End;
End
Else
MacAddress:='No IP Config';
End
Else
MacAddress:='No Results';
Free
end;
OutputStream.Free;
End
Else
MacAddress:='IPConfig.exe not found';
If MacAddress='Not Found' then
GetMac:='FF-00-00-00-00-FF'
Else
If MacAddress='No IP Config' then
GetMac:='F0-F0-F0-F0-F0-F0'
Else
If MacAddress='No Results' then
GetMac:='10-10-10-10-10-10'
Else
If MacAddress='IPConfig.exe not found' then
GetMac:='01-01-01-01-01-01'
Else
If MacAddress='No Addresses' then
GetMac:='01-02-03-04-05-06'
Else
GetMac:=MacAddress;
If getmactest then
Begin
Textcolor(13);
writeln(MacAddress);
Textcolor(12);
End;
End;
{=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=+=-=}
Begin
Writeln(Getmac(True));
Readkey;
End.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment