How Can I get IP and MAC address for remote PCs over the network by batch file

Started by Abo-Zead, December 28, 2020, 08:16:49 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Abo-Zead

Hi All,
I have many hostnames included in a text file named "hosts.txt"
I want to create a batch file containing at "for loop" command which will Go through this batch
Also at the same time, I want that batch to create a text file named "Info.txt".
And every time a batch passes through the text file "hosts.txt", it creates a new line inside the text file "Info.txt" It contains the following information:
Hostname: IP: MAC address.

Example ;
-Hosts.txt contain :
Host1
Host2
Host3

-and Info.txt contains :
Hostname : IP : MAC Address

Note: All hosts in the same domain and I have admin privilege.
, So Can Anyone help me with that, please Using the command line?

zoomy


  • Enter the "arp" command with an "-a" flag.
  • Once you enter the command "arp -a" you'll receive a list with all ARP entries to the ARP Table in your computer.
  • The output will show a line with the IP address followed by the MAC address, the interface, and the allocation type (dynamic/static), etc.

Abo-Zead

Thanks
but you didn't get my point, I want to get all Hostnames located inside the text file then process it by bat file then output to another text file sorted looks like that Hostname: IP: MAC   for each host

Hackoo

First , You should make an effort and show us your try with your own code ! ;)
May be this can help you to construct your own code : How to Use an IP Address to Find a MAC Address

patio

He doesn't try...he just wants people writing code for him...
Not once has he posted anything he did himself.
" Anyone who goes to a psychiatrist should have his head examined. "

Abo-Zead

Quote from: Hackoo on December 31, 2020, 12:47:35 PM
First , You should make an effort and show us your try with your own code ! ;)
May be this can help you to construct your own code : How to Use an IP Address to Find a MAC Address


Dear Hacoo
Thanks for your gentle reply and here is my code

@echo off
echo All Information >"Hosts Info.txt"
echo. >>"Hosts Info.txt"
For /f %%d in (hosts.txt) do (
   echo. %%d >>"Hosts Info.txt"
   getmac /s %%d /v /nh >>"Hosts Info.txt"
   ping %%d |find "Ping statistics for" >>"Hosts Info.txt"
   echo._______________________________ >>"Hosts Info.txt"
)

pause
exit

but it's so dummied as I don't know but a few things about the batch programming, so I'm trying to do my best and I can't deal ever with many things like ( "tokens & delims" , " ~ " , " 1% " , " find "," findstr" ) What is these things means and how we use it

But I try to find what I want from some people's ideas and try to imitate them in some codes, but I do not reach the desired result most of the time due to my ignorance of most of the language of the batch files but I'm still trying to learn

- The previous getmac code give me the whole NIC MAC with manufacturere with many details For example:
Ethernet        Intel(R) Ethern 8C-Df-D5-4e-a8-72   \Device\Tcpip_{817345F1-e93E-44D7-B19a-70F0f4F73Ec4}
and i want only tha mac address  8C-Df-D5-4e-a8-72
And The previous Ping code give me also the whole the Ping statistics result for Example :
Ping statistics for 172.217.17.238: and i just need the IP only like that  172.217.17.238

And finally if you can to help me to put all the results to gether in one line you are highly appretiated and if you won't also you are highly appretiated for the previous & gentle help


Hackoo

Ok, First,  we should proceed to your problem step by step
What result did you get when you type this command with your cmd ?  ???
getmac /s localhost /v /nh /fo csv | find /I "N/A"

Abo-Zead

Unfortunately after tried this code on my PC at home I didn't get anything even after saving the result into a text file as attached screenshot, so I'll go to my work tomorrow then  I'll try the same code again on any workstation inside my domain and feed back to you with the result

Hackoo

Quote from: Abo-Zead on January 02, 2021, 07:51:25 AM
Unfortunately after tried this code on my PC at home I didn't get anything even after saving the result into a text file as attached screenshot, so I'll go to my work tomorrow then  I'll try the same code again on any workstation inside my domain and feed back to you with the result
No problem; can you post me the result of this command without find /I "N/A"
I mean like this :
getmac /s localhost /v /nh /fo csv
I got as result like this one :
"Ethernet","Broadcom NetLink (TM) Gigabit Ethernet","XX-XX-XX-XX-XX-XX","Support déconnecté"
"Wi-Fi","Qualcomm Atheros AR5BWB222 Wireless Network Adapter","XX-XX-XX-XX-XX-XX","N/A"
"Ethernet 2","TAP-Windows Adapter V9","XX-XX-XX-XX-XX-XX","Support déconnecté"
"Connexion réseau Bluetooth","Bluetooth Device (Personal Area Network)","XX-XX-XX-XX-XX-XX","Support déconnecté"

Abo-Zead

After using this code I got that result now

"Ethernet","Intel(R) Ethernet Connection I217-LM","XX-XX-XX-XX-XX-XX","\Device\T                                                            cpip_{827317F1-C9XX-XXD9-BXXD-7XXX9BXXXXE4}"
"Ethernet 2","TeamViewer VPN Adapter","XX-FF-A7-XX-XX-XX","Media disconnected"
"vEthernet (Default Switch)","Hyper-V Virtual Ethernet Adapter","00-15-5D-XX-XX-                                                            32","\Device\Tcpip_{AXXXXDE8-6B77-4XX3-A2XX-XXXF376BXX7F}"
"vEthernet (External switch 1)","Hyper-V Virtual Ethernet Adapter #2","XX-XX-XX-                                                            XX-XX-XX","\Device\Tcpip_{XXXD4XXX-CXXF-XXDA-97XX-CXXA7946XXX}"

Abo-Zead

Any Idea, How to reduce the output to required needed IP and MAC only

Hackoo

Quote from: Abo-Zead on January 03, 2021, 01:31:18 AM
Any Idea, How to reduce the output to required needed IP and MAC only
Try this batch to extract only MAC and reduce the output result:
@echo off
Title Extracting only MAC Address from GetMac Command
(
for /f "tokens=3 delims=," %%a in ('getmac /s localhost /v /nh /fo csv') do echo %%~a
)>"%~dp0MAC-Address.txt"
If Exist "%~dp0MAC-Address.txt" Start "" "%~dp0MAC-Address.txt" & Exit

Abo-Zead

Quote from: Hackoo on January 03, 2021, 02:56:41 AM
Try this batch to extract only MAC and reduce the output result:
@echo off
Title Extracting only MAC Address from GetMac Command
(
for /f "tokens=3 delims=," %%a in ('getmac /s localhost /v /nh /fo csv') do echo %%~a
)>"%~dp0MAC-Address.txt"
If Exist "%~dp0MAC-Address.txt" Start "" "%~dp0MAC-Address.txt" & Exit


Thanks, Hackoo

After test your code it works in my workplace but it doesn't work on my PC at home so maybe the problem in my OS at home
Kindly how we put this code under for loop? as it does not work with me in my code below

@echo off
for /f %%i in (hosts.txt) do (
Title Extracting only MAC Address from GetMac Command
(
for /f "tokens=3 delims=," %%a in ('getmac /s localhost /v /nh /fo csv') do echo %%~a
)>"%~dp0MAC-Address.txt"
)
If Exist "%~dp0MAC-Address.txt" Start "" "%~dp0MAC-Address.txt" & Exit

also how we can put it with the Hostname and IP in the same row to look like that as I asked in my first question?
Hostname: IP: MAC Address

Hackoo

Refer to this thread How to get MAC address of remote computer
Suppose that you a have the inputfile with computers or ip address list, you can give a try with batch file :
Hosts.txt
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
192.168.1.19
192.168.1.20

IP-MAC-Scan.bat
@echo off
Set "Copyright=by Hackoo 2021"
Title Get IP and MAC address for remote PCs over the network using batch %Copyright%
Mode con cols=90 lines=12
cls & color 0A & echo.
echo     ********************************************************************************
echo         Get IP and MAC address for remote PCs over the network %Copyright%
echo     ********************************************************************************
echo(
if _%1_==_Main_  goto :Main
:getadmin
    echo %~nx0 : self elevating
    set vbs=%temp%\getadmin.vbs
(
    echo Set UAC = CreateObject^("Shell.Application"^)
    echo UAC.ShellExecute "%~s0", "Main %~sdp0 %*", "", "runas", 1
)> "%vbs%"
    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
goto :eof
::-------------------------------------------------------------------------------------
:Main
set "InputFile=%~dp0Hosts.txt"
set "OutPutFile=%~dp0IP-MAC.txt"
If Exist "%OutPutFile%" Del "%OutPutFile%"

If Not Exist "%InputFile%" (
color 0C & echo "%InputFile%" does not exist. Please check it first !
Timeout /T 8 /NoBreak>nul & Exit
)

Netsh interface ip delete arpcache >nul 2>&1

@for /f "tokens=* delims=" %%H in ('Type "%InputFile%"') do (
Ping -n 1 %%H>nul
@for /f "tokens=2" %%M in ('arp -a %%H ^| find "%%H"') do (
echo %%H : %%M
echo %%H : %%M>>"%OutPutFile%"
)
)
If Exist "%OutPutFile%" Start "" "%OutPutFile%" & Timeout /T 1 /NoBreak>nul & Exit
::-------------------------------------------------------------------------------------

Abo-Zead

Appreciate your effort Hackoo but after test this code in some workstation inside the domain it gives me this message

ARP: Bad argument: %hostname%
I don't know why?