找回密码
 注册
查看: 414|回复: 0

Linux使用lldpd获取交换机端口信息

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2023-3-17 20:55:03 | 显示全部楼层 |阅读模式
1. 什么是lldpd?
LLDP 可以让你准确的知道服务器所连接的交换机端口号。
LLDP是一种工业标准协议,用于取代EDP或CDP等专用链路层协议。LLDP的目标是提供一个inter-vendor兼容机制,向相邻网络设备发送链路层通知。
lldpd是一个LLDP协议的一种实现,给各种Unixes使用。除此之外,它还支持一些专有协议。
1.1. lldpad与lldpd的对比
实现交换机端口的抓取,目前我知道的有两种工具,一种是lldpad,另一种是lldpd。
这两个工具做了一下对比,最终选择了lldpd。原因如下:
1.2. 安装lldpd
需要先配置好配置好epel-release源,然后直接yum安装即可。
[root@controller ~]# yum install -y epel-release
1 C4 z" F  m7 d4 OLoaded plugins: fastestmirror
, \) Z- Z3 d7 S. X. M. x: ^% y( S( _Loading mirror speeds from cached hostfile
; w0 \9 Y; G; K  Y0 f * base: ftp.sjtu.edu.cn
4 J" G0 f4 |% Y) o * extras: ftp.sjtu.edu.cn
! U$ B! A$ K' e# d8 s4 |5 i* x * updates: ftp.sjtu.edu.cn; n8 F: M, g) x
base                                                                                | 3.6 kB  00:00:00     
; e  U) N# m6 o; k" }extras                                                                              | 2.9 kB  00:00:00     
, L+ r* u- \8 J. R9 aupdates                                                                             | 2.9 kB  00:00:00     6 N3 P+ `% j3 C5 n' @  H5 c1 D
(1/2): extras/7/x86_64/primary_db                                                   | 249 kB  00:00:00     
+ P! C2 t+ o! R! z: G% F(2/2): updates/7/x86_64/primary_db                                                  |  20 MB  00:00:02     
, M. v, U% Z1 w* wResolving Dependencies
" i, A/ c' Y8 C/ {) m# m--> Running transaction check
5 y+ v4 B3 G& s0 H! J7 {& p3 F* K---> Package epel-release.noarch 0:7-11 will be installed
; ~' {/ m  [0 O# X( m) \0 u7 O8 o--> Finished Dependency Resolution
0 N9 ~/ `: D$ m( a( N% H- E6 ]2 \8 L- G( b7 J3 Y
Dependencies Resolved
6 k) I- o2 M) l; v' H
, E/ J9 G, \6 @  j, T===========================================================================================================0 \* v8 z/ @. T( T' y* c9 I  {( d
Package                       Arch                    Version               Repository               Size
* n- e& C1 l" y( }===========================================================================================================
  `; p5 p1 I5 QInstalling:
% W  c: O  m; p- c epel-release                  noarch                  7-11                  extras                   15 k3 {1 c8 e- F$ w

) r: c. y# E5 B' }Transaction Summary
8 {! }" d0 [- L6 [$ u  c% ?) v===========================================================================================================* n9 F/ [% _1 d" V. f2 W& q
Install  1 Package
8 T1 b8 ^: N! [3 l( ?1 C5 d
1 M/ w% E4 n4 A- ~7 y+ u; y; K1 BTotal download size: 15 k# a2 B1 f" Z* E* W$ v+ ~: H* Z. a, \5 Z
Installed size: 24 k
* g/ m% i/ B+ s) s' w6 G& h) b1 `Downloading packages:
. T- p5 w8 }: h4 f' bDelta RPMs disabled because /usr/bin/applydeltarpm not installed.
/ q$ v; L. g. X, Z! s! pepel-release-7-11.noarch.rpm                                                        |  15 kB  00:00:00     
1 b: ?4 |! U# o$ v( i2 JRunning transaction check" ^3 D8 [- E( Y7 T& r
Running transaction test
9 I8 v( Y4 D5 \, ?/ RTransaction test succeeded
' ~( Y) Q' N& A% B2 B, z0 ARunning transaction* H6 S  x0 s0 y; P# x- d
  Installing : epel-release-7-11.noarch                                                                1/1 & B& [& }4 o0 b% `; n
  Verifying  : epel-release-7-11.noarch                                                                1/1
4 w1 H6 x" T( r3 \
- H6 `% {: P: C' f8 e' [Installed:
: E  \# G8 h- Y* w  epel-release.noarch 0:7-11                                                                               - y3 x6 l. I" Y. Q

9 ]  ?3 G! k- L# O  Z, y; wComplete!
# _# m) P8 n( d- f% d/ T[root@controller ~]# yum install -y lldpd
& K5 q' S( ?" K4 m% m: d, e- v/ kLoaded plugins: fastestmirror0 r3 g; B6 W# G+ B3 U! o
Loading mirror speeds from cached hostfile
1 g; w# @7 T7 g5 G8 y; q  jepel/x86_64/metalink                                                                | 5.3 kB  00:00:00       G9 T4 }: D* i* E8 x) H
* base: ftp.sjtu.edu.cn& [3 M2 q9 |  ^/ [% ^
* epel: ftp.iij.ad.jp8 Y4 O. I  _5 h2 L. e5 S6 Y
* extras: ftp.sjtu.edu.cn
; {5 Q/ i7 B$ T2 a  m6 `! D * updates: ftp.sjtu.edu.cn9 g/ Y* v6 L% E, q, U
epel                                                                                | 4.7 kB  00:00:00     & O( B: _9 s* ~) e4 |. P
(1/3): epel/x86_64/group_gz                                                         |  99 kB  00:00:00     # x/ I! }6 M$ x, m7 r& ?$ W
(2/3): epel/x86_64/updateinfo                                                       | 1.0 MB  00:00:03     % |7 `7 M1 j* A: T% }
(3/3): epel/x86_64/primary_db                                                       | 7.0 MB  00:00:05     
! O' y: O' q5 h$ F' W  VResolving Dependencies
7 o9 i( F7 g3 T3 U3 y--> Running transaction check
6 ?2 ?2 n) X% D4 ?  G---> Package lldpd.x86_64 0:1.0.4-1.el7 will be installed% R9 J+ v/ V* Y- T
--> Processing Dependency: libevent-2.0.so.5()(64bit) for package: lldpd-1.0.4-1.el7.x86_64
2 n' B  |9 R/ T--> Processing Dependency: libnetsnmp.so.31()(64bit) for package: lldpd-1.0.4-1.el7.x86_64/ P; P  Z2 F* l3 P, ?
--> Processing Dependency: libnetsnmpagent.so.31()(64bit) for package: lldpd-1.0.4-1.el7.x86_64
9 B' {4 P! \! ^* s7 a--> Processing Dependency: libnetsnmpmibs.so.31()(64bit) for package: lldpd-1.0.4-1.el7.x86_648 L' J" w- p8 X
--> Processing Dependency: libsensors.so.4()(64bit) for package: lldpd-1.0.4-1.el7.x86_64
8 d9 J4 ~- g& K6 b--> Running transaction check, m9 S2 R( ?: c5 j9 M
---> Package libevent.x86_64 0:2.0.21-4.el7 will be installed. l& ^2 t, H9 z9 P5 O
---> Package lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7 will be installed5 l% i- c$ s0 |, N, ~  g
---> Package net-snmp-agent-libs.x86_64 1:5.7.2-49.el7_9.2 will be installed6 u& m0 A! u3 M3 ^1 i
---> Package net-snmp-libs.x86_64 1:5.7.2-49.el7_9.2 will be installed
) |  i3 s% E5 a8 v$ m0 Y& a% F5 C$ d--> Finished Dependency Resolution
" I' F7 ^0 k$ B8 Z0 Z& P  k/ A2 q
Dependencies Resolved; z  z- E! }3 q/ {

* s% ]7 ]; }% q# m, E- X===========================================================================================================# R4 n/ a! g9 I2 `' t. H' q
Package                     Arch           Version                                  Repository       Size; n, b, y9 k0 o6 N. [+ Y
===========================================================================================================; _! B5 F" ?. M/ i  B
Installing:
" s% l3 Y4 J- A) ?' L9 N lldpd                       x86_64         1.0.4-1.el7                              epel            173 k
- d' o1 U9 Y" d" }- G2 jInstalling for dependencies:) D% r+ n) H6 A3 b4 {
libevent                    x86_64         2.0.21-4.el7                             base            214 k8 I* |% K  ~6 ^$ v7 r
lm_sensors-libs             x86_64         3.4.0-8.20160601gitf9185e5.el7           base             42 k* s7 t" Q( R5 ^. S/ ^! ^5 c1 n
net-snmp-agent-libs         x86_64         1:5.7.2-49.el7_9.2                       updates         707 k
" X1 A' w: }4 S1 m net-snmp-libs               x86_64         1:5.7.2-49.el7_9.2                       updates         752 k5 x' K, P& a4 d0 t, `) J
. v8 e& K4 G7 z/ N1 d( x* r
Transaction Summary) j% g+ ~. ^$ M
===========================================================================================================* V6 w7 I7 D! D, T( l' y
Install  1 Package (+4 Dependent packages)2 N# ]3 U4 |8 h. ~" A6 t  E4 d  \" Y

0 b) `; {! C5 i7 T+ tTotal download size: 1.8 M5 C3 r) X4 \9 B8 l
Installed size: 6.2 M# [- g! Y/ R% x" L
Downloading packages:8 e; \  C  l$ w9 P/ q& _8 V1 y
(1/5): lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64.rpm                    |  42 kB  00:00:00     
: @7 {8 t& T$ \6 Y8 }(2/5): libevent-2.0.21-4.el7.x86_64.rpm                                             | 214 kB  00:00:00     
0 ^& c+ x, B/ b+ Y& q+ m9 u3 m(3/5): net-snmp-libs-5.7.2-49.el7_9.2.x86_64.rpm                                    | 752 kB  00:00:00     % k5 q- P! i! q
(4/5): net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64.rpm                              | 707 kB  00:00:00     * ?/ C/ D: ~9 \& I! S# o
warning: /var/cache/yum/x86_64/7/epel/packages/lldpd-1.0.4-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
8 [/ X% a% Q0 S: [* z( f  ?Public key for lldpd-1.0.4-1.el7.x86_64.rpm is not installed
; R% G' V7 B8 n3 R- e" a(5/5): lldpd-1.0.4-1.el7.x86_64.rpm                                                 | 173 kB  00:00:03     
' y. z1 `. A; X  o! U% M-----------------------------------------------------------------------------------------------------------
$ t. Z' t2 L8 k4 C7 J, S, ZTotal                                                                      614 kB/s | 1.8 MB  00:00:03     
/ b! e$ Q/ z; ~0 M7 `Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-78 _. `) k- F& `+ z& h6 N4 q( n" a6 o/ P" s+ ?
Importing GPG key 0x352C64E5:- D$ f+ Z9 g# Q8 t* H: A& j. c
Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"  F: S# E9 O5 l, s
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5) I) p% J- r! j1 A( @- |/ a$ Q
Package    : epel-release-7-11.noarch (@extras)! S8 l3 ^- S1 n! V! k
From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-71 T3 p, H5 `; L4 ~4 d
Running transaction check
) y5 r' u% C: D* F2 X0 tRunning transaction test
. O% |) U1 Z2 FTransaction test succeeded" d8 ^4 E5 F# a8 U# v% D
Running transaction
. m4 ]* J( n" c2 H4 W, V" D  Installing : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64                                   1/5 $ w: ]/ L. ]# M- d& A- R% W$ P
  Installing : 1:net-snmp-libs-5.7.2-49.el7_9.2.x86_64                                                 2/5
+ P0 l" e3 \8 z) l  Installing : 1:net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64                                           3/5
6 h( p0 W5 V+ f2 A  m  Installing : libevent-2.0.21-4.el7.x86_64                                                            4/5 1 K# w- w1 V" j
  Installing : lldpd-1.0.4-1.el7.x86_64                                                                5/5 3 H' E0 x9 z7 I* x  A8 e
  Verifying  : lldpd-1.0.4-1.el7.x86_64                                                                1/5 * k  t/ U8 w* e9 B$ u
  Verifying  : 1:net-snmp-libs-5.7.2-49.el7_9.2.x86_64                                                 2/5
1 B2 h9 }+ t. T* a/ m  Verifying  : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64                                   3/5 / G! W( S9 z! d' D9 B
  Verifying  : libevent-2.0.21-4.el7.x86_64                                                            4/5
$ ]+ U1 V2 [! a( o7 R3 J  Verifying  : 1:net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64                                           5/5
" y% X6 L1 _' @5 A/ g4 Y  m3 R( f, J4 R4 C: E
Installed:8 K& m9 k' e' c) ~: K
  lldpd.x86_64 0:1.0.4-1.el7                                                                               $ M3 w9 D0 l2 [+ m/ q% G
' D+ A. ^; b. x" K9 ?3 q
Dependency Installed:
. T" j( x. q2 Y" P4 s5 z  libevent.x86_64 0:2.0.21-4.el7                  lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7  
% O( N- M/ p5 |/ V  net-snmp-agent-libs.x86_64 1:5.7.2-49.el7_9.2   net-snmp-libs.x86_64 1:5.7.2-49.el7_9.2                  
! @  N& J0 _( a+ B7 G( f
  E8 h0 T: T3 O  p3 J0 aComplete!) p) _# c1 B3 z

; K4 O% _3 H% w1 s2 `. g2 U" G5 W8 C; j) [  _: A( y3 v
启动服务- S: M: k, B' t( W5 @! c  s% Z7 t
systemctl start lldpd.service
  d: |: C. P" v" F; {) K0 R
# P% k4 g3 |* N0 B[root@controller ~]# lldpcli show neighbors! r/ u) ]/ l0 B1 T
-------------------------------------------------------------------------------
" H; q# H* d3 O& A5 d; Z4 Z1 R2 Z3 HLLDP neighbors:+ T5 l4 B- Z3 `2 \* X% i
-------------------------------------------------------------------------------
3 X# \" r$ u/ F% X1 k[root@controller ~]# lldpcli show neighbors- \- a9 u# n/ o( `& k% ~! F
-------------------------------------------------------------------------------
- m& |8 e, B1 r; ?* }5 OLLDP neighbors:, E- ~! `" z+ k" e0 k. r: ?  u' W$ G
-------------------------------------------------------------------------------6 f3 K4 M( E- U  F' m/ ?

0 ?8 A& A3 \7 }$ Z9 K5 A0 R- {; Z8 ~# I/ K2 }; P+ ]% j
+ a* h5 J& d7 i$ S: B7 W- p( W% e
查看所有端口的本地信息(即使未接线,也会显示)) w# o5 c& K+ o$ ~- x% ~) R
[root@controller ~]# lldpcli show neighbors ports ens35 summary8 g$ q; U9 S' ~9 q/ A  K* Z
-------------------------------------------------------------------------------; m' p" E2 }( }* Y
LLDP neighbors:
) R! l: V+ w4 J, X: q! S-------------------------------------------------------------------------------% _$ l3 I2 V  G5 t; f

/ [0 F9 W& d0 A$ ^  g+ a; U: p/ y' E5 b# j
[root@controller ~]# lldpcli show interfaces& W. U8 A% J( L$ b0 U' P3 U
-------------------------------------------------------------------------------
/ A$ @* F# d  \( Q2 [LLDP interfaces:
' [% k$ }; b9 V/ A% I-------------------------------------------------------------------------------" h; n& X/ P% A2 W) e( r
Interface:    ens33, via: unknown, Time: 0 day, 00:04:33! D0 ^& E  c: E% ?0 T
  Chassis:     1 \( p4 F( e  P* M! @! {, z+ h
    ChassisID:    mac 00:0c:29:c2:76:a68 P) _+ @. d& r5 |2 f5 h5 {$ M
    SysName:      controller
3 [. X3 h3 U# s3 t* I    SysDescr:     CentOS Linux 7 (Core) Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64. U. z  z) b) g; U& L' c" B6 A
    MgmtIP:       162.96.90.165
+ e5 j% y0 s9 `3 n) d    MgmtIP:       fe80::20c:29ff:fec2:76a6  H' P9 K7 U7 l- i
    Capability:   Bridge, off
0 G6 L0 Z, a2 g* Z9 L4 F    Capability:   Router, off
/ s  a* v7 x) `' b  C! j+ J3 \    Capability:   Wlan, off1 B3 D1 _1 K+ g: z8 b7 ?5 f
    Capability:   Station, on
9 [) B4 d0 ^+ B7 \: k* B& g  Port:        
/ x: z& M1 Z9 m7 |7 D  L/ O1 e    PortID:       mac 00:0c:29:c2:76:a6( i* l# H! y5 l
    PortDescr:    ens337 |# @% e! E7 v% c9 C9 s. p+ Y
  TTL:          120
1 ?3 z5 s7 Z3 h5 F5 X* a-------------------------------------------------------------------------------
. G) r  M6 O. i: O, G, NInterface:    ens34, via: unknown, Time: 0 day, 00:04:33
& ~8 Z7 a( _  M+ {1 e% C3 u! L5 o/ t  Chassis:     1 [9 c! @# }: M* T3 R4 S
    ChassisID:    mac 00:0c:29:c2:76:a6
  w5 ~" n9 f& D% [" Q    SysName:      controller. p* Z  f& {& c4 m$ l8 d
    SysDescr:     CentOS Linux 7 (Core) Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64- v% m4 z. B$ o3 [  P% E4 E
    MgmtIP:       162.96.90.1654 j) z& r: S! I2 i3 G% K0 ], F$ s
    MgmtIP:       fe80::20c:29ff:fec2:76a6
/ ]9 l9 O6 Q( M: E% |    Capability:   Bridge, off$ m# f( J& o( X( I& _
    Capability:   Router, off* A  Z/ @2 W0 k9 d; f3 {
    Capability:   Wlan, off6 }# o) d' a% e8 J  z8 q
    Capability:   Station, on1 e; L4 G; _: S/ j0 R, m
  Port:        8 f8 D) J6 D8 m$ i4 |
    PortID:       mac 00:0c:29:c2:76:b0
7 }7 r( A/ f) p, E6 q  z5 `2 f6 g$ u    PortDescr:    ens34
$ i% _) X) }) A9 `( `9 N3 _) g) |  TTL:          1206 w! w' F5 r+ f( k
-------------------------------------------------------------------------------
' Q6 j" C. h! F- y' dInterface:    ens35, via: unknown, Time: 0 day, 00:04:336 Q; W9 u( r, a, `4 Y9 V
  Chassis:       u. O2 R, `& B3 }; Y
    ChassisID:    mac 00:0c:29:c2:76:a6& N, m( R" J* ?" N* t, {
    SysName:      controller
- }9 L9 Q: ?2 S( I9 u2 c1 a    SysDescr:     CentOS Linux 7 (Core) Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64
9 p/ E: w3 r$ G. \$ v    MgmtIP:       162.96.90.1655 ?$ n% U5 A- p5 ?% H2 e! S; C- b8 d
    MgmtIP:       fe80::20c:29ff:fec2:76a63 l# e" V3 g! d5 W- g: a" h: J
    Capability:   Bridge, off
$ t) ]/ ~2 \# {# J# M. ~    Capability:   Router, off
! z+ S+ W/ }6 Q; _$ }$ X    Capability:   Wlan, off
; Q+ d3 m( f' m* _    Capability:   Station, on
) b2 c5 ?: ]( t7 v  Port:        
5 t* E2 \9 J- `    PortID:       mac 00:0c:29:c2:76:ba( b" y+ ^" B2 X2 X8 \6 B# \+ S+ V
    PortDescr:    ens351 [, a7 }* {: G
  TTL:          120
7 t$ G: J8 \! x5 E; ?1 m-------------------------------------------------------------------------------
; T% B  R( w" b8 d+ b
+ C+ h3 U% `( R0 C8 p6 B! w$ @0 q1 _! R+ }+ z% j: r6 n

  f( W1 |( w( l
: @+ P& d) }4 V! u查看ens35端口的本地信息(即使未接线,也会显示)" s9 M; ^- P( P3 ?, K+ ~+ o9 V1 v
. _0 }6 W$ h$ Q8 p% A
[root@controller ~]# lldpcli show interfaces ports ens35 summary4 {% c; p& T, W( ]5 U& b3 P
-------------------------------------------------------------------------------
% J5 l& H8 U. z2 ~8 FLLDP interfaces:5 b# w1 n3 ^) G$ i
-------------------------------------------------------------------------------# d( }( T2 d* h( y0 f
Interface:    ens35, via: unknown
0 G2 q% J6 b" q1 R3 U  x& f: q  Chassis:     % L6 p1 k  p8 O, ]) T. u$ z
    ChassisID:    mac 00:0c:29:c2:76:a6
9 x1 o% Q- s: N    SysName:      controller
# S4 H1 c, t/ `3 A* E  Port:        , d" X* k9 d6 ]" B6 \# l0 v7 A
    PortID:       mac 00:0c:29:c2:76:ba
7 Y6 r2 U. h* C& B* H( M$ k    PortDescr:    ens35" F  ]$ H: v. N
  TTL:          120
& U- N. _: t" m0 T; d-------------------------------------------------------------------------------) o* o+ k- V1 p' ]
) m  N9 a. l1 i  L0 l% Z
2 j& s4 z8 Q& Q1 E' \3 `3 ?3 o4 U

- F. q5 k* v0 w  ], g
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 00:48 , Processed in 0.017398 second(s), 23 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表