Get IP from CMD

January 9, 2012 at 16:24:38
Specs: Windows 7
Hello All,

I am simply trying to write ONLY the IPV4 address to a file using CMD, what is wrong with my script?

net use \\sharemachine\c$ /u:administrator password
ipconfig > ipfile.txt
for /f "tokens=* delims= " %%a in (ipfile.txt) do (echo %%a | find "IPv4 Address. . . . . . . . . . . :" > nul & if not errorlevel 1 echo %%a >> new)
for /f "tokens=* delims= " %%a in (new) do (%%a >> \\sharemachine\c$\Shared\VMip.txt)


See More: Get IP from CMD

Report •

#1
January 10, 2012 at 07:06:20
I didn't troubleshoot your script, but instead wrote it the way I would do it. Hope it helps.

@echo off
ipconfig | find "IPv4" > tmp.tmp
set /p var=<tmp.tmp
set var=%var:IPv4 Address. . . . . . . . . . . : =%
echo %var%


Report •

#2
January 20, 2012 at 09:16:57
or you can try this one line solution:

for /f "tokens=1-12 delims=:" %%a in ('ipconfig^|find "Address"') do echo %%b > filename.txt

see script below for more options

@echo off
setlocal EnableExtensions
for /f "tokens=1-12 delims=:" %%a in ('ipconfig^|find "Address"') do echo The IPAddress is :%%b
for /f "tokens=1-12 delims=:" %%a in ('ipconfig^|find "Subnet"') do echo The Netmask is :%%b
for /f "tokens=1-12 delims=:" %%a in ('ipconfig^|find "Gateway"') do echo The Gateway is :%%b
for /f "tokens=1-12 delims=:" %%a in ('ipconfig /all^|find "Physical"') do echo The MAC Address is:%%b
for /f "tokens=1-12 delims=:" %%a in ('ipconfig /all^|find "Servers"') do echo The DNS Server1 is:%%b
for /f "tokens=1-12 delims=:" %%a in ('ipconfig /all^|find "Dhcp Enabled"') do echo Is DHCP turned on?:%%b
endlocal


Report •
Related Solutions


Ask Question