Sub topic 5
Network diagnostics: tracert, pathping, nslookup, and netstat
This chapter assumes you already know what an IP address, a default gateway, and DNS are. It also assumes you can open an elevated Command Prompt and run ping and ipconfig. Those tools tell you whether a host answers. The four tools here tell you where a path breaks, whether name resolution works, and which programs are using the network.
Choosing the tool
Each tool answers a different question.
| Tool | Question it answers | Needs admin? |
|---|---|---|
| `tracert` | Which routers does my traffic cross, and where does it stop? | No |
| `pathping` | Where along the path are packets being lost? | No (takes minutes to run) |
| `nslookup` | Does DNS return the right address, and which server answered? | No |
| `netstat` | Which connections and listening ports exist, and which process owns them? | `-b` needs admin |
A diagnostic order
A common method works from the inside out. You test the local stack first, then name resolution, then the route. That is the sequence in the spine's example.
ping -n 4 127.0.0.1
ping -n 4 example.com
tracert -d example.com
nslookup example.comping 127.0.0.1tests the local TCP/IP stack. It never leaves the machine. A failure points to the network software or a security product on this computer.ping example.comtests name resolution and reachability together. If pinging the IP address works but pinging the name fails, the problem is DNS.tracert -d example.comshows the route. The-dswitch skips reverse DNS lookups, so the output appears faster.nslookup example.comqueries DNS directly, so you can tell a DNS fault from a routing fault.
tracert
tracert sends packets with increasing Time To Live (TTL) values. TTL is a counter that each router lowers by one. A router that lowers it to zero discards the packet and reports back, which reveals that router's address. Each line of output is one hop, with three round-trip times.
Read the output this way:
*and "Request timed out" on one hop, with later hops responding normally, usually means that router does not answer these probes. It is not a fault.- Timeouts that continue from one hop to the end mean traffic stops at or after that hop.
- A large jump in time that stays high on every later hop suggests a slow link at that point.
- The first hop is normally your default gateway. If it times out, the problem is local.
Useful switches are -d (no name lookups), -h (maximum hops, default 30), and -w (timeout in milliseconds per reply).
pathping
pathping combines ping and tracert. It first traces the route. It then sends many packets to each hop over about 25 seconds per hop and reports the percentage lost at each one.
pathping -n -q 10 example.com-n skips name lookups and -q 10 sends 10 queries per hop instead of the default 100, which shortens the run. Compare the loss figure at each hop with the next. Loss that appears at one hop and carries through to the destination is real. Loss at one hop that disappears at later hops usually means that router deprioritizes replies to probes, and traffic passes through it fine.
nslookup
nslookup queries a DNS server and prints the answer. The first lines show which server responded. A line reading "Non-authoritative answer" means the reply came from a cache, not from the server that owns the domain.
nslookup example.com
nslookup example.com 8.8.8.8
nslookup -type=MX example.comThe second command asks a specific server. If it works while your default server fails, your configured DNS server is the problem. The third asks for mail exchanger records instead of address records.
Common record types:
| Record | Purpose |
|---|---|
| A | Name to IPv4 address |
| AAAA | Name to IPv6 address |
| MX | Mail server for the domain |
| CNAME | Alias pointing to another name |
| PTR | IP address back to a name (reverse lookup) |
If a name resolves to an old address, clear the local cache with ipconfig /flushdns, then query again.
netstat
netstat lists network connections and listening ports. By default it shows only established connections, so you normally add switches.
netstat -ano
tasklist | findstr /i "chrome"
taskkill /PID 1234 /F-ashows all connections and listening ports.-nshows numbers instead of resolving names, which is faster.-oadds the process ID (PID) that owns each connection.-bshows the executable name. It needs an elevated prompt and runs slowly.-rshows the routing table.-eshows Ethernet statistics.-sshows per-protocol statistics.
Common states in the output are LISTENING (waiting for connections), ESTABLISHED (active), TIME_WAIT (closing), and SYN_SENT (a connection attempt with no reply yet). Many SYN_SENT lines can mean a blocked destination.
To find what holds a port, locate the PID in netstat -ano, then match it with tasklist. Use taskkill /PID 1234 /F to end it. Replace 1234 with the real PID. The /F switch forces termination, so unsaved data in that program is lost. Use it only when you know what the process is. Unknown connections to unfamiliar addresses can indicate malware.
Common errors
- Reading a single timed-out hop in
tracertas the cause of an outage. - Running
pathpingand stopping it early. It shows no statistics until the run finishes. - Using
netstat -bwithout elevation. It fails with a message that the operation requires elevation. - Forgetting
-nand waiting for slow reverse lookups. - Trusting
nslookupalone. It queries DNS directly and can differ from what applications use, which also consults the local hosts file and cache.
Exercise
A user can ping 8.8.8.8 but cannot ping example.com. Which command do you run next, and what does the result tell you?
Show answer
Run nslookup example.com. The successful IP ping shows the network path works, so the fault is name resolution. If nslookup fails or returns a wrong address, check the configured DNS server, try nslookup example.com 8.8.8.8 to compare, and run ipconfig /flushdns.
Exercise
You suspect an unknown program is listening on port 8080. Which commands do you use to find it and end it?
Show answer
Run netstat -ano and find the line with port 8080 and its PID. Run tasklist | findstr PID (using that number) to identify the program. If it should be stopped, run taskkill /PID number /F.
Exercise
In a pathping report, hop 4 shows 40% loss, but hops 5 to 8 and the destination show 0% loss. Is hop 4 the problem?
Show answer
No. Loss that does not continue to later hops usually means hop 4 limits replies to probe traffic. Real forwarding loss would also appear at the later hops.