Connector/Radware.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 105 CC 7 SCAR 22.3
CC
7
Cov
0%
execCommands
20.0
waitfor
12.0
getConfig
6.0
CRAP 9.3
1 package PDK::Connector::Radware;
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 # 登录成功提示符(Radware设备特有格式)
14 has prompt => (
15 is => 'ro',
16 required => 0,
17 default => '^>>.*?#\s*$', # 匹配Radware设备的提示符格式
18 );
19
20 # 特权模式状态标志(Radware设备默认进入特权模式)
21 has _enable_ => (
22 is => 'ro',
23 required => 0,
24 default => 1, # Radware设备默认进入特权模式
25 );
26
27 # 等待设备响应,处理各种交互提示
28 sub waitfor {
29 my ($self, $prompt) = @_;
30 $prompt //= $self->{prompt}; # 使用提供的提示符或默认提示符
31
32 my $exp = $self->{exp};
33 my $buff = "";
34
35 # 等待设备响应,处理各种交互提示
36 my @ret = $exp->expect($self->{timeout},
37 # 处理保存确认提示
38 [ qr/Confirm saving without first applying changes/i => sub {
39 $self->send("y\r"); # 发送y确认
40 $buff .= $exp->before() . $exp->match();
41 exp_continue;
42 } ],
43 # 处理FLASH保存确认提示
44 [ qr/Confirm saving to FLASH/i => sub {
45 $self->send("y\r"); # 发送y确认
46 $buff .= $exp->before() . $exp->match();
47 exp_continue;
48 } ],
49 # 处理信息转储确认提示
50 [ qr/Confirm dumping all information/i => sub {
51 $self->send("y\r"); # 发送y确认
52 $buff .= $exp->before() . $exp->match();
53 exp_continue;
54 } ],
55 # 处理私钥显示确认提示
56 [ qr/(Display|Include) private keys/i => sub {
57 $self->send($self->{passphrase} ? "y\r" : "n\r"); # 根据是否有密码短语选择
58 $buff .= $exp->before() . $exp->match();
59 exp_continue;
60 } ],
61 # 处理密码短语输入提示
62 [ qr/(Enter|Reconfirm) passphrase/i => sub {
63 $self->send("$self->{passphrase}\r"); # 发送密码短语
64 $buff .= $exp->before() . $exp->match();
65 exp_continue;
66 } ],
67 # 匹配命令提示符
68 [ qr/$prompt/m => sub {
69 $buff .= $exp->before() . $exp->match(); # 保存完整输出
70 } ]
71 );
72
73 # 检查Expect操作是否出错
74 if (defined $ret[1]) {
75 croak("[等待回显捕捉异常]" . $ret[3] . $ret[1]);
76 }
77
78 # 清理输出内容(Radware设备特有控制字符)
79 $buff =~ s/\c@\cH+\s+\cH+//g; # 移除退格控制字符及其影响的文本
80 $buff =~ s/\cM//g; # 移除回车符(CR),保留换行符(LF)
81
82 return $buff;
83 }
84
85 # 执行多个命令
86 sub execCommands {
87 my ($self, $commands) = @_;
88
89 # 检查登录状态,如果未登录则先登录
90 if (!$self->{_login_}) {
91 my $ret = $self->login();
92 return $ret unless $ret->{success};
93 }
94
95 my $result = '';
96 # 逐个执行命令
97 for my $cmd (@{$commands}) {
98 $self->send("$cmd\n");
99
100 # 检查命令是否执行失败(Radware特有错误消息)
101 my $buff = $self->waitfor();
102 if ($buff =~ /Error:/i) {
103 return {
104 success => 0,
105 failCommand => $cmd,
106 result => $result,
107 reason => $buff
108 };
109 }
110 else {
111 $result .= $buff; # 保存命令执行结果
112 }
113 }
114
115 return { success => 1, result => $result };
116 }
117
118 # 获取设备配置方法(Radware特有命令)
119 sub getConfig {
120 my $self = shift;
121 my $commands = [
122 "cfg/dump", # 转储配置
123 "cd", # 切换目录(可能用于重置上下文)
124 ];
125 my $config = $self->execCommands($commands);
126 my $lines = "";
127
128 if ($config->{success} == 1) {
129 $lines = $config->{result};
130 }
131 else {
132 return $config; # 返回错误信息
133 }
134
135 return { success => 1, config => $lines };
136 }
137
138 # 使Moose类不可变
139 __PACKAGE__->meta->make_immutable;
140 1;
141
142 =encoding utf8
143 =head1 NAME
144
145 PDK::Connector::Radware - Radware 设备连接器
146
147 =head1 SYNOPSIS
148
149 use PDK::Connector::Radware;
150
151 # 创建 Radware 设备连接器实例
152 my $conn = PDK::Connector::Radware->new(
153 host => '192.168.1.1',
154 username => 'admin',
155 password => 'password'
156 );
157
158 # 登录设备
159 $conn->login;
160
161 # 执行配置转储命令
162 my $config = $conn->getConfig;
163
164 # 执行自定义命令
165 my $result = $conn->execCommands(['show version', 'show status']);
166
167 =head1 DESCRIPTION
168
169 Radware 设备专用连接器,继承 L<PDK::Connector::Role> 角色,提供与 Radware 网络设备的交互功能。
170
171 支持设备登录、命令执行、配置获取等操作,自动处理 Radware 设备特有的交互提示和控制字符。
172
173 =head1 ATTRIBUTES
174
175 =head2 prompt
176
177 Radware 设备命令行提示符的正则表达式模式,默认匹配 '^>>.*?#\s*$' 格式。
178
179 =head2 _enable_
180
181 特权模式状态标志,Radware 设备默认进入特权模式,值为 1。
182
183 =head1 METHODS
184
185 =head2 waitfor
186
187 my $output = $conn->waitfor($prompt);
188
189 等待设备响应,处理 Radware 设备特有的交互提示:
190 - 配置保存确认
191 - FLASH 存储确认
192 - 信息转储确认
193 - 私钥显示确认
194 - 密码短语输入
195
196 自动清理输出中的控制字符,返回处理后的文本内容。
197
198 =head2 execCommands
199
200 my $result = $conn->execCommands(\@commands);
201
202 执行多个命令序列。自动检测命令执行错误,返回包含执行结果的结构化数据。
203
204 =head2 getConfig
205
206 my $config = $conn->getConfig;
207
208 获取设备完整配置。使用 Radware 特有的 'cfg/dump' 命令转储配置信息。
209
210 =head1 SEE ALSO
211
212 L<PDK::Connector::Role>, L<Expect>, L<Moose>
213
214 =head1 AUTHOR
215
216 WENWU YAN E<lt>968828@gmail.comE<gt>
217
218 =head1 LICENSE AND COPYRIGHT
219
220 版权所有 2025 WENWU YAN。
221
222 本软件按Perl自身许可条款发布。
223 =cut