Connector/H3c.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 142 CC 10 SCAR 28.4
CC
10
Cov
0%
execCommands
42.0
enable
12.0
waitfor
6.0
CRAP 17.1
1 package PDK::Connector::H3c;
2
3 use utf8;
4 use v5.30;
5 use Moose;
6 use Carp;
7 use Data::Dumper;
8 use Expect qw(exp_continue);
9
10 # 继承连接器常用功能
11 with 'PDK::Connector::Role';
12
13 # 登录成功提示符(H3C设备特有格式)
14 has prompt => (
15 is => 'ro',
16 required => 0,
17 default => '^\s*(\x00)?[<\[].*?[>\]]\s*$', # 匹配H3C设备的提示符格式
18 );
19
20 # 进入特权模式
21 # H3C设备使用su命令而非enable进入特权模式
22 sub enable {
23 my $self = shift;
24 my $enPass = $self->{enPassword} // $self->{password}; # 优先使用特权密码
25 my $username = $self->{username};
26 my $exp = $self->{exp};
27
28 $exp->send("su\n"); # H3C使用su命令进入特权模式
29 my $enable = 1; # 标记是否已尝试输入密码
30 my $result = 0; # 操作结果
31
32 # 处理进入特权模式的过程
33 my @ret = $exp->expect($self->{timeout},
34 # 处理密码提示
35 [ qr/assword:\s*$/ => sub {
36 if ($enable) {
37 $enable = 0;
38 $exp->send("$enPass\n"); # 输入密码
39 }
40 else {
41 croak("[切换到特权模式异常] 用户或使能密码错误!")
42 }
43 exp_continue;
44 } ],
45 # 处理登录名提示
46 [ qr/ogin:\s*$|name:\s*$/i => sub {
47 $exp->send("$username\n"); # 输入用户名
48 exp_continue;
49 } ],
50 # 成功进入特权模式(匹配H3C特权模式提示)
51 [ qr/(privilege\s+level\s+is.+>\s*\z)|(privilege\s+is\s+.+>\s*\z)/si => sub {
52 $self->{_enable_} = 1;
53 $result = 1;
54 } ],
55 # 进入特权模式失败
56 [ qr/\^.*$/i => sub {
57 croak("请联系管理员检查原因: $1")
58 } ],
59 );
60
61 if (defined $ret[1]) {
62 croak("[切换到特权模式异常]" . $ret[3] . $ret[1]);
63 }
64
65 return $result;
66 }
67
68 # 等待设备响应,处理各种交互提示
69 sub waitfor {
70 my ($self, $prompt) = @_;
71 $prompt //= $self->{prompt}; # 使用提供的提示符或默认提示符
72
73 my $exp = $self->{exp};
74 my $buff = "";
75
76 # 处理设备输出中的各种情况
77 my @ret = $exp->expect($self->{timeout},
78 # 处理分页显示(H3C特有格式)
79 [ qr/---- More ----.*$/mi => sub {
80 $self->send(" "); # 发送空格继续显示
81 $buff .= $exp->before();
82 exp_continue;
83 } ],
84 # 处理确认提示
85 [ qr/(Are you sure|overwrite|Continue|save operation)\? \[Y\/N\]:/i => sub {
86 $self->send("Y\r"); # 自动确认
87 $buff .= $exp->before() . $exp->match();
88 exp_continue;
89 } ],
90 # 处理选择提示
91 [ qr/Before pressing ENTER you must choose/i => sub {
92 $self->send("Y\r"); # 自动选择是
93 $buff .= $exp->before() . $exp->match();
94 exp_continue;
95 } ],
96 # 处理按键提示
97 [ qr/press the enter key/i => sub {
98 $self->send("\r"); # 发送回车
99 $buff .= $exp->before() . $exp->match();
100 exp_continue;
101 } ],
102 # 匹配命令提示符,表示命令执行完成
103 [ qr/$prompt/m => sub {
104 $buff .= $exp->before() . $exp->match();
105 } ]
106 );
107
108 if (defined $ret[1]) {
109 croak("[等待回显捕捉异常]" . $ret[3] . $ret[1]);
110 }
111
112 # 清理输出内容(H3C设备特有控制字符)
113 $buff =~ s/\cM+[ ]+\cM//g; # 删除退格效果
114 $buff =~ s/\cM{2}//g; # 删除多余回车符
115 $buff =~ s/\cM//g; # 删除回车符
116 $buff =~ s/\x00//g; # 删除空字符
117
118 return $buff;
119 }
120
121 # 执行多个命令
122 sub execCommands {
123 my ($self, $commands) = @_;
124
125 # 检查登录状态,如果未登录则先登录
126 if ($self->{_login_} == 0) {
127 my $ret = $self->login();
128 return $ret if $ret->{success} == 0;
129 }
130
131 my $result = "";
132 # 如果未进入特权模式,则先进入特权模式
133 $self->enable() if not $self->{_enable_};
134
135 # 逐个执行命令
136 for my $cmd (@{$commands}) {
137 $self->send("$cmd\n");
138 my $buff = $self->waitfor();
139
140 # 检查命令是否执行失败(H3C特有错误消息)
141 if ($buff =~ /Ambiguous|Incomplete|not recognized|Unrecognized/i) {
142 return {
143 success => 0,
144 failCommand => $cmd, # 失败的命令
145 result => $result, # 已成功执行的命令结果
146 reason => $buff # 失败原因
147 };
148 }
149 elsif ($buff =~ /Too many parameters|Invalid (input|command|file)|Unknown command|Wrong parameter/i) {
150 return {
151 success => 0,
152 failCommand => $cmd, # 失败的命令
153 result => $result, # 已成功执行的命令结果
154 reason => $buff # 失败原因
155 };
156 }
157 else {
158 $result .= $buff; # 保存命令执行结果
159 }
160 }
161
162 return { success => 1, result => $result };
163 }
164
165 # 获取设备配置方法(H3C特有命令)
166 sub getConfig {
167 my $self = shift;
168 my $commands = [
169 "screen-length disable", # 禁用分页显示
170 "dis current-configuration", # 显示当前配置
171 "save force", # 强制保存配置
172 ];
173
174 my $config = $self->execCommands($commands);
175 my $lines = "";
176
177 if ($config->{success} == 1) {
178 $lines = $config->{result};
179 }
180 else {
181 return $config; # 返回错误信息
182 }
183
184 return { success => 1, config => $lines };
185 }
186
187 # 使Moose类不可变,提高性能
188 __PACKAGE__->meta->make_immutable;
189 1;
190
191 # 模块文档
192 =encoding utf8
193 =head1 NAME
194
195 PDK::Connector::H3c - H3C 华三设备连接器
196
197 =head1 SYNOPSIS
198
199 use PDK::Connector::H3c;
200
201 # 创建 H3C 设备连接器实例
202 my $conn = PDK::Connector::H3c->new(
203 host => '192.168.1.1',
204 username => 'admin',
205 password => 'password',
206 enPassword => 'enable_password' # 特权密码
207 );
208
209 # 登录设备
210 $conn->login;
211
212 # 获取设备配置
213 my $config = $conn->getConfig;
214
215 # 执行自定义命令
216 my $result = $conn->execCommands(['display version', 'display interface']);
217
218 =head1 DESCRIPTION
219
220 H3C 华三网络设备专用连接器,继承 L<PDK::Connector::Role> 角色,提供与 H3C 设备的交互功能。
221
222 支持设备登录、特权模式切换、命令执行、配置获取等操作,专门处理 H3C 设备特有的交互流程和提示信息。
223
224 =head1 ATTRIBUTES
225
226 =head2 prompt
227
228 H3C 设备命令行提示符的正则表达式模式,默认匹配包含尖括号或方括号的提示符格式。
229
230 =head1 METHODS
231
232 =head2 enable
233
234 $conn->enable;
235
236 进入特权模式。H3C 设备使用 'su' 命令而非 'enable' 进入特权模式,自动处理密码输入和权限验证。
237
238 =head2 waitfor
239
240 my $output = $conn->waitfor($prompt);
241
242 等待设备响应,处理 H3C 设备特有的交互提示:
243 - 分页显示控制(---- More ----)
244 - 操作确认提示(Are you sure? [Y/N])
245 - 选择提示(Before pressing ENTER you must choose)
246 - 按键提示(press the enter key)
247
248 自动清理输出中的控制字符和特殊字符,返回处理后的文本内容。
249
250 =head2 execCommands
251
252 my $result = $conn->execCommands(\@commands);
253
254 执行多个命令序列。自动检测 H3C 特有的错误消息(Ambiguous/Incomplete/Unrecognized/Invalid command 等),返回包含执行结果的结构化数据。
255
256 =head2 getConfig
257
258 my $config = $conn->getConfig;
259
260 获取设备完整配置。使用 H3C 特有的命令序列:
261 - 禁用分页显示(screen-length disable)
262 - 显示当前配置(dis current-configuration)
263 - 强制保存配置(save force)
264
265 =head1 SEE ALSO
266
267 L<PDK::Connector::Role>, L<Expect>, L<Moose>
268
269 =head1 AUTHOR
270
271 WENWU YAN E<lt>968828@gmail.comE<gt>
272
273 =head1 LICENSE AND COPYRIGHT
274
275 版权所有 2025 WENWU YAN。
276
277 本软件按Perl自身许可条款发布。
278 =cut