Windows下的netstat命令和nbtstat命令

Windows下的netstat命令和nbtstat命令

netstat和nbtstat可以说都是Windows下的网络检测工具,他们的输入形式很相似而且都是需要在安装了TCP/IP协议以后才可以使用的,但两者的功能却不同。

======= netstat命令 =======
Displays protocol statistics and current TCP/IP network connections. 
显示协议统计和当前的 TCP/IP 网络连接。

“Netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告”。

其格式如下:

NETSTAT [-a] [-b] [-e] [-n] [-o] [-p proto] [-r] [-s] [-v] [interval]

简述各个参数的含义:

-a 显示所有连接和监听端口。

-b 显示包含于创建每个连接或监听端口的可执行组件。在某些情况下已知可执行组件拥有多个独立组件,并且在这些情况下包含于创建连接或监听端口的组件序列被显示。这种情况下,可执行组件名在底部的 [] 中,顶部是其调用的组件,等等,直到 TCP/IP 部分。注意此选项可能需要很长时间,如果没有足够权限可能失败。

-e 显示以太网统计信息。此选项可以与 -s选项组合使用。

-n 以数字形式显示地址和端口号。

-o 显示与每个连接相关的所属进程 ID。

-p proto显示proto 指定的协议的连接;proto 可以是下列协议之一: TCP、UDP、TCPv6 或UDPv6。如果与 -s 选项一起使用以显示按协议统计信息,proto 可以是下列协议之一: IP、IPv6、ICMP、ICMPv6、TCP、TCPv6、UDP 或 UDPv6。

-r 显示路由表。

-s 显示按协议统计信息。默认地,显示 IP、IPv6、ICMP、ICMPv6、TCP、TCPv6、UDP 和 UDPv6 的统计信息;-p 选项用于指定默认情况的子集。

-v 与 -b 选项一起使用时将显示包含于为所有可执行组件创建连接或监听端口的组件。

interval 重新显示选定统计信息,每次显示之间暂停时间间隔(以秒计)。按 CTRL+C 停止重新显示统计信息。如果省略,netstat 显示当前配置信息(只显示一次)。

--------------
-a Displays all connections and listening ports. 
显示所有连接和侦听端口。 
此命令可以显示出你的计算机当前所开放的所有端口,其中包括TCP端口和UDP端口。有经验的管理员会经常的使用它,以此来查看计算机的系统服务是否正常,是否被“黑客”留下后门,木马等。比如说我就有一个习惯,在刚刚装了系统配置好服务器以后我就会运行一下netstat -a看看系统开放了什么端口,并记录下来,以便以后作为参考使用,当发现有不明的端口时就可以及时的做出对策。由于这个参数同时还会显示出当前计算机有什么人的IP正连接着你的服务器,所以也是一种实时入侵检测工具,如发现有个IP连接着不正常的端口,你也可以及时做出有效对策。示例: 
C:\>netstat -a

Active Connections

Proto Local Address Foreign Address State 
TCP iceblood:ftp iceblood.yofor.com:0 LISTENING 
TCP iceblood:telnet iceblood.yofor.com:0 LISTENING 
TCP iceblood:smtp iceblood.yofor.com:0 LISTENING 
TCP iceblood:http iceblood.yofor.com:0 LISTENING 
TCP iceblood:https iceblood.yofor.com:0 LISTENING 
……………… 
TCP iceblood:1171 iceblood.yofor.com:3306 ESTABLISHED 
TCP iceblood:ms-sql-s iceblood.yofor.com:0 LISTENING 
TCP iceblood:3306 iceblood.yofor.com:1171 ESTABLISHED 
……………… 
UDP iceblood:ms-sql-m *:* 
UDP iceblood:4000 *:* 
UDP iceblood:4001 *:* 
UDP iceblood:4002 *:* 
从上面的情况就可以知道现在开放的TCP端口有ftp(21),telnet(23),smtp(25),http(80),https (443),1171连接着自己的mysql(3306),ms-sql-s(1433),UDP端口有ms-sql-m(1433),4000- 4002都是OICQ。

-e Displays Ethernet statistics. This may be combined with the -s option. 
显示以太网统计。该参数可以与 -s 选项结合使用。
这个参数正如所说的,将在下面再跟大家说。

-n Displays addresses and port numbers in numerical form. 
以数字格式显示地址和端口号(而不是尝试查找名称)。 
大家如果只输入netstat的话就会看见如下类似的结果: 
C:\>netstat

Active Connections

Proto Local Address Foreign Address State 
TCP iceblood:1171 iceblood.yofor.com:3306 ESTABLISHED 
TCP iceblood:3306 iceblood.yofor.com:1171 ESTABLISHED 
TCP iceblood:1219 202.109.72.40:6667 ESTABLISHED 
TCP iceblood:3566 SERVER-2:microsoft-ds ESTABLISHED 
你会发现这些和netstat -a有相同的地方,只不过netstat可以很清楚的列举出来当前和你连接的所有计算机,在Local Address和Foreign Address里你也发现大多数给出的只是计算机NetBios名,却还是不知道当前和你连接的IP,但如果你加上-n参数就不同了,示例如下: 
C:\>netstat -n

Active Connections

Proto Local Address Foreign Address State 
TCP 127.0.0.1:1171 127.0.0.1:3306 ESTABLISHED 
TCP 127.0.0.1:3306 127.0.0.1:1171 ESTABLISHED 
TCP 192.168.1.21:1219 202.109.72.40:6667 ESTABLISHED 
TCP 192.168.1.21:3566 192.168.1.3:445 ESTABLISHED 
TCP 192.168.1.21:3577 202.107.208.187:110 TIME_WAIT 
TCP 192.168.1.21:3578 192.168.1.24:445 ESTABLISHED 
对方的IP全部都出来了。其实-n参数其实也就是告诉netstat不解析对方计算机的NetBios名。


-p proto Shows connections for the protocol specified by proto; proto may be TCP or UDP. If used with the -s option to display 
per-protocol statistics, proto may be TCP, UDP, or IP. 
显示由 protocol 指定的协议的连接;protocol 可以是 tcp 或 udp。如果与 -s 选项一同使用显示每个协议的统计,protocol 可以是 tcp udp、icmp 或 ip。 
这个参数你可以指定查看什么协议的连接状态,比如我想查看当前计算机正在连接的所有TCP端口,示例如下: 
C:\>netstat -p tcp

Active Connections

Proto Local Address Foreign Address State 
TCP iceblood:1171 iceblood.yofor.com:3306 ESTABLISHED 
TCP iceblood:3306 iceblood.yofor.com:1171 ESTABLISHED 
TCP iceblood:1219 202.109.72.40:6667 ESTABLISHED 
…………

-r Displays the routing table. 
显示路由表的内容。 
这个没有特别的,可以输入netstat -r以后自己研究。


-s Displays per-protocol statistics. By default, statistics are shown for TCP, UDP and IP; the -p option may be used to specify a subset of the default. 
显示每个协议的统计。默认情况下,显示 TCP、UDP、ICMP 和 IP 的统计。-p 选项可以用来指定默认的子集。 
这个参数让我们来配合-e来使用。 
C:\>netstat -s -e 
Interface Statistics

Received Sent

Bytes 505385542 41745793 
Unicast packets 150106 150547 
Non-unicast packets 313008 807 
Discards 0 0 
Errors 0 0 
Unknown protocols 327149

IP Statistics

Packets Received = 379906 
Received Header Errors = 0 
Received Address Errors = 215043 
Datagrams Forwarded = 0 
Unknown Protocols Received = 0 
Received Packets Discarded = 0 
Received Packets Delivered = 166002 
Output Requests = 151620 
Routing Discards = 0 
Discarded Output Packets = 0 
Output Packet No Route = 0 
Reassembly Required = 0 
Reassembly Successful = 0 
Reassembly Failures = 0 
Datagrams Successfully Fragmented = 0 
Datagrams Failing Fragmentation = 0 
Fragments Created = 0

TCP Statistics

Active Opens = 1556 
Passive Opens = 1 
Failed Connection Attempts = 4 
Reset Connections = 143 
Current Connections = 4 
Segments Received = 141243 
Segments Sent = 140462 
Segments Retransmitted = 477

UDP Statistics

Datagrams Received = 15125 
No Ports = 9634 
Receive Errors = 0 
Datagrams Sent = 10628 
网络基本状态都在这里面,比如你接受了多少数据包,多少字节,有多少TCP端口打开,有多少UDP端口打开

interval Redisplays selected statistics, pausing interval seconds between each display. Press CTRL+C to stop redisplaying statistics. If omitted, netstat will print the current configuration information once. 
重新显示所选的统计,在每次显示之间暂停 interval 秒。按 CTRL+B 停止重新显示统计。如果省略该参数,netstat 将打印一次当前的配置信息。 
这个就是自己定义检查网络状况的时间的参数,比如我想每过10秒检查一次我的计算机当前TCP连接的状态你就输入netstat 10 -p tcp这样netstat就会每过10秒就把你所有的TCP端口检查一次。

--------------------------
打开命令提示符窗口,并键入:


C:/WINDOWS>netstat -an |find /i "listening"

如果用户想要将显示结果存到一个文件中(通常是文本文件),以备日后参考,可以使用重定向命令,如“>”或“>>”:

netstat -an |find /i "listening" > c:/openports.txt

我们可以将“listening”改为“established”,查看一下计算机到底通过哪些端口通信:

C:/WINDOWS>netstat -an |find /i "established"

注意:在Windows XP和Windows Server2003中,如果用户要得到与每个连接相关的所有自有进程的ID列表,可以输入执行“NETSTAT -O”:

C:/WINDOWS>netstat -ao |find /i "listening"
TCP   pro1:epmap   pro1.dpetri.net:0   LISTENING   860
TCP   pro1:microsoft-ds   pro1.dpetri.net:0   LISTENING   4
TCP   pro1:1025   pro1.dpetri.net:0   LISTENING   908
TCP   pro1:1084   pro1.dpetri.net:0   LISTENING   596
TCP   pro1:2094   pro1.dpetri.net:0   LISTENING   596
TCP   pro1:3389   pro1.dpetri.net:0   LISTENING   908
TCP   pro1:5000   pro1.dpetri.net:0   LISTENING   1068

用户可以访问[url]http://www.petri.co.il/download_free_reskit_tools.htm.[/url]

使用PULIST来找到PID和使用它的进程。例如,我们可能会发现计算机在TCP端口80上有一个与远程IP地址的连接,但是计算机上并没有打开Internet Explorer或其它的浏览器窗口。为了查看到底是什么进程在使用这个会话,我们使用如下的命令:

C:/WINDOWS>netstat -no
Active Connections
Proto Local Address Foreign Address State PID
TCP   192.168.0.100:2496   212.179.4.7:80   ESTABLISHED   1536

然后使用带有“FIND”参数的“PULIST”命令:


C:/WINDOWS>pulist |find /i "1536"
Process   PID   User
LUCOMS~1.EXE   1536   DPETRI/danielp

可以看出, DANIELP在运行着 LUCOMS~1.EXE,这是Symantec Live Update进程。

要查看所有打开的、已建立的、关闭的以及使用的端口,可以使用如下的命令:


C:/WINDOWS>netstat -a

  在Windows XP或2003中,我们可以使用-o开关:

C:/WINDOWS>netstat -ao


======= nbtstat命令 =======
Displays protocol statistics and current TCP/IP connections using NBT (NetBIOS over TCP/IP). 
该诊断命令使用 NBT(TCP/IP 上的 NetBIOS)显示协议统计和当前 TCP/IP 连接。

NBTSTAT [ [-a RemoteName] [-A IP address] [-c] [-n] [-r] [-R] [-RR] [-s] [-S] [interval] ]

-a (adapter status) Lists the remote machine's name table given its name 
使用远程计算机的名称列出其名称表。 
此参数可以通过远程计算机的NetBios名来查看他的当前状态。示例 
C:\>nbtstat -a iceblood

本地连接: 
Node IpAddress: [192.168.1.2] Scope Id: []

NetBIOS Remote Machine Name Table

Name Type Status 
--------------------------------------------- 
ICEBLOOD <00> UNIQUE Registered 
WORK <00> GROUP Registered 
ICEBLOOD <20> UNIQUE Registered 
WORK <1E> GROUP Registered 
ICEBLOOD <03> UNIQUE Registered 
ICEBLOOD$ <03> UNIQUE Registered 
LIU_ICEBLOOD <03> UNIQUE Registered

MAC Address = 00-D0-09-52-91-DC 
从上面就可以知道我的计算机当前计算机的NetBios名为iceblood属于work组或域,当前有liu_iceblood登陆的该计算机,全都出来了。当然你也可以把计算机名换为IP也就是netstat -a 192.168.1.21,效果和上面的一样。这就有点像UNIX/Linux的finger了,如果你经常去netstat -a一台NT主机,你也可以收集到一些对方计算机中的用户列表了。

-A (Adapter status) Lists the remote machine's name table given its IP address. 
使用远程计算机的 IP 地址并列出名称表。 
这个和-a不同的是就是这个只能使用IP,其实-a就包括了-A的功能了,我也不再介绍。

-c (cache) Lists NBT's cache of remote [machine] names and their IP addresses 
给定每个名称的 IP 地址并列出 NetBIOS 名称缓存的内容。 
这个参数表示的是在你的NetBIOS里缓存的你连接过的计算机的IP。示例: 
C:\>nbtstat -c

本地连接: 
Node IpAddress: [192.168.1.21] Scope Id: []

NetBIOS Remote Cache Name Table

Name Type Host Address Life [sec] 
------------------------------------------------------------ 
WORK <20> UNIQUE 192.168.1.20 597 
从上面就可以知道你刚刚和IP为192.168.1.20的计算机的NetBIOS连接过。而这个命令也提供给了“黑客”在入侵了对方的主机以后进而入侵到内部网的一个有利的线索。因为NetBIOS的Cache里储存的IP是对方已经信任你的计算机的IP。聪明的“黑客”当然也会从这个方便的地方入手了。

-n Lists local NetBIOS names. 
列出本地 NetBIOS 名称。 
此参数和netstat -a类似,只是这个是检查本地的,如果把netstat -a后面的IP换为自己的就和netstat -n的效果是一样的了。

-r Lists names resolved by broadcast and via WINS 
列出 Windows 网络名称解析的名称解析统计。在配置使用 WINS 的 Windows 2000 计算机上,此选项返回要通过广播或 WINS 来解析和注册的名称数。 
这个正如上面所说的,列出当前Windows 网络名称解析的名称解析统计。

-R Purges and reloads the remote cache name table 
清除 NetBIOS 名称缓存中的所有名称后,重新装入 Lmhosts 文件。 
这个参数就是清除netstat -c所能看见的Cache里的IP缓存的。

-S Lists sessions table with the destination IP addresses 
显示客户端和服务器会话,只通过 IP 地址列出远程计算机。 
此参数可以查看计算机当前正在会话的NetBIOS。示例: 
C:\>nbtstat -S

本地连接: 
Node IpAddress: [192.168.1.21] Scope Id: []

NetBIOS Connection Table

Local Name State In/Out Remote Host Input Output

-----------------------------------------

ICEBLOOD <00> Connected Out 192.168.1.22 8MB 316KB 
ICEBLOOD <03> Listening 
ICEBLOOD$ <03> Listening 
LIU_ICEBLOOD <03> Listening

从上面就可以知道我的计算机现在正在和192.168.1.22进行会话,看得出是在复制文件,而且是从对方往自己的计算机里复制。通过以上参数所看到的信息到了“黑客”的手中也是非常重要的线索了。

-s Lists sessions table converting destination IP 
显示客户端和服务器会话。尝试将远程计算机 IP 地址转换成使用主机文件的名称。 
此参数和-S差不多,只是这个会把对方的NetBIOS名给解析出来。

-RR (ReleaseRefresh) Sends Name Release packets to WINs and then, starts Refresh 
释放在 WINS 服务器上注册的 NetBIOS 名称,然后刷新它们的注册。

interval Redisplays selected statistics, pausing interval seconds between each display. Press Ctrl+C to stop redisplaying statistics. 
重新显示所选的统计,在每次显示之间暂停 interval 秒。按 CTRL+B 停止重新显示统计。如果省略该参数,netstat 将打印一次当前的配置信息。 
此参数和netstat的一样,nbtstat的是配合-s和-S一起使用的。

你可能感兴趣的:(windows,tcp,cache,服务器,statistics,protocols)