I want to get the ipv4 192.168.1.* from the ipconfig. I'm learning scripting and want to make an output value message box that displays the ip and copies it to clipboard. I have already done this by using the registry, but that is computer-specific as the adapters are unique per computer. So how do I get the computer to grab the 192.168.1.* for any computer using ipconfig, and copy it to the clipboard?
-
1Which scripting language are you learning? – Gantendo Sep 15 '21 at 00:24
-
2Does it has to be by using ipconfig? If you have more than 1 network adapter like Ethernet, Wi-Fi or virtual adapters like for Virtual Machines machines, Teamviewer or VPN there may be more than 1 local IPV4 Address shown in the ipconfig command... – Ricardo Bohner Sep 15 '21 at 01:43
3 Answers
ipconfig /all|clip
So the first part is the ipconfig /all command. You already know that one.
Next is a vertical line |, called a pipe. It "pipes" the output of the command to the left of it to another place. clip simply means the clipboard.
Note that not every local IP starts with 192.168..; cool people like me use 10.0.0.*.
If you want just the IP address, and not the rest of the output of the command, please look at the accepted answer over at this question: How do I extract the IPv4 IP Address from the output of ipconfig
I edited that answer a bit so that it does what you want:
@echo off
setlocal
setlocal enabledelayedexpansion
rem throw away everything except the IPv4 address line
for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr /i "ipv4"`) do (
rem we have for example "IPv4 Address. . . . . . . . . . . : 192.168.42.78"
rem split on : and get 2nd token
for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
rem we have " 192.168.42.78"
rem split on . and get 4 tokens (octets)
for /f "tokens=1-4 delims=." %%c in ("%%b") do (
set _o1=%%c
set _o2=%%d
set _o3=%%e
set _o4=%%f
rem strip leading space from first octet
set _4octet=!_o1:~1!.!_o2!.!_o3!.!_o4!
echo !_4octet!|clip
)
)
)
rem add additional commands here
endlocal
Credits: DavidPostill
Copy that to a textfile. Rename the file whatever.bat. Doubleclick on it to run it. It is a Batch file.
- 4,615
- 1
- 18
- 30
-
Ok, this seems to do the trick! I do have a few questions though. So as I said I'm learning, what does the "rem" line in the bat mean? Also here is one very specific issue with this solution. For at least this computer I'm testing it on, I have assigned myself two IPv4 addresses, this script picks up only the second IPv4 which isn't the actual one in use at the moment. I was just wondering if it's possible to copy both the IPv4 addresses that are assigned. If not that's okay, I only really wanted this script to find the auto-assigned DCHP (if the computer has one of course). – SolidSouless Sep 15 '21 at 02:30
-
@SolidSouless Rem is a comment inside the batch. It stands for "Remark" and the batch basically skips all lines that start with Rem. Do you want the IP addresses or DHCP server or both? – Ricardo Bohner Sep 15 '21 at 05:42
Just open a command prompt:
ipconfig /all.
Then use Mark and Copy feature of Command.
Then copy and now it is on the clipboard.
Screen shots shown. There is more than necessary - you can just copy the IP address
.
.
I pasted into Notepad to show the result.
Autoconfiguration Enabled . . . . : Yes Link-local IPv6 Address . . . . . : IPv4 Address. . . . . . . . . . . : 192.168......(Preferred) Subnet Mask . . . . . . . . . . . : 255.255.255.0 Lease Obtained. . . . . . . . . . : Tuesday, September 14, 2021 1:20:05 PM Lease Expires . . . . . . . . . . : Wednesday, September 15, 2021 7:08:24 PM Default Gateway . . . . . . . . . : 192.168. DHCP Server . . . . . . . . . . . : 192.168. DHCPv6 IAID . . . . . . . . . . . : 165980064 DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-22-F6-47-43-54-EE-75-B2-63-DA DNS Servers . . . . . . . . . . . : 192.168. NetBIOS over Tcpip. . . . . . . . : Enabled
- 46,167
- 4
- 33
- 54
-
-
-
1I think they want the computer to do it. `So how do I get the computer to grab the 192.168.1.* for any computer using ipconfig, and copy it to the clipboard?` – Gantendo Sep 15 '21 at 00:16
As I said before in the comments you can have more than 1 local IP (version 4) depending on how many real or virtual network adapters you have installed and if they are active.
Here is a batch | VBS hybrid that I made that shows the IP addresses of your adapters and then saves it to the clipboard.
Copy the code to notepad and save it to a file with .bat extension...
@echo off
setLocal EnableDelayedExpansion
IF /i exist "MessageBox.vbs" del /q "MessageBox.vbs"
If /i exist "IP.tmp" del /q "IP.tmp"
set "Text1=x=msgbox^("Your local IPV4 Address has been copied to the clipboard:"^& vbCrLf ^& vbCrLf ^&"
For /f "skip=2 delims=" %%a in ('"wmic nicconfig where IPEnabled=True get Description,IPAddress /format:csv"') do for /f "tokens=2,3 delims=,;{" %%b in ("%%~a") do (
set /a Counter+=1
set IP[!Counter!]=%%b: %%c
)
for /L %%a in (1,1,%Counter%) do (
>>"IP.tmp" echo !IP[%%a]!
IF NoT %%a EQU %Counter% (set "Text2=!Text2!"!IP[%%a]!"^& vbCrLf &") else (set "Text2=!Text2!"!IP[%%a]!"^& vbCrLf")
)
>"MessageBox.vbs" echo %Text1%!Text2!,0, "Your Local IP Adresses"^)
type "IP.tmp" |clip
MessageBox.vbs
del /q Messagebox.vbs
del /q "IP.tmp"
- 3,903
- 2
- 18
- 12


