Computer Hope

Microsoft => Microsoft DOS => Topic started by: Abo-Zead on December 28, 2020, 08:16:49 PM

Title: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Abo-Zead on December 28, 2020, 08:16:49 PM
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?
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: zoomy on December 29, 2020, 04:37:18 AM
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Abo-Zead on December 29, 2020, 05:29:39 PM
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
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: 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 (https://www.lifewire.com/find-a-mac-address-using-an-ip-address-818132)
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: patio on December 31, 2020, 05:52:45 PM
He doesn't try...he just wants people writing code for him...
Not once has he posted anything he did himself.
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Abo-Zead on January 01, 2021, 10:49:41 AM
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 (https://www.lifewire.com/find-a-mac-address-using-an-ip-address-818132)


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

Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Hackoo on January 01, 2021, 02:42:46 PM
Ok, First,  we should proceed to your problem step by step
What result did you get when you type this command with your cmd ?  ???
Code: [Select]
getmac /s localhost /v /nh /fo csv | find /I "N/A"
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: 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
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Hackoo on January 02, 2021, 08:17:07 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 :
Code: [Select]
getmac /s localhost /v /nh /fo csvI got as result like this one :
Code: [Select]
"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é"
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Abo-Zead on January 02, 2021, 08:39:49 AM
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}"
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Abo-Zead on January 03, 2021, 01:31:18 AM
Any Idea, How to reduce the output to required needed IP and MAC only
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Hackoo on January 03, 2021, 02:56:41 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:
Code: [Select]
@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
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Abo-Zead on January 04, 2021, 08:37:15 PM
Try this batch to extract only MAC and reduce the output result:
Code: [Select]
@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
Code: [Select]
@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
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Hackoo on January 05, 2021, 03:16:17 AM
Refer to this thread How to get MAC address of remote computer (https://serverfault.com/questions/411054/how-to-get-mac-address-of-remote-computer/1048419#1048419)
Suppose that you a have the inputfile with computers or ip address list, you can give a try with batch file :
Hosts.txt
Code: [Select]
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
Code: [Select]
@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
::-------------------------------------------------------------------------------------
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Abo-Zead on January 05, 2021, 05:14:26 PM
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?
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Abo-Zead on January 06, 2021, 10:29:14 PM
Hackoo
After a lot of searches, I found the below code
Code: [Select]

@ECHO OFF
For /f %%a in (hosts.txt) do (
     ping -n 1 %%a | for /f "tokens=1-4* delims=: " %%i in ('find "Reply"') do (
           nbtstat -a %%a | for /f "tokens=1-2 delims=^=" %%x in ('find "MAC Address"') do (
           echo [ %%a   :  %%k   :  %%y   ]>> Host_IP_MAC.txt
echo [ %%a   :  %%k   :  %%y   ]
           )
     )
)

and after testing I found it works only with all workstations working under WinXp only and the rest of the workstations working under Win7, 8, 8.1, and 10 doesn't work, so kindly help If you have an idea to fix this bug.
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Koverland on December 07, 2021, 05:42:28 PM
I don't quite understand how you solved your problem? Like you, I have many different hostnames, but I do not know how to get information about all of them at once. Unfortunately, I do not understand the software part at all. This was done by my partner, who literally went on vacation two days ago. Therefore, I am literally at a dead end, I hope it will not be difficult for you to suggest a solution to my problem. I thought that I could somehow call all the hosts I needed using 192.168, but unfortunately I didn't succeed. So I stopped trying to do something by myself. Nowadays, literally any problem can be solved via the Internet, so I thought that my problem could also be solved.
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Abo-Zead on October 01, 2022, 03:20:43 AM
Dear Koverland
 Here is the mentioned code that i have it with the help of some good people here:
Code: [Select]
for /f %%a in ('type "Your_file"') do (
   set bHOSTUP=0
   ping -n 1 %%a -w 2000 |find "TTL=" > nul && set bHOSTUP=1
   IF !bHOSTUP! equ 1 (
      set g=%%a
      CALL :HOSTUP %%a
   ) else (
      set g=%%a
      CALL :HOSTDOWN %%a
   )
)
:HOSTUP
for /f "tokens=3 delims=," %%i in ('getmac /s %g% /v /nh /fo csv') do (
echo [%%a : %%~i] >>"Online MAC-Addresses.txt"

:HOSTDOWN
echo.[%g% : is Offline or not Exist]>>"Offline MAC-Addresses.txt"

And it works for me on windows 7 and latter
just insert your file name and be sure that the mentioned file in the same directory
Title: Re: How Can I get IP and MAC address for remote PCs over the network by batch file
Post by: Abo-Zead on October 07, 2022, 01:31:19 AM
Dear Koverland,

I hope my previous code helps you and I'm so sorry to see your reply too late as I didn't open the site for a while
and you should ask Hackoo for more help after you make your best efforts as he's from the best people here.