TExcelTAD
Home › Windows CMD & Batch › ipconfig, ping & netstat

ipconfig, ping & netstat

When "the internet is down", three commands answer most questions in under a minute:

  1. ipconfig: does my PC have an address, a gateway and DNS servers?
  2. ping: can I reach that machine, and how fast?
  3. netstat: which connections are open, and which program is listening on a port?

Plus a few helpers: tracert (the route to a host), nslookup (DNS lookups) and curl (HTTP requests; built into Windows 10 and 11). None of them run in the playground (it has no network), so this lesson shows them as sample sessions from a Windows 11 PC and then uses runnable batch examples to process their output, which is what scripts actually do with them. The layers underneath (IP, TCP, ports, DNS) are explained in The Network Stack.

ipconfig

cmd
C:\Users\ada>ipconfig

Windows IP Configuration


Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . : home.arpa
   Link-local IPv6 Address . . . . . : fe80::1c2b:9e4f:7a10:33d5%12
   IPv4 Address. . . . . . . . . . . : 192.168.1.42
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

Ethernet adapter Ethernet:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Sample output. How to read it:

Line Meaning
IPv4 Address this PC's address on the local network. 169.254.x.x means DHCP failed and Windows picked an emergency address
Subnet Mask which addresses are "local" (here 192.168.1.0–192.168.1.255)
Default Gateway the router that forwards everything else to the internet. Empty gateway = no internet
Link-local IPv6 an fe80:: address every adapter gives itself; %12 is the interface number
Media disconnected no cable / not connected to Wi-Fi

The most useful switches:

Command Does
ipconfig /all adds MAC address (Physical Address), DHCP server, lease times and DNS Servers
ipconfig /release then /renew gives back and requests a new DHCP address
ipconfig /flushdns empties the DNS cache (after a site moved to a new server)
ipconfig /displaydns shows what's in that cache

Extracting the IPv4 address

A classic script task: "put my IP address in a variable". findstr picks the line, for /f "tokens=2 delims=:" takes what's after the colon, and a second for /f trims the leading space. The playground saves the ipconfig text to a file first; on your PC the in (...) part would be ('ipconfig ^| findstr /c:"IPv4 Address"'):

cmd · try it yourself
@echo off
(
echo Wireless LAN adapter Wi-Fi:
echo.
echo    Connection-specific DNS Suffix  . : home.arpa
echo    Link-local IPv6 Address . . . . . : fe80::1c2b:9e4f:7a10:33d5%%12
echo    IPv4 Address. . . . . . . . . . . : 192.168.1.42
echo    Subnet Mask . . . . . . . . . . . : 255.255.255.0
echo    Default Gateway . . . . . . . . . : 192.168.1.1
) > ipconfig.txt

for /f "tokens=2 delims=:" %%a in ('findstr /c:"IPv4 Address" ipconfig.txt') do (
  for /f "tokens=*" %%b in ("%%a") do set "MYIP=%%b"
)
for /f "tokens=2 delims=:" %%a in ('findstr /c:"Default Gateway" ipconfig.txt') do (
  for /f "tokens=*" %%b in ("%%a") do set "GATEWAY=%%b"
)
echo My address: %MYIP%
echo Gateway:    %GATEWAY%
for /f "tokens=1-3 delims=." %%a in ("%MYIP%") do echo Network:    %%a.%%b.%%c.0/24
My address: 192.168.1.42
Gateway:    192.168.1.1
Network:    192.168.1.0/24

Don't split the IPv6 line with delims=:: the address itself is full of colons.

ping

ping sends small ICMP "echo request" packets and waits for replies. Windows sends 4 and stops (Linux pings forever):

cmd
C:\Users\ada>ping example.com

Pinging example.com [203.0.113.10] with 32 bytes of data:
Reply from 203.0.113.10: bytes=32 time=18ms TTL=56
Reply from 203.0.113.10: bytes=32 time=21ms TTL=56
Reply from 203.0.113.10: bytes=32 time=17ms TTL=56
Request timed out.

Ping statistics for 203.0.113.10:
    Packets: Sent = 4, Received = 3, Lost = 1 (25% loss),
Approximate round trip times in milli-seconds:
    Minimum = 17ms, Maximum = 21ms, Average = 18ms

Sample output. time is the round-trip time; TTL is how many more router hops the reply could have survived (it starts at 64 or 128 and loses one per router, so 56 hints at roughly 8 hops from a Linux server). Request timed out. is a lost packet.

Option Effect
-n 10 send 10 pings (-t = until Ctrl+C)
-w 500 wait at most 500 ms per reply
-4 / -6 force IPv4 / IPv6
-l 1400 packet size in bytes
-a resolve an IP address to a name

A "no reply" does not always mean "down": many servers and firewalls (including Windows' own) block ping. And watch out for this exit-code trap: when a router answers Destination host unreachable., ping still counts a reply and exits with 0. Scripts that need "is it up?" therefore look for TTL= in the output: only real replies contain it.

cmd
ping -n 1 -w 1000 192.168.1.1 | find "TTL=" >nul && echo router is up || echo router is DOWN

Reading ping statistics

cmd · try it yourself
@echo off
(
echo Reply from 203.0.113.10: bytes=32 time=18ms TTL=56
echo Reply from 203.0.113.10: bytes=32 time=21ms TTL=56
echo Reply from 203.0.113.10: bytes=32 time=17ms TTL=56
echo Request timed out.
echo.
echo Ping statistics for 203.0.113.10:
echo     Packets: Sent = 4, Received = 3, Lost = 1 ^(25%% loss^),
echo Approximate round trip times in milli-seconds:
echo     Minimum = 17ms, Maximum = 21ms, Average = 18ms
) > ping.txt

set /a replies=0
for /f %%r in ('findstr /c:"TTL=" ping.txt') do set /a replies+=1
for /f "tokens=6 delims=,=" %%a in ('findstr /c:"Average" ping.txt') do set "avg=%%a"
set "avg=%avg: =%"
echo Replies: %replies% of 4
echo Average: %avg%
if %replies% LSS 4 echo WARNING: packet loss detected
Replies: 3 of 4
Average: 18ms
WARNING: packet loss detected

delims=,= splits the statistics line at commas and equals signs, so the pieces are Minimum, 17ms, Maximum, 21ms, Average, 18ms (with spaces) and token 6 is the average; %avg: =% deletes the spaces. Inside the parenthesized block, (, ) and % in the text are escaped as ^(, ^) and %%.

netstat

netstat lists TCP and UDP connections. The options almost everyone uses are -a (all, including listening ports), -n (numbers, no slow name lookups) and -o (owning PID), together netstat -ano:

cmd
C:\Users\ada>netstat -ano

Active Connections

  Proto  Local Address          Foreign Address        State           PID
  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       1032
  TCP    0.0.0.0:445            0.0.0.0:0              LISTENING       4
  TCP    127.0.0.1:8080         0.0.0.0:0              LISTENING       14220
  TCP    192.168.1.42:51234     140.82.112.4:443       ESTABLISHED     8812
  TCP    192.168.1.42:51240     203.0.113.10:443       TIME_WAIT       0
  TCP    [::]:135               [::]:0                 LISTENING       1032
  UDP    0.0.0.0:5353           *:*                                    2480

Sample output, shortened.

Column Meaning
Proto TCP or UDP
Local Address this PC's address and port. 0.0.0.0 / [::] = all interfaces; 127.0.0.1 = only reachable from this PC
Foreign Address the other side; 0.0.0.0:0 or *:* for a listening socket
State LISTENING (a server waiting), ESTABLISHED (connected), TIME_WAIT / CLOSE_WAIT (closing). UDP has no state
PID the owning process; look it up with tasklist /fi "pid eq 14220"

Other options: -b shows the program name (needs an elevated prompt), -r the routing table, -s per-protocol statistics, and a number such as netstat -ano 5 repeats every 5 seconds.

"Who is using port 8080?"

The most common netstat question, as a script: find the listening line, take its PID (token 5), then look the PID up in a process list (here a saved tasklist /fo csv /nh, see tasklist & taskkill):

cmd · try it yourself
@echo off
setlocal
set PORT=8080
(
echo   TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       1032
echo   TCP    127.0.0.1:8080         0.0.0.0:0              LISTENING       14220
echo   TCP    192.168.1.42:51234     140.82.112.4:443       ESTABLISHED     8812
echo   UDP    0.0.0.0:5353           *:*                                    2480
) > netstat.txt
(
echo "svchost.exe","1032","Services","0","14,120 K"
echo "chrome.exe","8812","Console","1","412,336 K"
echo "node.exe","14220","Console","1","61,904 K"
) > procs.csv

set "PID="
for /f "tokens=5" %%a in ('findstr /r /c:":%PORT% .*LISTENING" netstat.txt') do set "PID=%%a"
if not defined PID (
  echo Port %PORT% is free
  goto :eof
)
for /f "tokens=1 delims=," %%n in ('findstr /c:"\"%PID%\"" procs.csv') do echo Port %PORT% is used by %%~n ^(PID %PID%^)

echo.
echo UDP sockets have no State column, so the PID is token 4:
for /f "tokens=1,2,4" %%a in ('findstr /r /c:"^  UDP" netstat.txt') do echo %%a %%b PID %%c
Port 8080 is used by node.exe (PID 14220)

UDP sockets have no State column, so the PID is token 4:
UDP 0.0.0.0:5353 PID 2480

The regular expression :8080 .*LISTENING needs the space after the port number, otherwise port 80800 or :8080 as a foreign port could match too.

tracert, nslookup and curl

cmd
C:\Users\ada>tracert -d example.com

Tracing route to example.com [203.0.113.10]
over a maximum of 30 hops:

  1     2 ms     1 ms     1 ms  192.168.1.1
  2    11 ms    10 ms    12 ms  100.64.0.1
  3     *        *        *     Request timed out.
  4    17 ms    18 ms    17 ms  203.0.113.10

Trace complete.

C:\Users\ada>nslookup example.com
Server:  router.home.arpa
Address:  192.168.1.1

Non-authoritative answer:
Name:    example.com
Address:  203.0.113.10

C:\Users\ada>curl -sI https://example.com
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1256

Sample output. tracert lists every router on the way (three timings each; * = no answer from that hop, often harmless); -d skips the slow name lookups. nslookup asks the DNS server shown under Server:; "Non-authoritative" just means the answer came from a cache. curl is the real curl, the same tool as on Linux and macOS.

PowerShell equivalents

CMD PowerShell
ipconfig Get-NetIPConfiguration, Get-NetIPAddress -AddressFamily IPv4
ipconfig /flushdns Clear-DnsClientCache
ping host Test-Connection host
port check Test-NetConnection host -Port 443
netstat -ano Get-NetTCPConnection -State Listen (has an OwningProcess property)
nslookup Resolve-DnsName

Get-NetTCPConnection -LocalPort 8080 -State Listen | Select-Object OwningProcess answers the port question without any text parsing.

Learn more: The Network Stack, for /f: Parse Text, findstr: Search Text.

Test yourself

ipconfig shows IPv4 Address . . . : 169.254.37.12. What does that suggest?

  1. The PC has a public internet address
  2. DHCP failed and Windows assigned itself an automatic link-local address
  3. It is the address of the router
  4. IPv6 is disabled

Which command shows which process (PID) is listening on each port?

  1. ipconfig /all
  2. netstat -ano
  3. ping -a
  4. tracert -d

Why do robust scripts check ping output for TTL= instead of trusting the exit code?

  1. The exit code is always 1
  2. ping exits with 0 even when a router replies Destination host unreachable
  3. TTL is only printed with -t
  4. find can't read ping output