1. 描述
当我们想在操作系统里面查看网口和交换机连接的状态信息,我们可以使用lldptool这个工具。
2.LLDP协议
LLDP是一个数据链路层发现协议,LLDP协议使得接入网络的一台设备可以将其主要的能力,管理地址,设备标识,接口标识等信息发送给接入同一个局域网络的其它设备。lldptool工具采用的是LLDP协议,一般我们使用lldptool是为了得到设备的物理拓扑结构以及管理配置信息,比如说,和eth1网口相连的网络交换机的Vlan ID是多少?网络交换机的名称是什么?网线插在网络交换机的什么地方?
在这里我们不去讨论LLDP协议的架构和具体时序状态码等,这里仅仅介绍怎么用它。
LLDP协议有四种工作模式:
- TxRx:既发送也接收LLDP 帧
- Tx :只发送不接收LLDP 帧
- Rx:只接收不发送LLDP 帧
- Disable:既不发送也不接收
一般设置LLDP工作在TxRx模式。
3.lldptool的命令格式
lldptool <command> [options] [参数]
Options
-i [ifname]
specifies the network interface to which the command applies
-V [tlvid]
specifies the TLV identifier
The tlvid is an integer value used to identify specific LLDP TLVs. The tlvid value is the Type value for types not equal to 127 (the organizationally specific type). For organizationally specific TLVs, the tlvid is the value represented by the 3 byte OUI and 1 byte subtype - where the subtype is the lowest order byte of the tlvid.
The tlvid can be entered as a numerical value (e.g. 10 or 0xa), or for supported TLVs, as a keyword. Review the lldptool help output to see the list of supported TLV keywords.
-n
"neighbor" option for commands which can use it (e.g. get-tlv)
-g [bridge scope]
specify the bridge scope this command operates on. Allows to set and query all LLDP TLV modules for "nearest_bridge" (short: "nb"), "nearest_customer_bridge" ("ncb") and "nearest_nontpmr_bridge" ("nntpmrb") group mac addresses. Configurations are saved into independent sections in lldpad.conf. If no bridge scope is supplied this default to "nearest bridge" to preserve the previous behaviour.
-c <argument list>
"config" option for TLV queries. Indicates that the query is for the configuration elements for the specified TLV. The argument list specifies the specific elements to query. If no arguments are listed, then all configuration elements for the TLV are returned.
-r
show raw client interface messages
-R
show only raw Client interface messages
Commands
license
show license information
-h, help
show usage information
-v, version
show version information
-S, stats
get LLDP statistics
-t, get-tlv
get TLV information
-T, set-tlv
set TLV information
-l, get-lldp
get LLDP parameters
-L, set-lldp
set LLDP parameters
-q, quit
exit from interactive mode
4.lldptool使用举例
ip a查看启用网口:

先看一些有没有lldptool工具,如果没有的话yum装一下:
1[root@wow ~]# service lldpad status 2lldpad is stopped 3[root@wow ~]# service lldpad start 4Starting lldpad: [ OK ] 5[root@wow ~]#
先设置lldptool的工作模式:
1[root@wow ~]# lldptool set-lldp -i eth0 adminStatus=rxtx 2adminStatus = rxtx #这样就和网络交换机建立了邻居关系,接下来就可以查询连接的交换机信息了
使用lldptool查看网口与交换机连接状态:
1[root@wow ~]# lldptool -t -n -i eth0 2Chassis ID TLV 3 MAC: 48:xx:xx:xx:xx:51 #mac地址 4Port ID TLV 5 Ifname: GE1/0/1 #网线插在交换机上的端口位置 6Time to Live TLV 7 120 8System Name TLV 9 xx-xx-CE5850-xxxxx #交换机设备名 10System Description TLV 11 Huawei Versatile Routing Platform Software 12VRP (R) software, Version 8.150 (CE5855EI V200R002C50SPC800) 13Copyright (C) 2012-2017 Huawei Technologies Co., Ltd. 14HUAWEI CE5855-xxxx-xxx #华为的交换机 15 16System Capabilities TLV 17 System capabilities: Bridge, Router 18 Enabled capabilities: Bridge, Router 19Management Address TLV 20 IPv4: xx.xxx.xxx.xx #ip地址 21 Ifindex: 4 22 OID: +[) 23Port VLAN ID TLV 24 PVID: 25Port and Protocol VLAN ID TLV 26 PVID: 0, not supported, not enabled 27VLAN Name TLV 28 VID 3652: Name VLAN3652 29MAC/PHY Configuration Status TLV 30 Auto-negotiation supported and enabled 31 PMD auto-negotiation capabilities: 0xxxxx 32 MAU type: 1000 BaseXFD 33Link Aggregation TLV 34 Aggregation capable 35 Currently not aggregated 36 Aggregated Port ID: 0 37Maximum Frame Size TLV 38 9216 39End of LLDPDU TLV 40[root@wow ~]#
如果想利用lldptool对网口进行一些设置,可以参考手册,下面是在Linux上找男人看到的一些例子:
1Configure LLDP adminStatus to Receive and Transmit for interface eth2 #为网口eth2配置LLDP的收发工作模式 2lldptool -L -i eth2 adminStatus=rxtx 3lldptool set-lldp -i eth2 adminStatus=rxtx 4 5Disable the LLDP adminStatus for all interfaces #让所有网口都不可用LLDP 6lldptool set-lldp adminStatus=disabled 7Query the LLDP adminStatus for interface eth3 #查询网口eth3的LLDP状态 8lldptool -l -i eth3 adminStatus 9lldptool get-lldp -i eth3 adminStatus 10 11Query the LLDP statistics for interface eth3 #查询网口eth3的统计信息 12lldptool -S -i eth3 adminStatus 13lldptool stats -i eth3 adminStatus 14 15Query the local TLVs which are being transmitted for a given interface: #查询为给定接口传输的本地 16lldptool -t -i eth3 17lldptool get-tlv -i eth3 18 19Query the received neighbor TLVs received on a given interface: #查询给定接口上收到的接收邻居TLV 20lldptool -t -n -i eth3 21lldptool get-tlv -n -i eth3 22 23Query the value of the System Description TLV as received from the neighbor on a given interface: 24lldptool -t -n -i eth3 -V sysDesc 25lldptool get-tlv -n -i eth3 -V 6 26 27Disable transmit of the IEEE 802.3 MAC/PHY Configuration Status TLV for a given interface: 28lldptool -T -i eth3 -V macPhyCfg enableTx=no 29lldptool set-tlv -i eth3 -V 0x120f01 enableTx=no 30 31Enable transmit of the Port Description TLV for all interfaces: 32lldptool -T -V portDesc enableTx=yes 33lldptool set-tlv -V 4 enableTx=yes 34 35Query value of the transmit setting for the Port Description TLV for a given interface: 36lldptool -t -V portDesc -c enableTx 37lldptool get-tlv -V 4 -c enableTx 38 39Set a Management Address TLV on eth3 to carry IPv4 address 192.168.10.10 40lldptool -T -i eth3 -V mngAddr ipv4=192.168.10.10 41Set a Management Address TLV on eth3 to carry IPv6 address ::192.168.10.10 42lldptool -T -i eth3 -V mngAddr ipv6=::192.168.10.10 43 44Get the configured IPv4 address for the Management Address TLV on eth3 45lldptool -t -i eth3 -V mngAddr 46- 47 48c ipv4 49 50Get all configured attributes for the Management Address TLV on eth3 51lldptool -t -i eth3 -V mngAddr -c 52Query the current DCBX mode that will be used if lldpad is restarted. (this is not a persistent setting) 53lldptool 54- 55 56t -i eth3 -V IEEE-DCBX -c mode 57 58Reset the DCBX mode to be 'auto' (start in IEEE DCBX mode) after the next lldpad restart 59lldptool -T -i eth3 -V IEEE-DCBX mode=reset 60Enable transmit of the Edge Virtual Bridging TLV for interface eth4 61lldptool 62- 63 64i eth4 -T -V evbCfg enableTx=yes 65 66Configure EVB TLV to set reflective relay and RTE,ECP and VDP capabilities 67lldptool -T -i eth4 -V evbCfg fmode=reflectiverelay 68lldptool -T -i eth4 -V evbCfg capabilities=rte,ecp,vdp 69 70Enable transmit of the VDP for interface eth4 71lldptool -i eth4 -T -V vdp enableTx=yes 72Configurations per port have higher precedence than global configurations.